Home / Guides

HTML Space: How to Add Spaces with  ,   and CSS

Published 2026-11-02

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

EntityNumberNameWidthWraps?
&nbsp;&#160;No-break spaceSame as a normal spaceNo
&ensp;&#8194;En spaceHalf an emYes
&emsp;&#8195;Em spaceOne emYes
&numsp;&#8199;Figure spaceWidth of a digitNo
&thinsp;&#8201;Thin spaceAbout a fifth of an emYes
&hairsp;&#8202;Hair spaceThinner than a thin spaceYes

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 &nbsp;. 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:

A poor use is layout. Strings of &nbsp; 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

GoalCSS
Gap before or after an elementmargin-left, margin-right, or padding
Gap between items in a row or gridgap on a flex or grid container
Indent the first line of a paragraphtext-indent: 2em
Wider gaps between wordsword-spacing: 0.25em
Wider gaps between lettersletter-spacing
Show spaces and line breaks exactly as typedwhite-space: pre or pre-wrap
Stop text from wrappingwhite-space: nowrap

The white-space property

ValueSpaces and tabsLine breaks in the sourceWraps at the edge?
normalCollapsedCollapsedYes
nowrapCollapsedCollapsedNo
preKeptKeptNo
pre-wrapKeptKeptYes
pre-lineCollapsedKeptYes
break-spacesKept, and trailing spaces take up roomKeptYes

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 &nbsp;, 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

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("&nbsp;", " ")     # 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 &nbsp;. For layout, use CSS margin, padding or gap.

How do I add multiple spaces in HTML? Repeat &nbsp;, or use <pre> or white-space: pre-wrap to keep the spaces you typed.

What is the difference between &nbsp; and &ensp;? 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.

Copied text from a web page? Paste it into the cleaner to turn non-breaking and other special spaces into normal ones. The cleaner works on characters, so a literal &nbsp; in pasted source code is not converted. Open the AI Text Cleaner

Related guides