HTML Space: How to Add Spaces with ,   and CSS
In HTML, a run of ordinary spaces is shown as one space. To get more room you can use a space entity such as , or, in most cases better, a CSS property such as margin, padding or white-space. This guide is for web developers and site editors. It explains how browsers treat whitespace, lists the HTML space entities with their widths, shows which CSS to use for common jobs, and explains why spaces copied from a web page cause trouble elsewhere.
How HTML treats spaces
HTML collapses whitespace. Spaces, tabs and line breaks in the source are all whitespace, and the browser shows any run of them as a single space. Whitespace at the start and end of a block is dropped.
<p>Hello world</p>
<p>Hello
world</p>
Both paragraphs display as "Hello world", with one space. Pressing the space bar several times in the source does not add room. The HTML specification counts only five characters as whitespace: space, tab, line feed, form feed and carriage return. Other space characters, including the non-breaking space, are not collapsed.
HTML space entities
| Entity | Number | Name | Width | Wraps? |
|---|---|---|---|---|
|   | No-break space | Same as a normal space | No |
  |   | En space | Half an em | Yes |
  |   | Em space | One em | Yes |
  |   | Figure space | Width of a digit | No |
  |   | Thin space | About a fifth of an em | Yes |
  |   | Hair space | Thinner than a thin space | Yes |
An em is the current font size, so in 16-pixel text one em space is about 16 pixels. The widths are nominal, and a font may vary them. The full list of Unicode spaces, including ones with no HTML name, is in our guide to Unicode space characters.
The non-breaking space
The one you will use most is . It does two things a normal space does not: it never lets a line break at that spot, and it is not collapsed, so five of them in a row give five spaces.
Good uses:
- Keeping a number and its unit together:
10 km. - Keeping a title and a name together:
Dr. Smith. - Stopping the last word of a heading from standing alone on its own line.
A poor use is layout. Strings of to push content across the page are brittle, break at other screen sizes and are hard to maintain. Our non-breaking space guide covers the character itself in more depth.
CSS options for spacing
| Goal | CSS |
|---|---|
| Gap before or after an element | margin-left, margin-right, or padding |
| Gap between items in a row or grid | gap on a flex or grid container |
| Indent the first line of a paragraph | text-indent: 2em |
| Wider gaps between words | word-spacing: 0.25em |
| Wider gaps between letters | letter-spacing |
| Show spaces and line breaks exactly as typed | white-space: pre or pre-wrap |
| Stop text from wrapping | white-space: nowrap |
The white-space property
| Value | Spaces and tabs | Line breaks in the source | Wraps at the edge? |
|---|---|---|---|
normal | Collapsed | Collapsed | Yes |
nowrap | Collapsed | Collapsed | No |
pre | Kept | Kept | No |
pre-wrap | Kept | Kept | Yes |
pre-line | Collapsed | Kept | Yes |
break-spaces | Kept, and trailing spaces take up room | Kept | Yes |
The <pre> element is the same as white-space: pre with a monospaced font, which makes it the natural home for code samples and anything where every space counts.
Everyday jobs
Indent a paragraph
p { text-indent: 2em; }
Keep a phrase on one line
<span class="nowrap">5 minutes</span>
.nowrap { white-space: nowrap; }
This leaves normal spaces in the text, which copy and paste cleanly. With , the copied text carries a non-breaking space.
Show a block of text with its spacing
<div style="white-space: pre-wrap">Line one
Line two, indented</div>
Put space between buttons
.buttons { display: flex; gap: 12px; }
Surprise gaps
Whitespace can also appear when you did not ask for it. Inline and inline-block elements written on separate lines have a line break between them in the source, which the browser turns into a space of about one character. That is the small gap you see between neat rows of buttons or images. Fixes are to lay the items out with flexbox and gap, which ignores that whitespace, or to set the parent's font-size: 0 and restore it on the children.
Why HTML spaces cause trouble outside the page
- Copied text contains U+00A0. A browser copies
as the characterU+00A0, which looks like a space and does not match one. Searches, comparisons and code can fail. - Editors add them for you. Many rich-text editors replace a second space or a leading space with
so it does not collapse. The result is text full of non-breaking spaces. - Source code is not the same as text. If you copy the HTML source, the string
arrives as six ordinary characters. That is code, not a space, and needs to be decoded or replaced.
How to clean HTML spaces
For the characters, the AI Text Cleaner has an option called Replace unusual spaces, on by default. It turns the no-break space and the other special spaces (en, em, thin, hair and others) into normal ones. For literal entities in pasted source, use a find-and-replace or a line of code:
text = text.replace(" ", " ") # entity written as text
text = text.replace("\u00a0", " ") # the actual character
If you are processing a page, a proper HTML parser decodes entities for you, which is safer than patching strings by hand.
Quick answers
How do I add a space in HTML? For one that cannot be collapsed, write . For layout, use CSS margin, padding or gap.
How do I add multiple spaces in HTML? Repeat , or use <pre> or white-space: pre-wrap to keep the spaces you typed.
What is the difference between and  ? The first is as wide as a normal space and blocks line breaks. The second is half an em wide and allows a break.
Why does my text have extra spaces after copying from a website? Check for non-breaking spaces. They look normal and are different characters.