Abbey Graebner
Abbey Graebner

Reputation: 2037

How to get a tab character?

In HTML, there is no character for a tab, but I am confused as to why I can copy and paste one here: " " (You can't see the full width of it, but if you click to edit my question, you will see the character.) If I can copy and paste a tab character, there should be a unicode equivalent that can be coded into html. I know it doesn't exist, but this is a mystery I've never been able to grasp.

So my question is: why is there not a unicode character for a tab even if I can copy and paste it?

Upvotes: 199

Views: 502169

Answers (7)

Duane Lortie
Duane Lortie

Reputation: 1260

As mentioned, for efficiency reasons sequential spaces are consolidated into one space the browser actually displays. Remember what the ML in HTML stand for. It's a Mark-up Language, designed to control how text is displayed.. not whitespace :p

Still, you can pretend the browser respects tabs since all the TAB does is prepend 4 spaces, and that's easy with CSS. either in line like ...

 <div style="padding-left:4.00em;">Indented text </div>

Or as a regular class in a style sheet

.tabbed {padding-left:4.00em;}

Then the HTML might look like

<p>regular paragraph regular paragraph regular paragraph</p>
<p class="tabbed">Indented text Indented text Indented text</p>
<p>regular paragraph regular paragraph regular paragraph</p>

Upvotes: 1

josh3736
josh3736

Reputation: 144912

Sure there's an entity for tabs:

&#9;

(The tab is ASCII character 9, or Unicode U+0009.)

However, just like literal tabs (ones you type in to your text editor), all tab characters are treated as whitespace by HTML parsers and collapsed into a single space except those within a <pre> block, where literal tabs will be rendered as 8 spaces in a monospace font.

Upvotes: 242

Abhishek Goel
Abhishek Goel

Reputation: 19751

Try &emsp;

as per the docs :

The character entities &ensp; and &emsp; denote an en space and an em space respectively, where an en space is half the point size and an em space is equal to the point size of the current font. For fixed pitch fonts, the user agent can treat the en space as being equivalent to A space character, and the em space as being equuivalent to two space characters.

Docs link : https://www.w3.org/MarkUp/html3/specialchars.html

Upvotes: 162

GrayFace
GrayFace

Reputation: 1381

I use <span style="display: inline-block; width: 2ch;">&#9;</span> for a two characters wide tab.

Upvotes: 7

crokusek
crokusek

Reputation: 5624

Posting another alternative to be more complete. When I tried the "pre" based answers, they added extra vertical line breaks as well.

Each tab can be converted to a sequence non-breaking spaces which require no wrapping.

"&nbsp;&nbsp;&nbsp;&nbsp;" 

This is not recommended for repeated/extensive use within a page. A div margin/padding approach would appear much cleaner.

Upvotes: 22

Raymund
Raymund

Reputation: 7892

put it in between <pre></pre> tags then use this characters &#9;

it would not work without the <pre></pre> tags

Upvotes: 61

DanRedux
DanRedux

Reputation: 9349

Tab is [HT], or character number 9, in the unicode library.

Upvotes: 2

Related Questions