Could we help you? Please click the banners. We are young and desperately need the money
When coding anything, high readability and clean formatting is of utmost importance. Good readability allows you and others to more quickly understand the code. Clever formatting makes it easier to extend or change it. Both are aspects that'll increase you and your team's efficiency and reduce potential frustration.
In this article, I'll show you how I format my HTML. Do keep in mind that this is a highly subjective topic.
Here's an example following these rules:
<div class="foo"></div>
Lines are individual, numbered horizontal stretches containing text. The topmost line is the first, lines that follow are placed below the prior.
By pressing "ENTER" on the keyboard, we can end and start a new line. By pressing "SHIFT" and "DELETE" (sometimes labelled "DEL"), we can remove a line.
This is a line
This is, too
And so is this
Indentation is the horizontal shift we apply to individual lines before their text begins. Since HTML is a language represented by a tree of nodes, indentation is the perfect tool to visually highlight the depth of each node.
By pressing "TAB" on the keyboard when at the start of a line, we can increase its indentation. By pressing "SHIFT" and "TAB" together, we can decrease the indentation.
No indentation
Level 1
Level 2
My base rules for lines are:
My base rules for indentation are:
Clarifications:
Here's an example showcasing all the aforementioned rules:
<div>
text
<div>
<div>
text
</div>
</div>
<div>
<div></div>
text
</div>
</div>
Formatting attributes can be a topic in itself, thus I'm giving it its own chapter here.
If a tag only includes one attribute, keep it all on one line. Otherwise, follow these rules:
Here's an example showcasing all these rules:
<div class="foo">
text
</div>
<button
id="text-button"
class="button"
>
text
</button>
Stacking attributes like this yields the following benefits: