Sub topic 3
The document skeleton
This chapter covers the 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 a local file in a browser, and recognize an element with its start tag, end tag and attributes from the previous chapter.
The four required parts
Every complete HTML document has the same outline. Here is a minimal example:
<!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 it as index.html and open it in a browser. The tab shows "My First Page", and the page shows a heading and a paragraph.
| Part | Role |
|---|---|
| `<!DOCTYPE html>` | Tells the browser to use standards mode. |
| `<html>` | The root element. All other elements sit inside it. |
| `<head>` | Information about the page that is not shown in the page itself. |
| `<body>` | The content people see and use. |
The doctype
The doctype must be the very first thing in the file. It is not an element and has no end tag. Its only job is to make the browser render the page in standards mode.
Without it, browsers fall back to quirks mode, which imitates old browser behavior. Layout and box sizing then differ in ways that are hard to trace. The short form <!DOCTYPE html> is all you need. It is not case sensitive, but writing it in this form is the convention.
The html element
The html element is the root. It has exactly two children, in this order: head, then body.
Always set the lang attribute on it. The value is a language code such as en, fr or pt-BR. Screen readers use it to choose the right pronunciation. Browsers use it for hyphenation and translation prompts, and search engines use it to classify the page.
The head and the body
The head holds metadata. Its contents do not appear in the page, except that the title shows in the browser tab. The next chapter covers head metadata in detail. For now, include three things:
<meta charset="utf-8">declares the character encoding. Keep it within the first 1024 bytes of the file.<meta name="viewport" ...>makes the page scale correctly on phones.<title>names the page. Every page needs one, and it should be unique.
The body holds everything visible: text, images, links, lists, tables and forms. A document has exactly one body.
The diagram shows the structure the browser builds, called the DOM tree. Each element is a node, and nesting creates parent and child relationships.
Common errors
- Missing doctype. The page still displays, but in quirks mode. Layout may then look different from what you expect.
- Content in the wrong place. A
pinsideheadis invalid. The browser silently closes the head and moves on, so the result can look odd. - Missing charset. Characters such as
éor€appear as garbled symbols. - Missing
lang. The page works, but assistive technology guesses the language and may guess wrong. - Two
bodyelements or a secondhtml. Browsers merge or discard the extras, which hides your mistake.
Indentation
Browsers ignore indentation. Indent children by two spaces anyway, so the nesting is visible and mismatched tags are easy to spot.
Exercise
A page shows the text "café" as "café". Which part of the skeleton is most likely missing or wrong, and what line fixes it?
Show answer
The character encoding declaration in the head is missing or does not match the file's encoding. Add <meta charset="utf-8"> inside head, early in the file, and save the file as UTF-8.
Exercise
List the correct order of these items from first to last in a document: body, <!DOCTYPE html>, head, html.
Show answer
<!DOCTYPE html>, then html, which contains head followed by body.