How to insert spaces/tabs in text using HTML/CSS
Asked Answered
M

20

220

Possible ways:

<pre> ... </pre>

or

style="white-space:pre"

Anything else?

Medwin answered 20/3, 2012 at 18:35 Comment(4)
White space in HTML, Tabs in HTML, Spacing Out Your HTML, and of course &nbsp;Ignace
Do you mean "tags" or "tabs"?Dianndianna
Is there an equivalent to &nbsp; but to make a tab?Savill
There's no &tab;, but I did find this little js snippet on github (basically find and replace to create five &nbsp; in a row)... gist.github.com/AustinDizzy/1231e82184bea35c42adBradshaw
D
175

To insert tab space between two words/sentences I usually use

&emsp; and &ensp;

Displode answered 4/2, 2015 at 0:56 Comment(0)
A
130

In cases wherein the width/height of the space is beyond &nbsp; I usually use:

For horizontal spacer:

<span style="display:inline-block; width: YOURWIDTH;"></span>

For vertical spacer:

<span style="display:block; height: YOURHEIGHT;"></span>
Aliphatic answered 20/3, 2012 at 20:53 Comment(1)
Note: Make sure to specify the height or width in terms of pixels, i.e. 10px.Lonee
P
68

You can use &nbsp; for spaces, &lt; for < (less than, entity number &#60;) and &gt; for > (greater than, entity number &#62;).

A complete list can be found at HTML Entities.

Portsalut answered 20/3, 2012 at 18:39 Comment(0)
B
52

Try &emsp;.

As per the documentation at Special Characters:

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 equivalent to two space characters.

Birecree answered 5/5, 2016 at 7:34 Comment(0)
A
37

Types of Spaces in HTML

Creates four spaces between the text- &emsp;

Creates two spaces between the text - &ensp;

Creates a regular space between the text - &nbsp;

creates a narrow space ( similar to regular space but slight difference - "&thinsp";

spacing between sentences - "</br>"

This link might help you. Check out [https://hea-www.harvard.edu/~fine/Tech/html-sentences.html]

Acuity answered 2/9, 2018 at 12:28 Comment(1)
&nbsp; worked fine for the browsers that I tested, new and old versions of Chrome and Firefox. &emsp; worked on some but not all.Beitris
K
25

I like to use this:

In your CSS:

.tab { 
       display:inline-block; 
       margin-left: 40px; 
}

In your HTML:

<p>Some Text <span class="tab">Tabbed Text</span></p>
Kellene answered 29/6, 2015 at 4:30 Comment(2)
This is a good answer. But I just would like to say that it would be better using CSS class instead of id (it means replacing #tab by .tab and id="tab" by class="tab") because if we use it more than once in a same document, we may have undefined behaviors. See, for instance, this question.Pelham
This helped me today. Thank you so much.Seismism
P
16
<p style="text-indent: 5em;">
The first line of this paragraph will be indented about five characters, similar to a tabbed indent.
</p>

The first line of this paragraph will be indented about five characters, similar to a tabbed indent.

See How to Use HTML and CSS to Create Tabs and Spacing for more information.

Pleura answered 14/5, 2013 at 17:33 Comment(0)
R
10

Go a step further than @Ivar and style my own custom tag like so... For me 'tab' is easier to remember and type.

tab {
    display: inline-block;
    margin-left: 40px;
}

And the HTML implementation...

<p>Left side of the whitespace<tab>Right side of the whitespace</p>

Screenshot of this code sample

And my screenshot...

Riboflavin answered 22/11, 2018 at 10:19 Comment(1)
Aye, now that's a trick worth remembering... yay to modern CSS!Banwell
L
6

<span style="padding-left:68px;"></span>

You can also use:

padding-left
padding-right
padding-top
padding-bottom
Langrage answered 23/6, 2014 at 5:35 Comment(0)
A
5

Alternatively referred to as a fixed space or hard space, non-breaking space (NBSP) is used in programming and word processing to create a space in a line that cannot be broken by word wrap.

With HTML, &nbsp; allows you to create multiple spaces that are visible on a web page and not only in the source code.

Apace answered 17/11, 2018 at 5:54 Comment(0)
G
4

Use the standard CSS tab-size. see browser compatibility

To insert a tab symbol (if standard tab key, move cursor) press Alt + 0 + 0 + 9

.element {
    -moz-tab-size: 4;
    tab-size: 4;
}

My preferred:

*{-moz-tab-size: 1; tab-size: 1;}

Look at snippet or quick found example at tab-size.

.t1{
    -moz-tab-size: 1;
    tab-size: 1;
}
.t2{
    -moz-tab-size: 2;
    tab-size: 2;
}
.t4{
    -moz-tab-size: 4;
    tab-size: 4;
}
pre {border: 1px dotted;}
<h3>tab = {default} space</h3>
<pre>
    one tab text
        two tab text
</pre>

<h3>tab = 1 space</h3>
<pre class="t1">
    one tab text
        two tab text
</pre>

<h3>tab = 2 space</h3>
<pre class="t2">
    one tab text
        two tab text
</pre>

<h3>tab = 4 space</h3>
<pre class="t4">
    one tab text
        two tab text
</pre>
Gonsalves answered 21/1, 2019 at 21:11 Comment(2)
It would be helpful for all users if you add the info about browser compability. Caniuse page says that IE11 does not support tab-size.Micturition
@Bharata, ok, I'll make the link more explicitGonsalves
C
4

You can do through padding like <span style="padding-left: 20px;">, you can check more ways here at - blank space in html

Coniology answered 10/6, 2020 at 14:32 Comment(1)
WARNING! The link is corrupted.Gobbler
P
3

If you're asking for tabs to align stuff in some lines, you can use <table>.

Putting each line in <tr> ... </tr>. And each element inside that line in <td> ... </td>. And of course you can always control the padding of each table cell to adjust the space between them.

This will make them aligned and they will look pretty nice :)

Passionate answered 25/4, 2016 at 23:58 Comment(4)
Tables should be used to represent tabular data, not for formatting. In the 90s tables where frequently used for formatting because of limitations in HTML and frustrations writing HTML with styles that worked consistently across browsers. Today it is considered to be an anti-pattern.Synchro
I see your point, It may be some old-school solution but I had his problem once and using tables worked perfect for mePassionate
Yes, but you can use table styling (with modern CSS) without messing up the semantic meaning! display: table, etc.Onomasiology
Well, tab is an abbreviation of the word tabulator, which was basically a spring-mounted lever which advanced the mechanical typewriter's carriage a fixed amount of space, dividing it in precise columns... for making tables to align precisely! :-) Aye, that's what it was used for: "tabs" were designed to make "tables". So, doing the equivalent in HTML is not quite "messing up" with semantics, but rather using older semantics for something that we have forgotten in the modern world of computers, which have replaced mechanical typrewriters but still have similar functions & names...Banwell
S
3

This worked for me:

In my CSS I have:

tab0  { position:absolute;left:25px;  }
tab1  { position:absolute;left:50px;  }
tab2  { position:absolute;left:75px;  }
tab3  { position:absolute;left:100px; }
tab4  { position:absolute;left:125px; }
tab5  { position:absolute;left:150px; }
tab6  { position:absolute;left:175px; }
tab7  { position:absolute;left:200px; }
tab8  { position:absolute;left:225px; }
tab9  { position:absolute;left:250px; }
tab10 { position:absolute;left:275px; }

Then in the HTML I just use my tabs:

Dog Food <tab3> :$30
Milk <tab3> :$3
Pizza Kit <tab3> :$5
Mt Dew <tab3> :$1.75
Superload answered 31/10, 2017 at 12:42 Comment(0)
W
1

user8657661's answer is closest to my needs (of lining things up across several lines). However, I couldn't get the example code to work as provided, but I needed to change it as follows:

<html>
    <head>
        <style>
            .tab9 {position:absolute;left:150px; }
        </style>
    </head>

    <body>
        Dog Food: <span class="tab9"> $30</span><br/>
        Milk of Magnesia:<span class="tab9"> $30</span><br/>
        Pizza Kit:<span class="tab9"> $5</span><br/>
        Mt Dew <span class="tab9"> $1.75</span><br/>
    </body>
</html>

If you need right-aligned numbers you can change left:150px to right:150px, but you'll need to alter the number based on the width of the screen (as written the numbers would be 150 pixels from the right edge of the screen).

Wacky answered 16/12, 2017 at 22:0 Comment(0)
M
1

White space? Couldn't you just use padding? That is an idea. That is how you can add some "blank area" around your element. So you can use the following CSS tags:

padding: 5px;
padding-top: 5px;
padding-bottom: 5px;
padding-left: 5px;
padding-right: 5px;
Melvinamelvyn answered 11/1, 2018 at 4:10 Comment(0)
D
1

You can use this code &#8287; to add a space in the HTML content. For tab space, use it 5 times or more.

Check an example here: https://www.w3schools.com/charsets/tryit.asp?deci=8287&ent=ThickSpace

Dayton answered 5/5, 2019 at 8:1 Comment(0)
O
0

in my case I needed to insert at the beginning of each paragraph, so I did the following and it worked for me, I hope it can help someone

p:first-letter {
    margin-left: 20px
}
Onus answered 21/3, 2022 at 21:10 Comment(0)
R
0

The &#9; character entity represents the horizontal tab space in HTML, which functions the same as pressing the tab key on your keyboard. This character is especially useful for aligning text or code in HTML documents.

This tab character will be visible within an element styled with white-space: pre; (just like any other whitespace character). You can also use <pre>&#9; text</pre> tag.

For example:

<p style="white-space: pre;">
Text aligned&#9;&#9;&#9;&#9;with a tab space.
more text&#9;&#9;&#9;&#9;aligned
</p>
Rutherfurd answered 4/3, 2024 at 17:30 Comment(0)
H
-2

Here is a "Tab" text (select, copy and paste):

It may not display correctly on the rendered version of the answer, but you can copy & paste it from the Markdown source if you wish.

Hungerford answered 15/5, 2019 at 13:14 Comment(2)
I think you need to phrase the answer more clearly. Do you mean copy and paste a space from somewhere ? i dont think that would work here.Aguste
There is a TAB in the Markdown source, but not in the HTML output here (it is instead a single space).Tailorbird

© 2022 - 2025 — McMap. All rights reserved.