Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to indent text in HTML

Understanding Indentation in HTML

Why is Indentation Important?

Before we dive into the technicalities of indentation in HTML, let's first understand its significance. Imagine reading a book without any paragraphs, spaces or line breaks. It would be a nightmare, right? Similarly, in the world of programming and HTML, indentation helps in organizing code, making it easier to read and debug.

What is Indentation?

Indentation is the space at the beginning of a line to help visually distinguish groups of code. It's similar to how we use paragraphs and line breaks in English to separate thoughts and ideas.

Indenting in HTML

HTML, or HyperText Markup Language, doesn't pay much attention to white spaces. This means that no matter how much space or how many line breaks you add, the browser will ignore it. But for the sake of code readability, we need to manually indent our HTML code.

The Basic Principle of Indentation

The basic principle of indentation in HTML is that every nested tag should be indented one level more than its parent tag. A "nested" tag means a tag that is inside another tag. "Parent" and "child" are terms we borrow from family trees where a parent tag has one or more child tags inside it.

For instance, consider this HTML code:

<p>Hello, world!</p>

Here, <div> is the parent tag and <p> is the nested or child tag. The child tag <p> is indented one level more than its parent <div>.

Using Spaces or Tabs?

Now, you may question: Should we use spaces or tabs for indentation? The answer is: it's personal preference. Some developers prefer spaces, while others prefer tabs. However, it's important to be consistent. If you choose spaces, stick with them throughout your project. If you choose tabs, use them consistently. Mixing both can lead to confusing and messy code.

Indenting Complex HTML Structures

Indentation becomes crucial when we're dealing with complex HTML structures. For example:

    <p>Hello, world!</p>
        <li>Item 1</li>
        <li>Item 2</li>

In this code, we have several nested tags: <p>, <ul>, and <li>. Each nested tag is indented one level more than its parent. This makes the structure of the code clear at a glance.

Using HTML Pre or Code Tags

But what if you want the browser to display indented text? In that case, you can use the <pre> or <code> tags. These tags tell the browser to respect white spaces and line breaks.

For example:

    This is an example
    of indented text.

This will display the text exactly as it is written in the HTML code, with all indents and line breaks.

Indentation Tools and Editors

Most code editors, like Sublime Text, VS Code, or Atom, come with features that auto-indent your code. This can save a lot of time and ensure consistency. You can usually find this feature in the menu bar of your editor, under "Edit" > "Auto Indent". Additionally, there are online tools such as DirtyMarkup that will format and indent your code for you.


Although HTML doesn't technically require indentation, it's a best practice that makes your code more readable and maintainable. So, next time you write HTML, remember to indent your code properly. It might seem like a small thing, but it can make a big difference in your coding journey. Happy coding!