On this page12 sections
Lecture 026: Style Guide: Writing Clean HTML
| Category | 5: Code, Characters & Standards |
| Lecture | 026 of 048 |
| Estimated video length | 10 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Browsers forgive a lot of messy HTML, but people reading your code don't. A few simple habits make your pages easier to read, easier to fix, and less likely to break in unexpected ways.
Learning Objectives
- Follow common conventions: doctype, lowercase, quotes, closing tags
- Indent and space code so it's easy to read
- Always include
lang,charset,viewport,titleandalt - Name files consistently
1. Always Start With the Doctype and Language
<!DOCTYPE html>
<html lang="en">lang helps screen readers pronounce words and helps search engines. Use ur for Urdu, ar for Arabic, and so on.
2. Lowercase Everything
<!-- Good -->
<section class="intro">
<!-- Avoid -->
<SECTION CLASS="intro">3. Close Your Elements, Quote Your Attributes
<!-- Good -->
<p>First paragraph.</p>
<p>Second paragraph.</p>
<img src="logo.png" alt="City Bakery logo">
<!-- Works, but hard to read and easy to break -->
<p>First paragraph.
<p>Second paragraph.
<img src=logo.png alt=City Bakery logo>In the last line, the missing quotes split City Bakery logo into three separate attributes.
4. Indent Nested Elements
Indent with two spaces for each level. Don't indent <html>, <head> and <body> if
that makes lines very long.
<ul>
<li>Tea</li>
<li>Coffee
<ul>
<li>Espresso</li>
<li>Latte</li>
</ul>
</li>
</ul>Leave blank lines between large blocks, not between every line.
5. No Spaces Around =
<!-- Good -->
<a href="about.html">About</a>
<!-- Avoid -->
<a href = "about.html">About</a>6. The Must-Haves in Every Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>A Clear, Unique Title</title>
</head>
<body>
<img src="photo.jpg" alt="Describe the photo" width="400" height="300">
</body>
</html>alton every image, withwidthandheight- A
titlethat describes this page
7. File Names
- Use lowercase and hyphens:
contact-us.html,team-photo.jpg - No spaces:
my page.htmlbecomesmy%20page.htmlin links - Use the
.htmlending consistently (not a mix of.htmand.html)
Common Beginner Mistakes
1. Copying messy code from old tutorials. Uppercase tags, <font> and <center> come from the 1990s.
2. Leaving out lang and charset. Everything may look fine to you, but not to every visitor.
3. Inconsistent indentation. Pick two spaces and stick to it. Most editors can format code automatically.
Practice Questions
- Why should you add
lang="en"to the<html>tag? - Rewrite
<IMG SRC=cat.jpg>following the style guide. - How many spaces should you indent each nested level?
- Which file name is better:
About Us.HTMLorabout-us.html? - Name three things every page's
<head>should contain.
Practice Answers
- It tells browsers, screen readers and search engines which language the page is in.
<img src="cat.jpg" alt="A grey cat sleeping" width="300" height="200">- Two.
about-us.html: lowercase, hyphens, no spaces.- Any three of:
<meta charset="UTF-8">, the viewport meta tag, a<title>.
Key Takeaways
- Start with
<!DOCTYPE html>and<html lang="…">. - Lowercase tags, quoted attributes, closed elements, no spaces around
=. - Indent two spaces per level.
- Lowercase, hyphenated file names.