HTML indent 2 or 4 spaces

Note: Notice how text/content is indented inside

<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
5, and then
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
5 is indented inside
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
9. This is called nesting. See how easy it is to tell what's a child of what?

<table> 
    <tr> 
        <td> 
            Lorem ipsum dolor sit amet 
        </td> 
        <td> 
            Consectetur adipisicing 
        </td> 
    </tr> 
    <tr> 
        <td> 
            Lorem ipsum dolor sit amet 
        </td> 
        <td> 
            Consectetur adipisicing 
        </td> 
    </tr> 
</table>

Note: Again with the multiple levels of indenting—but it should be obvious how much it helps reading that code!

Exceptions

<h1>Title of the page</h1>

Note: We don't indent

<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
0 & other headers because they're short, but if you want to indent
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
1, that would be fine.

<head>
    …
    <title>Document title</title>
</head>

Note: Again, we don't indent

<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
2 & most other items inside <head> because they're short.

<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
0

Note: We don't indent

<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
3 because most of the time the contents are short, because we're more concerned about the
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
4 &
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
5, & because it can really lengthen the overall code. That said, if you want to indent
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
3, feel free.

Inline elements

You never indent inline elements. Treat them like text/content. Examples:

<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
1
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
2

What about the basic structure of a webpage?

Look at the following code. You'd think that

<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
7 &
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
8 would be indented inside
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
9—after all, they're children of
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
7, so shouldn't they be indented?

<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
3

So why aren't

<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
7 &
<p> 
<a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
</p>
8 indented? A couple of reasons:

  • You know that
    <p> 
    <a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
    </p>
    7 &
    <p> 
    <a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
    </p>
    8 are the children of
    <p> 
    <a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
    </p>
    9; in fact, they're always the only children of
    <p> 
    <a href="http://en.wikipedia.org/wiki/H._P._Lovecraft">H. P. Lovecraft</a> 
    </p>
    9, so there's no need to remind yourself of this via nesting
  • You save yourself one level on indentation; when you start nesting HTML inside HTML inside HTML inside HTML, this can add up

Your text editor should make it easy to indent your code

You could press tab or the space bar every single time you want to indent your code, but you shouldn't have to do that. Fortunately, good text editors help you out when it comes to indenting.

Auto-indent

BBEdit, for instance, has a setting in its Preferences for "Auto-indent", which it defines as follows: "When this option is selected, pressing the Return key in new windows automatically inserts spaces or tabs to indent the new line to the same level as the previous line."

Any good text editor should have a setting like this somewhere. The trick is finding it in the editor's Preferences or Options.

Spaces or tabs? (Spaces!)

Should you use spaces or tabs for indenting? A debate has raged on this topic for decades, with different developers insisting that theirs is the right choice. WebSanity uses spaces, for various reasons, so that is the right choice, clearly.

Your text editor should let you choose what gets inserted when you press the TAB key: a tab or spaces. Different text editors call that setting different things. Here are a few examples:

  • BBEdit: Preferences > Editor Defaults > Auto-expand tabs
  • Komodo Edit: Options/Preferences > Editor > Indentation > Prefer Tab characters over spaces
  • Notepad++: Preferences > Language Menu/Tab Settings > Replace by space

Note: If anyone knows of any others that I should add, let me know.

How many spaces?

The next question is, how many spaces get inserted when you press TAB? Basically, people either choose 2, 4, or 8 spaces. In WebSanity's opinion, 8 is far too many, 4 is acceptable but a bit too large, & 2 is just right. With 2, you can see the indentation, but lots of nesting doesn't push your code out to ridiculous lengths, as you can see in the following:

Your text editor should let you choose how many spaces get inserted when you press TAB. Different text editors set that in different ways. Here are some examples:

  • BBEdit: Preferences > Editor Defaults > Tab width
  • Komodo Edit: Options/Preferences > Editor > Indentation > Number of spaces per indent
  • Notepad++: Preferences > Language Menu/Tab Settings > Tab size

Note: Again, if anyone knows of any others that I should add, let me know.

Indenting several lines at once

What if you want to indent more than one line of code? Like 5? Or 25? You could manually move the cursor to the start of each line and press TAB the requisite number of times, but that would quickly grow tedious.

Fortunately, every good text editor lets you select all the lines you wish to indent and then indent them all as a group. This is a fantastic time saver!

Should I indent 2 or 4 spaces?

My preference as a software developer is to use 2-space indentation in front-end projects and 4-space indentation in backend projects. However, using 4-space indentation gives more margin than required. On the flip side, 2-space indentation makes for inadequate spacing and may reduce code readability in some cases.

How many spaces do I indent in HTML?

Indent by two spaces per indentation level. Use all-lowercase for elements and attributes. Don't leave trailing spaces at the end of a line (except as needed for Markdown).

Do I indent 4 spaces?

For example if you start off using four spaces for an indent, then you should always use four spaces. In the example below, four spaces have been used for the first indent, but only two for the second, and you can see that as a result the code doesn't “line up”.

Should I use 4 spaces or tab?

So, at the end of the day, tabs versus spaces is truly a matter of preference, however the tab is still the character specifically designed for indentation, and using one tab character per indentation level instead of 2 or 4 spaces will use less disk space / memory / compiler resources and the like.