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.
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.
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 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(the default) squeezes the spaces and wraps the text.-
prekeeps 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-wrapkeeps spaces and line breaks and still wraps long lines. It’s the safer pick.-
pre-linekeeps 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.
HTML does have a tab character (	 or 	), 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,
 .
Space between elements
Space around boxes, buttons, images and cards is CSS work. There are three tools, and each has its own job:
paddingis space inside an element, between its edge and its content. The background fills it.marginis space outside an element, pushing its neighbours away.-
gapis 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.
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 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.
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.
- Tags still work inside
<pre>. To show a<as text, write it as<(and>as>,&as&). 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-wraporoverflow-x: autoso 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 for each one, for example Hello 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 or white-space: pre-wrap when you want the spaces to show.
What does mean?
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  .
How do I add a tab in HTML?
HTML has a tab character (	 or 	), 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,   is as wide as the font size.
What is the difference between ,   and  ?
is as wide as a normal space and never breaks the line.   is about half the font size wide and   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 ?
A few are fine, for example to keep 10 kg together. Long rows of 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 between them, like 10 kg or 5 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 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
- HTML Entities The common space codes and 300 more characters, click to copy.
- HTML Entity Encoder Turns <, > and & into codes so they show as text.
- CSS Flexbox Generator Set gap and spacing visually and copy the CSS.
- HTML Editor Paste any example and keep building on it.