HTML whitespace refers to spaces, tabs, and line breaks added between HTML elements or text. Whitespace is often used to make HTML code easier for developers to read and organize.
For example, these two pieces of HTML are written differently:
<p>Hello World</p>
<p>
Hello
World
</p>
In normal HTML rendering, both usually display:
Hello World
The browser generally collapses consecutive whitespace characters into a single space when displaying normal text.
Understanding how HTML whitespace works helps you write clean HTML without expecting extra spaces or line breaks in the browser.
If you are learning HTML fundamentals, it is also useful to understand HTML Syntax and HTML Elements.
What Is Whitespace in HTML?
Whitespace includes characters such as:
- Spaces
- Tabs
- Line breaks
- Carriage returns
For example:
<p>Hello World</p>
There is one space between Hello and World.
You can also write:
<p>Hello World</p>
Even though there are several spaces in the HTML source code, the browser normally renders them as a single space:
Hello World
Note: HTML source code and the rendered webpage do not always display whitespace in the same way.
Spaces Between HTML Elements
Whitespace can be used between HTML elements to make the source code easier to read.
For example:
<div>
<h1>HTML Tutorial</h1>
<p>Learn HTML step by step.</p>
</div>
The indentation and blank lines improve the readability of the source code.
The browser does not normally display the indentation as visible empty space around the elements.
Multiple Spaces in HTML
Consider this example:
<p>HTML CSS JavaScript</p>
You might expect the browser to display all the spaces.
However, normal HTML rendering usually collapses consecutive spaces:
HTML CSS JavaScript
This behavior is known as whitespace collapsing.
Whitespace Collapsing
In normal HTML text, multiple whitespace characters are generally collapsed into a single space.
For example:
<p>
HTML
CSS
JavaScript
</p>
The browser normally displays:
HTML CSS JavaScript
The line breaks and indentation in the source code do not automatically create separate lines in the rendered text.
Whitespace Between Inline Elements
Whitespace can become more noticeable when working with inline elements.
Consider:
<a href="#">HTML</a>
<a href="#">CSS</a>
<a href="#">JavaScript</a>
Depending on the layout and CSS, these links can appear close together because there is no whitespace between the elements in the source.
Adding whitespace:
<a href="#">HTML</a>
<a href="#">CSS</a>
<a href="#">JavaScript</a>
does not necessarily give you reliable visual spacing for every layout situation. CSS is generally a better choice for controlling layout spacing.
For example:
a {
margin-right: 10px;
}
Important: Use CSS for layout and spacing instead of depending on random spaces in HTML source code.
Whitespace and Text
Whitespace inside normal text is generally collapsed.
For example:
<p>
Welcome
to
Web4Learners.
</p>
The browser normally renders:
Welcome to Web4Learners.
The line breaks in the HTML source do not force each word onto a separate line.
Whitespace at the Beginning and End
Whitespace at the beginning or end of normal text is generally collapsed or ignored depending on its position.
For example:
<p>
Hello World
</p>
The browser does not normally display additional empty space before Hello or after World.
The HTML source can therefore be formatted for readability without changing the visible result.
HTML Indentation
Indentation is one of the most useful ways to organize HTML code.
For example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Whitespace</title>
</head>
<body>
<main>
<h1>HTML Whitespace</h1>
<p>
Learn HTML
with Web4Learners.
</p>
</main>
</body>
</html>
The indentation makes the parent-child relationships easier to understand.
It also makes nested HTML easier to maintain.
You can learn more about nesting in Nested HTML Elements.
Spaces in HTML Attributes
Whitespace is also used between attributes.
For example:
<a href="https://example.com" target="_blank">
Visit Website
</a>
There is whitespace between:
href="..."
target="_blank"
This separates the attributes so the browser can identify them correctly.
Without proper separation, the HTML syntax can become invalid.
Learn more about how attributes work in HTML Attributes.
Whitespace Around Tags
Whitespace can appear between opening and closing tags.
For example:
<p>
Hello World
</p>
This is valid HTML.
You can also write:
<p>Hello World</p>
Both are commonly rendered as:
Hello World
The difference is mainly in source-code formatting.
Whitespace and <pre>
The <pre> element behaves differently from normal HTML text.
Whitespace inside <pre> is preserved.
For example:
<pre>
HTML
CSS
JavaScript
</pre>
The browser preserves the spaces and line breaks inside the <pre> element.
The result keeps the formatting from the source code.
This makes <pre> useful for displaying preformatted text, such as code, poems, or text where spacing matters.
Whitespace and <code>
The <code> element is commonly used to identify programming code.
For example:
<p>Use <code>display: block;</code> in CSS.</p>
The <code> element itself does not mean that all surrounding whitespace will automatically be preserved.
If you need to preserve line breaks and indentation, <pre> is commonly used together with <code>:
<pre><code>
function hello() {
console.log("Hello");
}
</code></pre>
This combination is commonly used for displaying formatted source code.
Using Non-Breaking Spaces
HTML provides a special character reference called for a non-breaking space.
Example:
<p>Hello World</p>
This creates non-breaking spaces between the words.
Unlike normal whitespace, is treated differently by the browser and can prevent a line break at that position.
However, it should not normally be used to create page layouts or large amounts of spacing.
For layout spacing, CSS is the better approach.
Whitespace vs
Normal spaces and non-breaking spaces behave differently.
| Feature | Normal Space | |
|---|---|---|
| Multiple spaces preserved normally | No | Yes, each entity represents a space |
| Can prevent line break | No | Yes |
| Common use | Normal text | Special spacing or keeping words together |
| Recommended for layout | No | No |
| CSS alternative | Yes | Yes |
For example:
<p>Web4Learners HTML Tutorial</p>
For keeping two words together:
<p>Web4Learners HTML</p>
Use only when you specifically need its non-breaking behavior.
Whitespace and Line Breaks
Adding a line break in the HTML source does not automatically create a visible line break.
For example:
<p>
HTML
CSS
</p>
Normally displays:
HTML CSS
If you want an explicit line break, you can use the <br> element:
<p>
HTML<br>
CSS
</p>
This displays:
HTML
CSS
However, <br> should be used for meaningful line breaks, not as a general layout tool.
Whitespace in Comments
Whitespace can also appear inside HTML comments.
For example:
<!--
This is an HTML comment.
It can contain multiple lines.
-->
The whitespace inside the comment is not displayed as webpage content.
You can learn more about comments in HTML Comments.
Whitespace and CSS
CSS gives you much more control over how whitespace behaves.
For example:
p {
white-space: pre;
}
The white-space property controls how whitespace inside an element is handled.
Some useful values include:
white-space: normal;
white-space: nowrap;
white-space: pre;
white-space: pre-wrap;
white-space: pre-line;
For example:
.code {
white-space: pre;
}
This can preserve whitespace and line breaks in the selected element.
white-space: nowrap
The nowrap value prevents normal text wrapping.
Example:
.title {
white-space: nowrap;
}
The text attempts to remain on one line.
This can be useful in specific interface situations, but it should be used carefully on responsive websites because long text may overflow smaller screens.
white-space: pre-wrap
pre-wrap preserves whitespace while still allowing text to wrap when necessary.
Example:
.code {
white-space: pre-wrap;
}
This can be useful when displaying formatted text that should remain readable on smaller screens.
Whitespace and HTML Formatting
Good formatting is important when writing HTML.
Compare this:
<div><h1>HTML</h1><p>Learn HTML.</p></div>
with:
<div>
<h1>HTML</h1>
<p>Learn HTML.</p>
</div>
Both can produce the same basic content, but the second version is much easier to read and maintain.
Best Practice: Use consistent indentation and line breaks in your HTML source code.
Does Whitespace Affect HTML Structure?
Whitespace normally does not create new HTML elements or change the basic element hierarchy.
For example:
<div>
<p>Hello</p>
</div>
The whitespace does not create another element between <div> and <p>.
The actual HTML structure comes from the tags and their relationships.
Understanding Opening and Closing Tags can help you understand how these relationships are created.
Common Mistakes
Expecting Multiple Spaces to Appear
This:
<p>Hello World</p>
normally displays:
Hello World
Do not use repeated normal spaces when you need precise visual spacing.
Using Spaces for Layout
Avoid:
<p>HTML CSS JavaScript</p>
Use CSS instead:
.item {
margin-right: 20px;
}
Using Too Many Entities
Although can create non-breaking spaces, using many of them for layout makes HTML difficult to maintain.
Prefer CSS for layout and spacing.
Assuming Source Line Breaks Create Visible Line Breaks
This:
<p>
Hello
World
</p>
does not normally display the words on separate lines.
Use <br> when an actual line break is semantically appropriate:
<p>
Hello<br>
World
</p>
Ignoring Indentation
Poorly formatted HTML can become difficult to understand, especially when many elements are nested.
Use consistent indentation to make the document structure clear.
Best Practices
- Use whitespace to make HTML source code readable.
- Use consistent indentation.
- Do not depend on multiple spaces for page layout.
- Use CSS for spacing and positioning.
- Use
<br>only when a meaningful line break is required. - Use
only when a non-breaking space is actually needed. - Use
<pre>when whitespace and line breaks need to be preserved. - Use the CSS
white-spaceproperty when you need more control over whitespace rendering. - Keep nested HTML properly formatted and easy to read.
- Remember that source-code whitespace and rendered whitespace are not always the same.
FAQs
What is whitespace in HTML?
Whitespace includes spaces, tabs, and line breaks used between HTML elements or within text.
Does HTML ignore whitespace?
HTML normally collapses consecutive whitespace characters in regular text into a single space.
Does pressing Enter create a new line in HTML?
Not normally. A line break in the HTML source code does not automatically create a visible line break. Use <br> when an explicit line break is required.
How do I add multiple spaces in HTML?
You can use for non-breaking spaces, but CSS should normally be used for visual spacing and layout.
What is in HTML?
is a character reference representing a non-breaking space. It can prevent a line break between content at that position.
How can I preserve whitespace in HTML?
You can use the <pre> element or control whitespace with CSS using the white-space property.
Does indentation matter in HTML?
Indentation generally does not change how normal HTML is rendered, but it makes the source code much easier to read, understand, and maintain.
What is the difference between <pre> and normal HTML text?
Normal HTML text generally collapses consecutive whitespace, while <pre> preserves spaces and line breaks from the source.
Should I use spaces or CSS for layout?
Use CSS for layout, spacing, margins, padding, and positioning. Do not use repeated spaces or as a general layout technique.
Does whitespace affect HTML elements?
Whitespace normally does not create elements or change the basic HTML hierarchy, but whitespace can affect how text and certain inline content are rendered.