How to put spaces between text in html?
Categories:
Mastering Spaces in HTML: A Comprehensive Guide

Learn various techniques to add and control spaces between text elements in HTML, from basic non-breaking spaces to advanced CSS properties.
Adding spaces between text in HTML might seem straightforward, but the browser's default behavior often collapses multiple spaces into a single one. This article explores different methods to achieve desired spacing, ranging from simple character entities to powerful CSS properties, ensuring your text layout is precise and visually appealing.
Understanding HTML's Whitespace Collapsing
By default, HTML browsers treat any sequence of whitespace characters (spaces, tabs, newlines) as a single space. This behavior, while often convenient for source code readability, can be a hurdle when you need explicit multiple spaces or specific text alignment. Understanding this fundamental rule is key to effectively controlling your layout.
flowchart TD A[HTML Source Code] --> B{Browser Rendering Engine} B --> C{Whitespace Collapsing Rule} C -- Multiple Spaces --> D[Single Space Rendered] C -- Single Space --> D D --> E[Displayed HTML]
How browsers handle whitespace in HTML
Method 1: Using Non-Breaking Space (
)
The
(non-breaking space) character entity is the most common and direct way to insert multiple visible spaces in HTML. Unlike regular spaces,
prevents the browser from collapsing them and also ensures that the text around it does not break onto a new line at that point.
<p>This text has multiple spaces.</p>
<p>Another example.</p>
Using
to add multiple spaces
for layout can make your HTML cluttered and harder to maintain. Consider CSS for more complex spacing needs.Method 2: Leveraging CSS for Spacing Control
For more robust and flexible control over spacing, CSS is the preferred method. Properties like margin
, padding
, word-spacing
, and letter-spacing
offer precise control over the distance between elements, words, and individual characters.
CSS margin
and padding
/* Using margin to space elements */ .spaced-paragraph { margin-bottom: 20px; }
/* Using padding to create internal space */ .button { padding: 10px 20px; }
This paragraph has a bottom margin.
CSS word-spacing
/* Increasing space between words */ .wide-words { word-spacing: 10px; }
This text has wider word spacing.
CSS letter-spacing
/* Increasing space between letters */ .wide-letters { letter-spacing: 2px; }
This text has wider letter spacing.
CSS white-space
property
/* Preserves whitespace exactly as typed in HTML */ .preformatted { white-space: pre; font-family: monospace; }
white-space: pre;
property is particularly useful when you want to display text exactly as it's written in your HTML source, including all spaces and line breaks, similar to the <pre>
tag.Method 3: Using HTML <pre>
Tag
The <pre>
tag (preformatted text) is a semantic HTML element designed to display text exactly as it is written in the HTML source code. This means all spaces, line breaks, and tabs are preserved, and the text is typically rendered in a monospaced font. It's ideal for displaying code snippets or ASCII art.
<pre>
This is preformatted text.
It preserves all spaces
and line breaks.
</pre>
Example of text within a <pre>
tag
<pre>
preserves spaces, it's primarily for semantic purposes (preformatted text). Do not use it solely for spacing if CSS or
is more appropriate for the content's meaning.