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>
<!DOCTYPE html>goes on the very first line, written exactly like that. It makes the browser use standards mode (the HTML rules in use today). Without it, the browser switches to quirks mode (an old mode), and some CSS gives strange results.<html lang="en">names the page's language: en is the code for English. Screen readers then read the text with English pronunciation.langdoesn't translate anything, but the browser's translation and spell-check features use it to know the language. Google doesn't use it: Google decides the language from the visible text.<head>holds information that doesn't show:<meta charset="UTF-8">and the<title>, the title in the tab. This skeleton is kept short on purpose. A real page also needs<meta name="viewport" content="width=device-width, initial-scale=1.0">here, so it fits a phone screen (Meta Tags lesson).<body>holds everything that shows.<header>is the top part of the page, and here it wraps the<nav>menu. Thehref="#"in that menu only stands in for an address in this example. On a real page, write the home page's address, for examplehref="index.html".<main>is the main content.<h1>is the title shown on the page. It is not the same as<title>, which only shows in the tab.alton the image and a<label for>that matches the input'sidlet screen readers read both of them.
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
- Close every tag you open. The exceptions are tags with no closing tag, such as
<img>,<input>and<meta>. - Keep headings in order:
<h1>then<h2>. Don't jump directly to<h4>. - Don't use obsolete tags such as
<center>, which is no longer in the HTML standard. Centring is a job for CSS:text-align: center. - Check your page at validator.w3.org. The validator shows tags that are misspelled or never closed.
⚠️ Code that works now but causes trouble later
- Elements that must not wrap each other. For example, an
<a>inside an<a>, or a<div>inside a<p>. The browser quietly fixes it, and the result is often not what you meant. style="..."everywhere. This attribute writes the styling directly on each tag, so later you have to change it one tag at a time. Put the styling in a CSS file and apply it with a class.- Mixed class names. Choose one style, usually kebab-case (lowercase with hyphens):
profile-card, not a mix ofprofileCardandprofile_card.
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).