- Why indent your code?
- Examples of code indentation
- Inline elements
- What about the basic structure of a webpage?
- Your text editor should make it easy to indent your code
Why indent your code?
Let’s look at some examples of coding techniques to make it clear why you should indent your HTML.
Wrong method 1: Everything on one line
- Hard to figure out where code ends & text/content begins
- Hard to see where the
</p>is, even with wrapping turned on
Wrong method 2: 3 lines, but no indentation
Problem: Because everything is at left margin, hard to differentiate code & text/content
Best method: indent text/content
- Start (
<p>) & end (
</p>) are at the same level, so it’s easy to see that you’ve closed code & also see where code starts & ends
- Easy to differentiate between code & text/content
Examples of code indentation
Here are some examples of how WebSanity likes to indent our code.
Note: Notice how text/content is indented inside
<p>, and then
<p>is indented inside
<blockquote>. This is called nesting. See how easy it is to tell what’s a child of what?
Note: Again with the multiple levels of indenting—but it should be obvious how much it helps reading that code!
Note: We don’t indent
<h1>& other headers because they’re short, but if you want to indent
<h#>, that would be fine.
Note: Again, we don’t indent
<title>& most other items inside <head> because they’re short.
Note: We don’t indent
<li>because most of the time the contents are short, because we’re more concerned about the
</ul>, & because it can really lengthen the overall code. That said, if you want to indent
<li>, feel free.
You never indent inline elements. Treat them like text/content. Examples:
What about the basic structure of a webpage?
Look at the following code. You’d think that
<body> would be indented inside
<html>—after all, they’re children of
<head>, so shouldn’t they be indented?
So why aren’t
<body> indented? A couple of reasons:
- You know that
<body>are the children of
<html>; in fact, they’re always the only children of
<html>, 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, 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.
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!
Here’s how various text editors enable you to indent lines at a group:
- BBEdit: Highlight the lines & press TAB. To backdent the lines, press Shift-TAB.
- Komodo Edit: Highlight the lines & press TAB. To backdent the lines, press Shift-TAB.
- Notepad++: Highlight the lines & press TAB. To backdent the lines, press Shift-TAB. Some tips for making your HTML as clean, readable, & pretty as possible.