HTML Style Guide
Browsers are extremely forgiving: they will display pages with missing closing tags, uppercase element names and unquoted attributes. But "it works in my browser" is not the same as "it is good HTML". A consistent coding style makes your pages easier to read, debug, maintain and share with other people. This guide collects widely used conventions.
The rules at a glance
| Do | Avoid |
|---|---|
<!DOCTYPE html> on the first line |
Leaving it out (triggers "quirks mode") |
| Lowercase element and attribute names | <P CLASS="x"> |
Quote attribute values: class="card big" |
class=card (breaks with spaces) |
Close elements: <p>…</p> |
Relying on the browser to guess |
alt, width, height on images |
Images without alt |
lang on <html> |
Omitting the language |
| 2-space indentation for nesting | Everything on one line, or random indentation |
| Meaningful, lowercase file names | My Page FINAL(2).HTML |
Document structure
Always include the doctype, the lang attribute, the character set, the viewport and a title. Explicit <html>, <head> and <body> tags are technically optional, but writing them makes the structure obvious.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recipe: Tomato Soup</title>
</head>
<body>
…
</body>
</html>Lowercase and quotes
HTML element and attribute names are not case-sensitive, but lowercase is the universal convention and is easier to type. Always quote attribute values — unquoted values break as soon as they contain a space:
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<!-- Unquoted: the browser reads class="note" plus a separate attribute "highlight" -->
<p class=note highlight>Unquoted value (only "note" applied)</p>
<!-- Quoted: both classes apply -->
<p class="note highlight">Quoted value (both classes applied)</p>
<style>
.note { padding: 6px; border-left: 4px solid #999; }
.highlight { background: #fff59d; }
</style>
</body>
</html>Indentation and whitespace
Indent child elements by two spaces. Do not add blank lines or indentation without reason, but do separate large blocks with a blank line.
<!-- Hard to read -->
<ul><li>Bread</li><li>Milk</li>
<li>Eggs</li></ul>
<!-- Easy to read -->
<ul>
<li>Bread</li>
<li>Milk</li>
<li>Eggs</li>
</ul>Avoid spaces around the = sign: class="x", not class = "x". Keep lines reasonably short (around 80–120 characters); break long elements across lines, one attribute per line:
<img
src="img/team-photo.jpg"
alt="Our six team members at the summer picnic"
width="800"
height="450"
loading="lazy">Images
Always give images an alt text and dimensions. alt describes the image for people who cannot see it (and is shown if it fails to load); width and height let the browser reserve space so the page does not jump while loading.
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<p>Good: alt text and size reserved.</p>
<img width="120" height="60" alt="Green banner reading OK"
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='60'%3E%3Crect width='120' height='60' rx='8' fill='%2322c55e'/%3E%3Ctext x='60' y='38' font-family='sans-serif' font-size='22' fill='white' text-anchor='middle'%3EOK%3C/text%3E%3C/svg%3E">
<p>Decorative image: empty alt so screen readers skip it.</p>
<img width="60" height="10" alt=""
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='10'%3E%3Crect width='60' height='10' fill='%23ddd'/%3E%3C/svg%3E">
</body>
</html>Comments
Keep comments short and useful. Explain why, not what is obvious:
<!-- Navigation is duplicated in the footer for mobile users -->Close your elements
Some closing tags (like </p> and </li>) are optional, but omitting them makes the structure hard to follow and can surprise you. In this example the author probably meant the <div> to sit inside the paragraph, but the browser closes the paragraph first:
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<div style="border: 2px solid tomato; padding: 4px">
<p>Paragraph without a closing tag
<div style="background: #eef">This div is NOT inside the paragraph — a block element closes the open p automatically.</div>
</div>
</body>
</html>File names and extensions
- Use lowercase, hyphen-separated names:
about-us.html,hero-image.webp. - Use
.html(or.htm, but pick one) consistently. - A folder's default page is usually
index.html, soexample.com/blog/servesblog/index.html.
Tip: Run your pages through the W3C Markup Validation Service or an editor extension (e.g. HTMLHint). They catch duplicate ids, missing
altattributes and unclosed elements before your users do.
Test yourself
Why should attribute values always be quoted?
- Quotes make pages load faster
- Unquoted values break when they contain spaces
- Browsers ignore unquoted attributes
- It is required for CSS to work
Which is the recommended way to write elements and attributes?
<IMG SRC="x.png"><Img Src="x.png"><img src="x.png" alt="…"><img src = x.png>
Why add width and height to an <img>?
- They are required for the image to show
- So the browser can reserve space and avoid layout jumps
- To compress the image
- For search engines only