An HTML file looks like a pile of markup until you know the vocabulary. But every line in a complete page is doing a job: it declares the document type, it sets the character encoding, it sizes the viewport for the phones, and it gives the page a title. Here is the whole structure, one piece at a time.
The previous lesson in this module covered "What Happens Between Your Browser and a Web Server?", and the next lesson in this module is about "How Do You Write and Open Your First Web Page?".
What does a complete page look like?
This is the smallest page that is still complete and correct.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The page title</title>
</head>
<body>
<h1>Hello, world</h1>
<p>Everything visible goes in the body.</p>
</body>
</html>There are four pieces at the top level. There is the doctype, there is the html element, and there are its two children, head and body. The head holds the information about the page, and the body holds the page itself.
What does the doctype do?
The first line is not decoration. It tells the browser which parsing rules to use, and it has to be the very first thing in the file. Not even a comment or a blank line before it.
<!doctype html> modern standards mode: the parser follows the current rules
(no doctype) quirks mode: the browser guesses, and old bugs come backWithout a doctype, the browsers fall back to the quirks mode, where the layout rules from decades ago are applied to a few elements. A page can look different in the quirks mode for no reason that you can see in the code.
The modern HTML has exactly one doctype, and it is not optional.
What belongs in the head?
The head is the metadata. It holds the things that the browser and the other software need, and not the things that the reader sees.
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="What this page is about, in one sentence.">
<title>The page title</title>
<link rel="stylesheet" href="/styles.css">
<link rel="icon" href="/favicon.ico">
</head>The charset must be the first thing in the head, inside the first 1024 bytes. It decides how the bytes become characters, and getting it wrong produces the classic mojibake, which is a run of odd characters where an accented letter should be.
The viewport is what makes a page respond to phone screens. Without it, the mobile browsers assume a fixed wide layout and scale it down, so your text ends up tiny and unreadable.
The title is the only element in the head that a reader sees. It is the tab label, it is the bookmark name, and it is the headline that the search engines show.
The description is the summary under the title in the search results. It is not a ranking factor. It is a click-through factor.
The link attaches the stylesheet and the favicon, and it preloads the resources that the page will need.
Why does the html element carry a language?
The lang attribute tells the browser and the assistive technology which language the text is in.
It changes how a screen reader pronounces the words, how the browser decides whether to offer a translation, and how the text-to-speech handles the page.
It is also useful for the CSS, because you can then write rules for one specific language.
<html lang="en">
<body>
<p lang="fr">Une phrase dans une autre langue</p>
</body>
</html>What goes in the body?
Everything that a reader can perceive. There are the headings, the paragraphs, the images, the links, the forms, and the scripts that make the page interactive.
The body has no required attributes, and adding presentational attributes to it, such as a background colour or a width, is a sign that the styling belongs in a stylesheet instead.
How do the elements nest?
The elements nest by containment, and the document tree is what everything else works on. The CSS selector body > p means a paragraph that is a direct child of the body, and the accessibility tree of the browser is built from the same structure.
<body>
<section> <!-- a direct child of body -->
<p>Directly inside the section.</p>
<div>
<p>Nested one level deeper.</p>
</div>
</section>
</body>The tags have to be closed in the right order. The last tag that is opened is the first one that is closed.
The browsers are forgiving about mistakes, and they will try to repair a broken page. That is exactly why a broken page can look almost right while it behaves strangely. The repair rules are not obvious, so you never rely on them.
You close your tags properly, and the page behaves the same in every browser.
What does the viewport meta tag actually do?
It is two settings in one attribute.
The width=device-width says that the layout viewport is as wide as the screen, so that a page can lay out against the real pixels instead of a fictitious 980-pixel canvas.
The initial-scale=1 says that the page starts at one device pixel per CSS pixel, so that the text is the size you designed and not a scaled-down postage stamp.
<meta name="viewport" content="width=device-width, initial-scale=1">Both parts matter.
With the width setting alone, a phone lays out a wide page and then scales it down, so everything is small. With the scale setting alone, the width is still the fictitious one, so you get a wide page that scrolls sideways.
Together they give you a layout that behaves like the desktop one you built, scaled to the screen.
Do the comments and the whitespace matter?
The comments never render, and they are free in terms of the performance. You use them to explain why something is there, and not what the markup says. The markup already says what it is.
<!-- a comment is invisible to the reader and to the browser -->
<section class="promo">
<!-- The order of these two fields matters: the total depends on both. -->
<input type="number" id="quantity">
<input type="number" id="unit-price">
</section>The whitespace between the elements does matter, but only in one place. Inside a pre block, the spaces and the line breaks are preserved exactly as you wrote them.
Everywhere else, the HTML collapses runs of whitespace into a single space. That is why you never see the accidental gaps in a paragraph, and why you do see them in the code samples.
Quick reference
- The doctype goes first, on its own line, and it puts the page in the standards mode.
- The head holds the metadata and the body holds the content. Nothing else belongs at the top level.
- The charset is first, the viewport is for the mobile, and the title is for the humans and the search engines.
- The lang on the html changes the pronunciation, the translation, and the styling.
- You close the tags in the reverse order of the opening, and you never rely on the browser repair.
Try it yourself: you write a page with a doctype, a charset, a viewport, a description, and a title, and you check each one of them in the developer tools. You find your title in the tab, you find the description by searching for it in the page source, and you remove the viewport line to see the mobile layout change.