Lesson 50 of 51 · Module 7, lesson 10 of 11 · 4 min

Clean, valid HTML

Follow a short style guide and check your pages with the W3C validator.

What you'll learn

  • A short style guide for clean HTML
  • How to check your page with the W3C validator
  • How to read an error and fix it

Why this matters

Browsers are forgiving: they display pages full of mistakes, and quietly repair them. But the repairs can differ between browsers, screen readers may read a repaired page wrongly, and CSS and JavaScript can break in strange ways. A validator finds these mistakes in seconds. Professional developers check their HTML before it goes live.

A short style guide

These are the habits you have used throughout this course:

  • <!DOCTYPE html> first; <html lang="…">; <meta charset="UTF-8"> first in the head; the viewport line; a unique <title>.
  • Lower-case element and attribute names; values in double quotes.
  • Close every element that needs closing, in the right order.
  • Indent nested elements by two spaces.
  • One <h1>; heading levels without gaps.
  • Every image has alt; every form field has a <label>; every iframe has a title.
  • Relative paths, lower case, no spaces, no leading slash.
  • Meaningful elements first; <div> and <span> only when nothing else fits.

Tidy your code automatically

VS Code can fix the indentation for you. Right-click in the code and choose Format Document, or press Shift + Alt + F (Windows) / Shift + Option + F (macOS).

The W3C validator

The W3C (World Wide Web Consortium) is the organisation that writes web standards. Its free validator checks your HTML against them.

  1. Go to validator.w3.org/nu/.
  2. Choose by file upload and pick your index.html — or choose by text input and paste your code.
  3. Press Check.

The W3C validator page, set to "by file upload". Before fixing, it shows an error: "An img element must have an alt attribute, except under certain conditions", from line 12, with the img code highlighted. After adding alt, it shows "Document checking completed. No errors or warnings to show." Below: read the message, go to the line number in VS Code, and fix the first error first, then check again, because one mistake often causes several messages.

Reading the results

  • An Error must be fixed.
  • A Warning is advice. Read it, and decide.
  • Each message gives a line number. In VS Code, the line numbers are on the left; press Ctrl + G (both systems) to go to a line.
  • Fix the first error first, then check again. One missing closing tag can cause many messages further down.

Try it yourself

This page has three mistakes. Select it and choose Try it — notice that the preview still looks almost right. Then paste the code into the validator's by text input box and fix every error.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Salad</title>
</head>
<body>
  <h1>Green salad</h1>
  <img src="images/salad.jpg" width="300" height="200">
  <p>Ready in <strong>ten minutes</p></strong>
  <ul>
    <p>Lettuce</p>
    <li>Tomatoes</li>
  </ul>
</body>
</html>

The invalid salad page in a browser: the heading "Green salad", a picture of a bowl of salad, the sentence "Ready in ten minutes" in bold, and a list — it looks almost correct, even though the code has errors.

Then run all your own pages through the validator until each shows no errors.

Common mistakes

  • Trusting the browser. "It looks fine" does not mean it is correct. Fix: validate.
  • Fixing the last error first. Many errors disappear when you fix the first one. Fix: work from the top.
  • Ignoring warnings without reading them. Some point to real problems. Fix: read each one.

Check yourself

  1. Where do you validate HTML?
  2. The validator shows 12 errors. Which do you fix first?
  3. Name the three mistakes in the salad page.

Answers

  1. At validator.w3.org/nu/ (by file upload or text input).
  2. The first one — then check again.
  3. The image has no alt; </p> and </strong> are closed in the wrong order; a <p> is directly inside <ul> instead of an <li>.

Next: you know everything you need. Time for the final project: your complete website.

Chat with us on WhatsApp