Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a Tab character (in PRE element) as 4 spaces in HTML? [duplicate]

Tags:

html

Possible Duplicate:
Specifying Tab-Width?

I need to print a tab character in a pre tag. The HTML character for tab is 	 but on all browsers I tried it renders to look like 8 spaces.

Is there a way to adjust this to render to be the width of 4 spaces or is there another HTML character I can use that is 4 spaces?

like image 303
jhchen Avatar asked Oct 02 '12 23:10

jhchen


People also ask

How do you render tabs in HTML?

The Proper Way. Tabs are for tabulating data. The proper way to tabulate data in HTML is to use the table tag. Every line in your original string translates to a row in the table, while each tab in the original string starts a new (left-aligned) cell in the table.

How do you add a tab space in HTML?

The tab character can be inserted by holding the Alt and pressing 0 and 9 together.

What does \t do in HTML?

It is equivalent to the tab character as such. Thus, from the HTML viewpoint, there is no need to “escape” it using the character reference; but you may do so e.g. if your editing program does not let you enter the character conveniently.


1 Answers

While I agree with the first answer that you might as well just put 4 spaces, there does seem to be some support for a tab-size property within CSS (though IE support is lacking):

pre {
    -moz-tab-size:    4; /* Firefox 4+ */
    -o-tab-size:      4; /* Opera 11.5 & 12.1 only */
    tab-size:         4; /* Chrome 21+, Safari 6.1+, Opera 15+ */
}

Only effective using white-space: pre or white-space: pre-wrap (or within <pre> tags).

  • W3C specification
  • jsFiddle demonstration
  • Browser support
like image 177
deizel Avatar answered Sep 29 '22 11:09

deizel