Back to HTML

HTML Coding Style and Formatting Conventions

Coding Tutorials Team 24 July 2026

HTML has almost no style rules. Case does not matter, indentation does not matter, and attribute order does not matter. Everything below is convention, adopted because markup is read many more times than it is written and because inconsistency makes reading slow.

Lowercase everything

Element names and attribute names are case insensitive, and the universal convention is lowercase. Mixed case is jarring to read and complicates any search across a codebase, since a search for one form misses the other. The exception is attribute values, which are frequently case sensitive, so filenames and identifiers keep whatever case they actually have.

Indent one level per nesting level

Two spaces is the most common choice, four is also fine, and tabs are defensible on the grounds that each reader can set their own width. The choice matters far less than applying it consistently, and the real benefit is that a closing tag failing to align with its opening partner is visible immediately.

Configure your editor to format on save. Manual indentation drifts on any file that has been edited more than a few times.

Quote every attribute value

Quotes may be omitted on simple values, and doing so is a false economy. An unquoted value ends at the first space, so any path or sentence containing one is silently truncated. Quote everything, use double quotes for consistency with almost all existing code, and be careful that your editor is producing straight quotes rather than the curly ones a word processor would insert.

Keep attribute order predictable

Order carries no meaning to the parser and a consistent order makes elements scannable. A common sequence puts identity first, then behaviour, then presentation.

<a id="home-link" href="/" class="nav-link" data-section="home">Home</a>

The value here is that when you scan a page of markup, the same information is always in the same position on every element, so your eye stops hunting for it.

Break long lines sensibly

An element with six attributes does not read well on one line. Breaking it with one attribute per line, indented, keeps it legible.

<input type="email" id="email" name="email" class="form-input" placeholder="you@example.com" required>

Take care doing this with inline elements. A line break inside your source becomes a rendered space, so breaking the line between an anchor and adjacent text will change the visible spacing, which is occasionally the cause of an unexpected gap.

Name classes for what things are

A class named after appearance becomes wrong the moment the design changes, and a codebase full of classes named for colours and sizes is one that nobody dares restyle. Naming after purpose survives redesign.

Established naming systems exist and are worth considering on a larger project. The block, element and modifier convention uses a structured pattern of double underscores and double hyphens to encode relationships in the name itself, which makes it obvious from a class alone where an element belongs. Whether you adopt it or invent something simpler matters less than writing down whichever you chose so the next person follows it.

Comment intent, not structure

A comment restating what a tag already says is noise. A comment explaining why something unusual exists is valuable, because it prevents a well meaning cleanup from reintroducing the problem the oddity was solving. Marking the end of long nested sections is the one structural comment that genuinely helps.

Do not fight the tooling

An automatic formatter will settle most of these arguments for you and remove them from code review entirely. It applies one style everywhere, it runs on save, and it means nobody spends time on indentation in a discussion that should be about the markup itself. The specific style it enforces is far less important than having it enforced at all.

Leave out what does nothing

Several habits survive from older versions of HTML and add characters without adding meaning. The type attribute on a script or a stylesheet link is assumed correctly by every browser. The trailing slash on void elements is ignored. Language attributes on script tags were deprecated many years ago. Removing these makes the remaining markup easier to scan, because everything left on an element is there for a reason.

Consistency is the actual rule

Every convention above has a defensible alternative, and the projects that are pleasant to work on are not the ones that picked correctly. They are the ones where every file looks like every other file, so a developer opening an unfamiliar page already knows how to read it. If you join an existing codebase, match what is there even where you would have chosen differently. A consistent style you mildly dislike is worth more than a better one applied to half the files.