On this page12 sections
  1. Learning Objectives
  2. 1. Always Start With the Doctype and Language
  3. 2. Lowercase Everything
  4. 3. Close Your Elements, Quote Your Attributes
  5. 4. Indent Nested Elements
  6. 5. No Spaces Around =
  7. 6. The Must-Haves in Every Page
  8. 7. File Names
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 026: Style Guide: Writing Clean HTML

Category5: Code, Characters & Standards
Lecture026 of 048
Estimated video length10 min
TargetHTML5 · any modern browser
LevelBeginner

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, title and alt
  • Name files consistently

1. Always Start With the Doctype and Language

html
<!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

html
<!-- Good -->
<section class="intro">

<!-- Avoid -->
<SECTION CLASS="intro">

3. Close Your Elements, Quote Your Attributes

html
<!-- 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.

html
<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 =

html
<!-- Good -->
<a href="about.html">About</a>

<!-- Avoid -->
<a href = "about.html">About</a>

6. The Must-Haves in Every Page

html
<!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>
  • alt on every image, with width and height
  • A title that describes this page

7. File Names

  • Use lowercase and hyphens: contact-us.html, team-photo.jpg
  • No spaces: my page.html becomes my%20page.html in links
  • Use the .html ending consistently (not a mix of .htm and .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

  1. Why should you add lang="en" to the <html> tag?
  2. Rewrite <IMG SRC=cat.jpg> following the style guide.
  3. How many spaces should you indent each nested level?
  4. Which file name is better: About Us.HTML or about-us.html?
  5. Name three things every page's <head> should contain.

Practice Answers

  1. It tells browsers, screen readers and search engines which language the page is in.
  2. <img src="cat.jpg" alt="A grey cat sleeping" width="300" height="200">
  3. Two.
  4. about-us.html: lowercase, hyphens, no spaces.
  5. 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.