Anatomy of an HTML Document
The skeleton every page needs: doctype, html with lang, head, body, charset and title. See what the parser does when a tag lands in the wrong place.
Anatomy of an HTML Document
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A complete page is `<!doctype html>`, then `<html lang="en">` containing one `<head>` and one `<body>`. The head holds information about the page. The body holds what people see.
- Anything in `<head>` that is not metadata ends the head early. In a test, a `<div>` placed before a `<meta charset>` pushed the meta into the body, where it does nothing.
- `<meta charset="utf-8">` tells the browser how to decode the bytes, and `<title>` is the text in the tab, in bookmarks and in search results.
- The `lang` attribute on `<html>` declares the page language. Screen readers pick a pronunciation from it, and browsers use it for hyphenation and translation prompts.
- You can omit `<html>`, `<head>` and `<body>` tags and the parser will still create them. Writing them is for readers and tools, not for the browser.
The smallest page that is correct
Every HTML file you write starts from the same skeleton. Here it is, with the four lines that matter:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first page</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
Think of it like a letter. The doctype is the language you are writing in, the head is the envelope and header (who, what, how to read it), and the body is the letter itself. The envelope isn’t shown to the reader, but without it the letter doesn’t reach anyone properly.
The doctype and quirks mode
<!doctype html> isn’t an element. It is an instruction to the parser. It exists because browsers kept old rendering behaviour for pages from the 1990s, and the doctype is how a page says “I’m modern, use the real rules”. Leave it out and you get quirks mode, where the box model, table sizing and some line-height rules differ.
In a test with headless Chromium 159, a page with no doctype reported document.compatMode === "BackCompat", and the same page with <!doctype html> reported "CSS1Compat". Always write the doctype, in that exact short form. The older long forms with a DTD URL are historical.
The html element and lang
<html> is the root of the tree. Its most useful attribute is lang. It tells the browser and assistive technology what language the text is in, which affects which voice a screen reader uses, what the browser offers to translate, and how hyphenation works. The language and internationalisation lesson goes further. For now: set it on every page.
The head
The <head> holds information about the page. The three entries you need in nearly every page:
<meta charset="utf-8">says how to turn the file’s bytes into characters. The HTML spec asks that it appear within the first 1024 bytes of the file, so put it first.<meta name="viewport" ...>tells phones to lay the page out at the device width instead of pretending to be a wide desktop screen. The meta tags lesson explains it.<title>is the text in the tab, in history and bookmarks, and as the clickable headline in search results. Every page needs its own.
Stylesheets, scripts and icons are also declared here. Those get their own lessons: link elements and script loading.
What happens when something is out of place
The head only accepts metadata elements: title, meta, link, style, script, noscript, base and template. Put anything else in it and the parser decides the head is over. I tested this with <head><title>t</title><div>oops</div><meta charset="utf-8"></head><body>x</body>:
- The resulting
headcontained only thetitle. - The
divand themetaboth ended up in thebody:<div>oops</div><meta charset="utf-8">x.
So the charset declaration sat in the body, doing nothing. A single stray element before it silently cancelled it. Likewise, in a test with <!doctype html>hello<title>t</title>, the head came out empty and the title sat in the body, because text before it started the body. Order inside the head matters, and the cure is simply to keep only metadata there.
Also, <html>, <head> and <body> are optional in the source. The parser invents them: in a test, the bare string hello produced <html><head></head><body>hello</body></html>. Writing them anyway makes the structure obvious to people and tools, and it’s where you attach lang.
What the video covers
The embed is Kevin Powell’s “HTML & CSS for Absolute Beginners: DOCTYPE, head, and body” (January 2025, about 9.5 minutes), from his free beginner course. The chapters are setting up the doctype (0:00), basic HTML structure (1:25), head and body components (2:58), viewing your website (4:44) and updating and saving changes (6:18). The description links an article on the doctype declaration. I could not read a transcript, so I describe it only from the chapters. The lang and charset details, the out-of-place-element test and the quirks-mode check are this lesson’s own. The deeper link covers the same ground within a tour of the most common elements.
Where this fits
The previous lesson, what HTML is and how browsers read it, described the parser. This one gives it the skeleton it expects. Next, elements, attributes and nesting is the vocabulary for everything you put inside the body.
Check yourself
What does `<!doctype html>` do, and what happens if you leave it out?
It puts the browser in standards mode. Without it the browser uses quirks mode, a set of legacy layout rules. In a test, `document.compatMode` was `BackCompat` with no doctype and `CSS1Compat` with it.
You write `<head><title>Hi</title><div>x</div><meta charset="utf-8"></head>`. Where does the `meta` end up, and why?
In the body. A `div` is not allowed in the head, so the parser closes the head and opens the body at that point. Everything after it, including the meta, becomes body content and the charset declaration is ignored.
Why put `lang` on the `html` element rather than leaving it off?
It is the page's declared language. Assistive technology uses it to choose a speech voice, browsers use it for translation and hyphenation, and search engines use it as a hint. Without it each tool has to guess.
What belongs in the head, and what in the body?
The head holds metadata that is not shown as page content: character set, title, viewport, stylesheet and script references, icons. The body holds the content that people read and interact with.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.