Sub topic 3
The document skeleton
This chapter covers the outer structure every HTML page shares: the doctype, the html element, the head, and the body. It assumes you can create and save a text file, open it in a browser, and recognize an element with a start tag, an end tag and attributes (covered in the previous chapter).
Why the skeleton matters
Browsers accept almost any input and try to repair it. That forgiving behavior hides mistakes. A page with a missing doctype or a misplaced element can still appear on screen, but it may render differently from one browser to the next, or behave oddly with screen readers and search engines.
The skeleton is the small amount of structure that removes this guesswork. It tells the browser which rules to apply, what language the content is in, and where the page's information about itself ends and its visible content begins.
A minimal complete document
<!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, HTML</h1>
<p>This is a paragraph.</p>
</body>
</html>Save this as index.html and open it in a browser. You should see a heading and a paragraph, and the tab title should read "My First Page".
The document has four layers:
- The doctype declaration.
- The
htmlroot element. - The
head, which holds metadata. - The
body, which holds the visible content.
The doctype
<!DOCTYPE html> is the first line of the document. It is not an element, so it has no end tag and no content. It is an instruction that tells the browser to use standards mode when rendering the page.
Without it, browsers switch to quirks mode, a compatibility mode that imitates the behavior of very old browsers. Quirks mode changes details such as how box sizes are calculated and how some table content inherits styles. Those changes are rarely what you want in new work.
Some points to remember:
- The keyword is case-insensitive, so
<!doctype html>works too. Most style guides write<!DOCTYPE html>. - It must come before everything else except an optional byte order mark. A comment placed before it can push some old browsers into quirks mode.
- Older doctypes were long strings that referred to a document type definition. Modern HTML needs only the short form shown above.
The html element
The html element is the root of the document. Every other element is a descendant of it. It holds exactly two children, in this order: one head followed by one body.
Its most useful attribute is lang, which declares the human language of the page:
<html lang="en">The value is a language tag. Common examples are en, fr, es, de, and regional forms such as en-GB or pt-BR. The attribute has real effects:
- Screen readers choose the correct pronunciation rules.
- Browsers can pick suitable hyphenation, quotation marks and fonts.
- Translation tools can tell whether a page needs translating.
- Spell-checkers choose the right dictionary.
If part of the page is in another language, put lang on that element only, for example <p lang="fr">Bonjour</p>. A lang on a child overrides the one inherited from html.
The head element
The head holds information about the document. Almost nothing in it is displayed in the page area. The next chapter covers it in detail, so here is only what a minimal page needs.
| Element | Purpose |
|---|---|
| `meta charset="utf-8"` | Declares how bytes in the file are decoded into characters. |
| `meta name="viewport"` | Tells mobile browsers to use the device width instead of a zoomed-out desktop width. |
| `title` | Names the page in the tab, in bookmarks and in search results. |
Two rules deserve attention:
- The
titleelement is required. A document without one fails validation, and the tab shows the file name or address instead. - The
meta charsetdeclaration must appear within the first 1024 bytes of the file. Browsers look for it early. If it comes too late, they may guess the encoding, and accented letters or symbols can turn into garbled characters. Putting it first in theheadavoids the problem.
Save the file itself as UTF-8 in your editor as well. The declaration only tells the browser what to expect. It does not change how the file was saved.
The body element
The body holds everything the reader sees: headings, paragraphs, lists, links, images, tables and forms. A document has exactly one body. The following chapters fill it with content.
What the browser does with a skeleton you left out
HTML parsing is defined so that the browser always builds a complete tree, even from incomplete markup. If you leave out html, head or body start tags, the parser inserts them for you. The following file is valid enough to render:
<!DOCTYPE html>
<title>Short page</title>
<p>Hello</p>The browser builds the same tree as if you had written the full skeleton, with html, head and body in place. The title ends up in the head and the p in the body.
This is allowed, but it is a poor habit for two reasons. First, the lang attribute has nowhere to go unless you write the html start tag. Second, the parser's guesses can surprise you. If you place a visible element before a title, the parser closes the head early and anything after it lands in the body.
You can see the tree the browser actually built by opening the developer tools and looking at the Elements panel. That panel shows the parsed tree, not your source, so it reveals any elements the parser added or moved.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Omitting `<!DOCTYPE html>` | Quirks mode, with inconsistent rendering. | Make it the first line. |
| Omitting `lang` | Screen readers may use the wrong pronunciation. | Add `lang` to `html`. |
| Putting visible content in `head` | The parser closes the `head` and moves on, so the order is not what you wrote. | Keep only metadata in `head`. |
| Two `body` elements | The second is ignored or merged. | Use one `body`. |
| Missing `meta charset` | Special characters may display incorrectly. | Add it as the first element in `head`. |
| Missing `title` | Validation error, with an unhelpful tab and bookmark label. | Write a short, specific title. |
| Missing viewport meta | Mobile browsers show a shrunken desktop layout. | Add the viewport meta tag. |
Indentation and whitespace
The indentation in the examples is for people. The browser ignores it. Any run of spaces, tabs and line breaks between elements collapses to a single space when text is rendered. Consistent indentation, two spaces per nesting level, makes it easy to see which end tag closes which start tag. If a closing tag is missing, the indentation usually shows the problem before the browser does.
Checking your work
After writing a skeleton, run through this short list:
- The first line is
<!DOCTYPE html>. - The
htmlstart tag has a correctlangvalue. - The
headcontainsmeta charset, the viewportmetaand atitle, and nothing visible. - The
bodyfollows theheadand appears once. - Every start tag that needs an end tag has one, and the elements are nested, not overlapping.
In the browser, open developer tools and check the Elements panel for the expected tree. Then paste the source into the W3C Nu HTML Checker, which reports problems such as a missing title or an unclosed element. A later chapter in this tutorial covers validation in more depth.
Exercise
A page begins with a comment line, then <!DOCTYPE html>, and has <html> with no attributes. The <meta charset="utf-8"> is the last element in a very long head. Name three problems this could cause and how to fix each.
Show answer
First, the comment before the doctype can trigger quirks mode in some old browsers, so move the doctype to the first line. Second, the html element has no lang, so screen readers and translation tools cannot tell the language, so add lang="en" (or the right code). Third, the charset declaration may fall beyond the first 1024 bytes, so browsers may guess the encoding and garble non-ASCII characters, so make meta charset the first element in head.
Exercise
You write a file containing only <!DOCTYPE html>, <title>Test</title> and <p>Hi</p>, with no html, head or body tags. In the browser's Elements panel, where do the title and the paragraph appear, and why?
Show answer
The title appears inside a head element and the paragraph inside a body element, both inside an html element. The HTML parser inserts the missing elements so that it always builds a complete tree. The title is placed in the head because it is metadata, and the paragraph begins the body.