Tab space instead of multiple non-breaking spaces ("nbsp")?

Viewed 3270264

Is it possible to insert a tab character in HTML instead of having to type   four times?

37 Answers

  is the answer.

However, they won't be as functional as you might expect if you are used to using horizontal tabulations in word-processors e.g. Word, Wordperfect, Open Office, Wordworth, etc. They are fixed width, and they cannot be customised.

CSS gives you far greater control and provides an alternative until the W3C provide an official solution.

Example:

padding-left:4em 

..or..

margin-left:4em 

..as appropriate

It depends on which character set you want to use.

You could set up some tab tags and use them similar to how you would use h tags.

<style>
    tab1 { padding-left: 4em; }
    tab2 { padding-left: 8em; }
    tab3 { padding-left: 12em; }
    tab4 { padding-left: 16em; }
    tab5 { padding-left: 20em; }
    tab6 { padding-left: 24em; }
    tab7 { padding-left: 28em; }
    tab8 { padding-left: 32em; }
    tab9 { padding-left: 36em; }
    tab10 { padding-left: 40em; }
    tab11 { padding-left: 44em; }
    tab12 { padding-left: 48em; }
    tab13 { padding-left: 52em; }
    tab14 { padding-left: 56em; }
    tab15 { padding-left: 60em; }
    tab16 { padding-left: 64em; }
</style>

...and use them like so:

<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Tabulation example</title>

        <style type="text/css">
            dummydeclaration { padding-left: 4em; } /* Firefox ignores first declaration for some reason */
            tab1 { padding-left: 4em; }
            tab2 { padding-left: 8em; }
            tab3 { padding-left: 12em; }
            tab4 { padding-left: 16em; }
            tab5 { padding-left: 20em; }
            tab6 { padding-left: 24em; }
            tab7 { padding-left: 28em; }
            tab8 { padding-left: 32em; }
            tab9 { padding-left: 36em; }
            tab10 { padding-left: 40em; }
            tab11 { padding-left: 44em; }
            tab12 { padding-left: 48em; }
            tab13 { padding-left: 52em; }
            tab14 { padding-left: 56em; }
            tab15 { padding-left: 60em; }
            tab16 { padding-left: 64em; }

        </style>

    </head>

    <body>
        <p>Non tabulated text</p>

        <p><tab1>Tabulated text</tab1></p>
        <p><tab2>Tabulated text</tab2></p>
        <p><tab3>Tabulated text</tab3></p>
        <p><tab3>Tabulated text</tab3></p>
        <p><tab2>Tabulated text</tab2></p>
        <p><tab3>Tabulated text</tab3></p>
        <p><tab4>Tabulated text</tab4></p>
        <p><tab4>Tabulated text</tab4></p>
        <p>Non tabulated text</p>
        <p><tab3>Tabulated text</tab3></p>
        <p><tab4>Tabulated text</tab4></p>
        <p><tab4>Tabulated text</tab4></p>
        <p><tab1>Tabulated text</tab1></p>
        <p><tab2>Tabulated text</tab2></p>

    </body>

</html>

Run the snippet above to see a visual example.

Extra discussion

There are no horizontal tabulation entities defined in ISO-8859-1 HTML, however there are some other white-space characters available than the usual &nbsp, for example; &thinsp;, &ensp; and the aforementioned &emsp;.

It's also worth mentioning that in ASCII and Unicode, &#09; is a horizontal tabulation.

Below are the 3 different ways provided by HTML to insert empty space

  1. Type &nbsp; to add a single space.
  2. Type &ensp; to add 2 spaces.
  3. Type &emsp; to add 4 spaces.

If you needed only one tab, the following worked for me.

<style>
  .tab {
    position: absolute;
    left: 10em;
   }
</style>

with the HTML something like:

<p><b>asdf</b> <span class="tab">99967</span></p>
<p><b>hjkl</b> <span class="tab">88868</span></p> 

You can add more "tabs" by adding additional "tab" styles and changing the HTML such as:

<style>
  .tab {
    position: absolute;
    left: 10em;
   }
  .tab1 {
    position: absolute;
    left: 20em;
   }
</style>

with the HTML something like:

<p><b>asdf</b> <span class="tab">99967</span><span class="tab1">hear</span></p>
<p><b>hjkl</b> <span class="tab">88868</span><span class="tab1">here</span></p>

There is a simple css for it:

white-space: pre;

It keeps the spaces that you add in the HTML rather than unifying them.

Instead of writing &nbsp; four time for space equal to tab, you can write &emsp; once.

The ideal CSS code that should be used should be a combination of "display" and "min-width" properties...

display: inline-block;
min-width: 10em; // ...for example, depending on the uniform width to be achieved for the items [in a list], which is a common scenario where tab is often needed.

If you want the TABs

  • work like tabulators, even in contenteditables
  • don't want to use the ugly "pre" fonts

then use instead of nbsp's:

<pre class='TAB'>&#009;</pre>

Place this in in your CSS:

.TAB {
margin:0; padding:0;
display:inline;
tab-size: 8;
}

Change the tab-size to your needs.

we use custom space span class

<span class='space'></span>

Include space class in CSS.

.space 
{
   margin-left:45px;
}

The &nbsp; unit is font size dependent. To understand that dependency, consider em and rem units of measure, using this CSS/HTML method.

CSS

.tab { margin-left: 1rem; }

HTML

<div class=“tab> ...p and h elements... </div>

DISPLAY

1em approximately equals 4 &nbsp; but &nbsp; em unit and &nbsp; rem unit display depend on element and style use, indenting something like this.

--------h1 indented 1em
--p indented 1em
----h1 indented 1rem
----p indented 1rem

Both margin-left and padding-left work. There is no tab unit of length in HTML or CSS. A .tab class selector can be used.

p { background-color: #FFEEEE; margin: 0; padding: 0.5em 0; }
<p style="white-space: pre-wrap; tab-size: 4">&#9← One tab&#9&#9← two tabs&#9&#9&#9← three tabs&#9&#9&#9&#9← four tabs.</p>
<p style="white-space: pre-wrap; tab-size: 4">  ← This one is one character.</p>
<p style="white-space: pre-wrap; tab-size: 42">&#9← This one has come out too far.</p>
<p style="white-space: pre-wrap; tab-size: 8">&#9← Say! what a lot of tabs there are.</p>

TL;DR (Too lazy; didn't 'Run code'):

  1. Insert tab character in your text by entering &#9, or (if you're sure your document is UTF-8 encoded) just hit your tab key!
  2. Add white-space: pre-wrap; to your text element's CSS rule, to stop whitespace characters (including tabs) from collapsing.
  3. Add tab-size: 4; to your text element's CSS rule, to ensure that your tab width is equal to four standard space characters.

(There were snippets and clues about each of these things sprinkled throughout the many answers and comments, but none that brought them all together to answer the question as it was worded.)

Edit: Unfortunately, the Stack Snippet converted my Unicode tab character to spaces! So that part of the demo (line 2) isn't working. You may encounter similar issues if running your code through a code formatter. &#9 avoids such issues.

Related