Is two space indentation for nested elements standard practice?


#1

Question

Is two space indentation for nested elements standard practice?

Answer

For readability, it is important that we use indentation to illustrate nesting but there is no official standard on how many spaces or tabs to indent by. That having been said, 2 space indentation is common and adheres to Google’s HTML style guide.


FAQ: Learn HTML Elements - Intro to HTML - Divs
#2

Is it considered proper , or even allowed, to also include one blank line, White Space, between elements in order to make the code more readable in the event some other person may need to read and/or edit the code?


#3

If the code is not cluttered with a inline style attributes or excessive use of class and id then a single line break is perfectly acceptable, perhaps even encouraged. Too much white space can negatively affect readability.

A blank line between distinct sections is not a bad idea, though. That can be helpful to spot page segments.


#4

Now one feels the perfect fool… linebreaks vs indentation. How the heck was I blinded by that?

Two space indentation is very likely preferred over four space or tabs owing that there is so much nesting in HTML. The browser cares not what we use, so it’s strictly readability we’re concerned with.

Indentation is not decoration. We use it to convey nesting to the reader. HTML is declarative and parental. Nested elements are children at every level of nesting to which they belong. This manner of writing the HTML helps in tracking these relationships.