HTML: Indentation And Spacing | CSE 154 Unofficial Style Guide

Indentation

Spacing and indentation should be consistent throughout your code. Many developers choose to use 4-space or 2-space indentation. In HTML, each nested tag should be indented exactly once inside of its parent tag.

Here is an example of bad indentation in HTML:

<p>I am a paragraph! </p> <p>I am another paragraph! </p> <p> <h2> I am a heading inside of a paragraph! </h2> </p>

Here are some examples of good indentation in HTML:

<p>I am a paragraph!</p> <p> I am another paragraph! </p> <p> <h2> I am a heading inside of a paragraph! </h2> </p>

Place a line break after every block element. Do not place more than one block element on the same line.

<p>Hello, how are you</p> <p>I am fine</p> <p>Hello, how are you</p> <p>I am fine</p>

Nesting Tags

Close tags in the opposite order in which they were opened.

Always nest every inline element inside of a block element. For example, don't just put an <a> tag directly inside the body; instead, place it inside a p or li or other block-level element.

Tag » How Many Spaces Is An Indent