Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a space in HTML

Understanding the Basics

Before we delve into the practical aspect of adding spaces in HTML, we need to understand the basics of how HTML deals with whitespace - spaces, tabs, and newlines.

In HTML, whitespace refers to the characters that create space in your web page. These can be spaces, tabs or newlines. However, HTML treats multiple spaces as one. So, if you type more than one space in your HTML code, the browser will interpret it as a single space.

This is similar to how we treat words in a sentence. When we talk or write, we leave a single space between words. Even if we leave more than one space, it doesn't change the meaning of the sentence. HTML functions in the same way.

Adding a Single Space

Adding a single space in HTML is as straightforward as it sounds. All you need to do is hit the space bar once. For example:

<p>Hello World!</p>

In the code above, there is a space between 'Hello' and 'World!'. When this HTML snippet is rendered in the browser, you'll see 'Hello World!' displayed as a paragraph, with a space between the two words.

Adding Multiple Spaces

As we've mentioned, typing multiple spaces in your HTML code won't work because the browser collapses them into a single space. If you've ever tried to align text or create extra spaces using the space bar, you'll understand how frustrating this can be.

However, there's a special character in HTML for adding extra spaces: &nbsp; - which stands for non-breaking space. This is an entity in HTML, which is a type of code that represents a character. The &nbsp; entity represents a space.

Think of it as a secret handshake or a coded message. When the browser sees &nbsp;, it understands that you're asking for an additional space.

For example, if you want to add two spaces between 'Hello' and 'World!', you can do:

<p>Hello&nbsp;&nbsp;World!</p>

When this code is rendered in a browser, it will display 'Hello  World!' with two spaces between the words.

Using CSS for Spaces

While &nbsp; can be useful, it's not the best way to add spaces in your HTML document. It can make your code messy and hard to read. Plus, it's not the most efficient way to control the layout and spacing of your web page.

Instead, many web developers use CSS (Cascading Style Sheets) to control the spaces between elements on their web page. CSS is a language used to style HTML documents.

Imagine you're building a house. HTML is like the bricks and the structure of the house, while CSS is like the paint and decorations.

In CSS, you can use properties like margin, padding, border, and position to control the spaces in your HTML document.

For example, if you want to add a space between two paragraphs, you can use the margin property in CSS:

<p style="margin-bottom: 20px;">Hello World!</p>
<p>How are you?</p>

In the code above, margin-bottom: 20px; adds a space of 20 pixels below the first paragraph.

Conclusion

While HTML collapses multiple spaces into one, there are ways around it. We can use the &nbsp; entity to add extra spaces, or better yet, use CSS to control the layout and spacing of our web page. With these tools, you can ensure that your web page looks exactly the way you want it to.

Remember, the key to mastering HTML and CSS is practice. So, don't be afraid to experiment with different properties and values. Before you know it, you'll be creating stunning web pages with perfect spaces. Happy coding!