tags:

views:

2127

answers:

8

I have a situation as follows

<body>
Test<br />
test<br />
test1<br />
</body>

I need to add a tab after the 2nd test and 3rd test

so that it looks similar to this.

Test
    test
        test1

Is there a special HTML entity or special character for TAB. eg. Non-breaking space == & nbsp ;

thanks

A: 

If you need to display tabs (tabulator characters), use a PRE element (or any element with the white-space: pre; CSS applied to it).

<!doctype html>
<html>
 <head>
  <title>Test</title>
  <style type="text/css">
   pre { white-space: pre; }
  </style>
 </head>
 <body>
  <p>This is a normal paragraph, blah blah blah.</p>
  <pre>This is preformatted text contained within a <code>PRE</code> element. Oh, and here are some tab characters, each of which are displayed between two arrows: ← → ← → ← → ← →</pre>
 </body>
</html>

You can also use the HTML entity &#x09; instead of the actual tab character.

Mathias Bynens
Or any block element with `white-space: nowrap;`
l0b0
Doesn't the pre tag screw up a lot of formatting and force an ugly uniform character width font?
Sam152
The code in the codeblocks on this site are in pre tags. Are they screwed up and ugly?
Macha
Ok, the first part is not helpful, but the is a correct solution, so don't understand the downvote.
pihentagy
A: 

[Answer withdrawn]

Nick Josevski
pre is entirely unnecessary here, far better to add padding-left based on em units to nested elements
annakata
The newly accepted answer contains the pre element, visually it achieves the effect of tabs...
Nick Josevski
and semantically it really doesn't...
annakata
A: 

See Making a 'Tab' in HTML by Neha Sinha:

Preformatted

You can put tab characters in your HTML directly if you use what’s called “preformatted” text.In HTML, surround text that you want “preformatted” in a pair of “<pre>” and “</pre>” start and end tags.

Tables

You can use a html table so that everything you put within the set of rows(<tr>) and columns(<td>) shows up the same way. You can very well hide the table borders to show the text alone.

Using the <dd> Tag

The <dd> tag is for formatting definitions. But it also will create a line break and make a tab!

&nbsp;, The Non-Breaking Space

One bit of HTML code I used in the table example is the “non-breaking space,” encoded as in HTML. This just gives you some space. Combined with a line break, <br>, you can create some tab-like effects.

Example

Test<br/>
<pre>   </pre>test<br/>
<pre>    </pre>test1<br/>

this should render as:

Test
    test
     test1
eed3si9n
pre is entirely unnecessary here, far better to add padding-left based on em units to nested elements
annakata
@annakata, there's more than one way to skin the cat on this one. I'd personally prefer the css way, but there could be situations that's an overkil, or simply not possible. pre I think is a quick hack.
eed3si9n
this was the most appropriate answer out of everyones....esp mine!
James
This answer does not deserve to be accepted, especially since the asker didn't even use this method to solve his problem.
Thomas Owens
*DO NOT* use this answer as an accepted solution. The options of using pre, definition lists and/or non-breaking spaces is completely inappropriate for creating 'tabs'. George IV's answer is by far the most relevant.
Metro Smurf
Hey, give @annakata credit. That solution makes plenty of sense too.
geowa4
+16  A: 

The simplest way I can think of would be to place the text in nested divs. Then add margin to the left of div. It will cascade down, giving you indentation.

<div id='testing'>
  Test1
  <div>
    Test2
    <div>
      Test3
    </div>
  </div>
</div>

With the CSS:

#testing div {
  margin-left: 10px;/*or whatever indentation size you want*/
}

With those, you'll get a nice tree, no matter how many levels deep you want to go.

EDIT: You can also use padding-left if you want.

geowa4
+1 For mentioning an infinite level depth. It could come in very handy, plus it would work better for dynamic content.
Sam152
I say list over div, but at least that's a subjective conversation. +1
annakata
The question was to display tab characters in HTML pages, not how to fake them using CSS...
Mathias Bynens
@Matthias - ridiculous concern, CSS is a more than reasonable assumption given HTML. Considerably less than 1% of your audience won't have CSS.
annakata
@annakata Lists are great, but for infinite depth, it is much easier to just throw in a div under the parent node. And xpath traversal is reduced as you no longer have to add `ul` all over the place.@Mathias Bynens My answer is better at solving his problem than using tab characters.
geowa4
ew divs - why not choose something that has semantics, maybe a list or a table, depending on the kind of data?
Jørn Schou-Rode
@annakata, a CSS solution more reasonable? That depends on the situation. Since the question was how to display tab _characters_, I'm guessing he actually wants to display the characters, not just indent some text. There is a big difference.
Mathias Bynens
I have no idea why this or the list option is not the accepted answer - using a list or divs are clearly the best options.
Thomas Owens
@Jørn Schou-Rode: divs for simplicity
geowa4
+6  A: 

Ye gods, tables?

Looks like an obvious use-case for lists, with variable margin and list-style-type: none; seasoned to taste.

annakata
+1 for lists, it is obvious.
jjclarkson
It isn't obvious. The example given by the OP is too vague to tell if it is list data or something else.
David Dorward
+3  A: 

I think that easiest thing to do is to use UL/LI html tags and then to manipulate (and remove if needed) symbols in front of list with CSS.

Then you get something like:

  • Test
  • Test2
    • Test 3

More info + working example you can try out.

Perica Zivkovic
+4  A: 

There have been a variety of good and bad answers so far but it seems no-one has addressed the way that you can choose between the solutions.

The first question to ask is "What is the relationship between the data being displayed?". Once this has been answered it the HTML structure you use should be obvious.

Please update the question explaining more about the structure of the content you need to display and you should find that you get better answers. At the moment everything from using <pre> to tables might be the best solution.

edeverett
+2  A: 

If you really want to use tabs (== tabulator characters), you have to go with the following solution, which I don't recommend:

<pre>
test
&#x09;test
&#x09;&#x09;test
</pre>

or replace the <pre/> with <div style="white-space: pre" /> to achieve the same effect as with the pre element. You can even enter a literal tab character instead of the escaped &#x09;.

I don't recommend it for most usages, because it is not really semantic, that is, from viewing the HTML source a program cannot deduce any useful information (like, e.g., "this is a heading" or such). You'd be better off using one of the nice margin-left examples of the other answers. However, if you'd like to display some stuff like source code or the such, the above solution would do it.

Cheers,

Boldewyn