Best Practices — HTML

Start with <!DOCTYPE html> and <html lang="en">. <head> holds <meta charset="UTF-8"> and the <title>, and <body> holds <header>, <nav> and <main>. Replace a…

Your page can look tidy on screen and still have problems. A screen reader (a program that reads pages aloud for blind users) may read your English text with the accent of another language. The browser may quietly fix tags that are arranged the wrong way, and the result may not be what you meant.

The habits that prevent problems like these are called best practices. They are ways of writing HTML that make your page correct in every browser, make your code easy for other people to read, and let everyone use the page. Most of them don't change what you see on screen, but they matter to browsers, Google and screen readers.

A tidy page skeleton

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Profile</title>
</head>
<body>
  <header>
    <nav><a href="#">Home</a></nav>
  </header>
  <main>
    <h1>My Profile</h1>
    <img src="photo.jpg" alt="Profile photo">
    <label for="name">Name:</label>
    <input id="name">
  </main>
</body>
</html>

Semantic tags, not named <div>s

<!-- Not quite right -->
<div class="header">...</div>

<!-- Better -->
<header>...</header>

A named <div> is a <div> that only gets a name through its class, like <div class="header"> above. The class header is only a name for CSS. Browsers and screen readers don't know what it means. Semantic tags (tags whose name explains their content) such as <header>, <nav> and <main> have a meaning that browsers and screen readers recognise.

When you replace a named <div> with a semantic tag, change only the opening tag and the closing tag. The content stays. <div class="header"> becomes <header>, and its matching </div> becomes </header>. Don't only add a <header> next to the old <div>, because the old <div> has to go.

Other small habits

⚠️ Code that works now but causes trouble later

TL;DR: Start with <!DOCTYPE html> and <html lang="en">. <head> holds <meta charset="UTF-8"> and the <title>, and <body> holds <header>, <nav> and <main>. Replace a named <div> such as <div class="header"> with its semantic tag (<header>), without losing the content. Close every tag, keep headings in order, stop using obsolete tags such as <center>, and check your page at validator.w3.org.

⚡ The two lines at the top

Write:

<!DOCTYPE html>
<html lang="en">
Guess first: without lang="en", how does a screen reader read your English text?

Possibly with the accent of another language. lang says the page is in English, so the screen reader reads it with English pronunciation.

Meaning: <!DOCTYPE html> on the first line makes the browser use standards mode. Without it, the browser switches to quirks mode (an old mode) and some CSS gives strange results.

⚡ Semantic tags, not named <div>s

Write:

<!-- Not quite right -->
<div class="header">...</div>

<!-- Better -->
<header>...</header>

Meaning: A named <div> is a <div> that only gets a name through its class. The class header is only a name for CSS, but the <header> tag has a meaning that browsers and screen readers recognise. Change the opening and closing tags and keep the content. Don't only add a <header> next to the old <div>: the old <div> has to go.

⚡ Leave obsolete tags behind

Meaning: <center> is no longer in the HTML standard. Centring is a job for CSS: text-align: center. Close every tag you open, keep headings in order, then check your page at validator.w3.org (a site that checks HTML).

What you will learn