Skip to content

How to Add Spaces in HTML

Short answer: HTML squeezes every run of spaces into one. For an extra space or two, type   (a non-breaking space). To keep all the spaces you typed, use the CSS white-space: pre-wrap. For space between boxes, buttons or images, use CSS margin, padding or gap, not a row of  . Every example below is live: edit the code and the preview changes.

The quick answer

The same five spaces, written three ways. Only the first one gets squeezed. Next to it, the right way to put space between two buttons.

Spaces,   and pre-wrap
Space between elements: gap

The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. To keep one, copy it into the HTML Editor.

Why HTML squeezes your spaces

HTML treats spaces, tabs and line breaks in your code as “white space”, and it’s there so you can lay out the code neatly. When the browser draws the text, it follows three rules:

  • A run of white space becomes one space. Ten spaces, a tab and a line break all end up as a single space.
  • Line breaks count as spaces. Pressing Enter in your code doesn’t start a new line on the page.
  • White space at the start and end of a line is dropped. The indent in your code doesn’t show up.
Messy spacing in the code, tidy on the page

That’s a good thing most of the time: you can indent your HTML however you like. When you do want the spaces to show, you have two choices: special space characters, or CSS. Both are below.

  and the other space codes

  is a non-breaking space. It looks like a normal space, but the browser doesn’t squeeze it and won’t start a new line there. That’s why a few   in a row show as a few spaces. HTML has wider and thinner spaces too:

Name Code Number Width Can break a line?
Non-breaking space     Same as a normal space No
En space     Half the font size Yes
Em space     The font size (16px text: 16px) Yes
Thin space     About a fifth of the font size Yes
Hair space     Thinner still Yes
Narrow no-break space no name   About a thin space No
Zero-width space ​ ​ Nothing Yes (a hidden break point)
The width of each space (green)

The exact widths depend on the font, so treat them as a guide. Only   and the narrow no-break space keep words together; the others can end a line like a normal space. You’ll find the common ones, ready to copy, on the HTML Entities page.

💡 A few   are fine. If you find yourself typing eight of them to line things up, that’s a job for CSS (padding, margin or a table), which stays lined up on every screen size.

Keep every space with white-space

When the text has lots of spaces you want to keep, like a poem, a column of numbers or text pasted from a form, set the CSS white-space property on the element instead of typing  :

normal, pre and pre-wrap in a narrow box
  • normal (the default) squeezes the spaces and wraps the text.
  • pre keeps spaces and line breaks exactly, but never wraps, so long lines stick out of the box. On a phone that can make the page scroll sideways.
  • pre-wrap keeps spaces and line breaks and still wraps long lines. It’s the safer pick.
  • pre-line keeps line breaks but squeezes spaces. See the line break tutorial for more.

Indent text

To indent the first line of a paragraph, like in a book, use text-indent. To move a whole block in, use padding-left (space inside the element) or margin-left (space outside it). Both beat a row of  : the indent stays the same everywhere and you change it in one place.

text-indent and padding-left

HTML does have a tab character (&Tab; or &#9;), but in normal text a tab is squeezed like any other space. It only shows where white-space keeps spaces (pre, pre-wrap, or inside <pre>), and tab-size sets its width. For indents in normal text, use text-indent, padding or, for one wide gap in a line, &emsp;.

Space between elements

Space around boxes, buttons, images and cards is CSS work. There are three tools, and each has its own job:

  • padding is space inside an element, between its edge and its content. The background fills it.
  • margin is space outside an element, pushing its neighbours away.
  • gap is space between the children of a flex or grid container. It never adds space before the first item or after the last one, which is usually what you want.
padding, margin and gap
The mystery gap between inline-blocks

About that mystery gap: when elements sit side by side as inline-block, the line break between their tags in your code is white space, so the browser draws a space between them. Make the parent a flexbox (or grid) and the gap goes away (here gap: 0, so the tags touch). Then set the exact space you want with gap. Want to try layouts like this by clicking? The CSS Flexbox Generator shows the code as you go.

Keep “10 kg” on one line

A number on one line and its unit on the next is hard to read. Put a &nbsp; between them and the browser keeps them together. It helps with “10 kg”, “5 pm”, “Page 4”, “Mr. Smith” and prices like “20 €”. For a longer phrase, like a phone number, wrap it in a span with white-space: nowrap.

A normal space, &nbsp; and nowrap in a narrow box

Don’t overdo it: text that can’t break anywhere sticks out of narrow boxes. Keep it to the small pieces that belong together.

Spaces in a <pre> block

<pre> means “preformatted”. Everything inside it shows exactly as typed: spaces, tabs and line breaks, in a monospace font where every letter is the same width. It’s made for code, plain text tables and ASCII art.

A plain text table in <pre>
Tabs in <pre>, with tab-size
  • Tags still work inside <pre>. To show a < as text, write it as &lt; (and > as &gt;, & as &amp;). The HTML Entity Encoder does it for a whole block at once.
  • One line break right after <pre> is ignored, so you can start the text on the next line in your code.
  • Long lines don’t wrap. Add white-space: pre-wrap or overflow-x: auto so it works on phones.

Questions people ask

How do I add a space in HTML?

A normal space works for one space. For extra spaces, type &nbsp; for each one, for example Hello&nbsp;&nbsp;&nbsp;there. To keep every space you typed, put the text in an element with the CSS white-space: pre-wrap. For space between elements, use CSS margin, padding or gap.

Why does HTML ignore my extra spaces?

Browsers squeeze every run of spaces, tabs and line breaks in your code into a single space, and drop the spaces at the start and end of a line. That lets you indent your HTML neatly without it showing on the page. Use &nbsp; or white-space: pre-wrap when you want the spaces to show.

What does &nbsp; mean?

&nbsp; is a non-breaking space. It looks like a normal space, but the browser doesn’t squeeze several of them into one, and it never starts a new line there. Its number code is &#160;.

How do I add a tab in HTML?

HTML has a tab character (&Tab; or &#9;), but in normal text a tab is squeezed like any space. It only shows where white-space keeps spaces (pre, pre-wrap, or inside <pre>), and tab-size sets its width. To indent the first line of a paragraph, use text-indent: 2em. To move a whole block in, use padding-left or margin-left. For one wide gap inside a line, &emsp; is as wide as the font size.

What is the difference between &nbsp;, &ensp; and &emsp;?

&nbsp; is as wide as a normal space and never breaks the line. &ensp; is about half the font size wide and &emsp; is about the font size wide, and both can break a line like a normal space. The exact width depends on the font.

Is it bad to use lots of &nbsp;?

A few are fine, for example to keep 10&nbsp;kg together. Long rows of &nbsp; to line things up or to push text along break easily on other screen sizes and fonts. Use CSS padding, margin, gap, text-indent or a table for layout instead.

How do I keep a number and its unit on the same line?

Put &nbsp; between them, like 10&nbsp;kg or 5&nbsp;pm. The browser won’t break the line there. For a longer phrase such as a phone number, wrap it in a span with white-space: nowrap.

How do I add space between two buttons or images?

Put them in a flex container and set gap, for example display: flex; gap: 16px;. Or give the first one a margin-right. Avoid &nbsp; for this: the gap would change with the font size and you couldn’t change it in one place.

Why is there a small gap between my inline-block elements?

The line break between the tags in your code counts as a space, so the browser draws one between inline-block elements. Make the parent display: flex (or grid), which ignores that white space, and set the space you want with gap.

Keep going

Tools that help