Sub topic 1
Start with a valid HTML document
Every page on the web begins as a text file that the browser reads from top to bottom. If the file follows the HTML5 rules, the browser builds the page you expect. If it doesn't, the browser guesses, and different browsers can guess differently. This chapter builds the skeleton that the rest of the tutorial adds to.
Prerequisites
This chapter assumes you can do three things:
- Create and save a plain text file in a text editor.
- Give a file the
.htmlextension, such asindex.html. - Open that file in a web browser by double-clicking it or using File, Open.
You don't need any HTML or CSS experience.
The working model
The browser parses your file into a tree of nested elements called the Document Object Model (DOM). An element is usually a start tag, some content, and an end tag, such as <title>Home</title>. Elements inside other elements are their children. Every valid page has one html element at the root. That element has exactly two children: head and body.
Here is a minimal valid document. Save it as index.html and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Jane Doe | Web Developer</title>
</head>
<body>
<h1>Jane Doe</h1>
<p>I build small, fast websites.</p>
</body>
</html>The tab should read "Jane Doe | Web Developer", and the page should show a heading and one paragraph.
What each part does
| Part | Purpose |
|---|---|
| `<!DOCTYPE html>` | Tells the browser to use standards mode. Without it, browsers fall back to quirks mode, which imitates old behavior. |
| `<html lang="en">` | Marks the root element and declares the page language. Screen readers use it to choose a pronunciation. |
| `<meta charset="utf-8">` | Sets the character encoding so accented letters and symbols display correctly. |
| `<meta name="viewport" ...>` | Makes phones use the device width instead of a zoomed-out desktop width. The responsive chapters depend on it. |
| `<title>` | Sets the browser tab text, the bookmark name, and the headline in search results. |
| `<body>` | Holds everything the visitor sees. |
The head holds information about the page. The body holds the page itself. Nothing in the head appears on screen except the title in the tab.
Common errors
- Missing doctype. The page still shows, but spacing and sizing can differ from what you expect because of quirks mode.
- Missing charset. Characters such as the copyright sign can turn into garbled symbols. Put the charset tag early in the
head, before the title. - Wrong file extension. A file saved as
index.html.txtopens as plain text. Turn on "show file extensions" in your file manager to check. - Unclosed or crossed tags. Browsers repair these silently, so the page may look fine while the structure is wrong. Close tags in the reverse order you opened them.
- Content in the
head. Headings and paragraphs placed in theheadget moved into thebodyby the parser, which can change your layout.
Check your work
Open the page and use your browser's "View page source" command. Confirm that the source matches what you saved. Later, in the validation chapter, you will run the file through the W3C validator. It reports the errors the browser hides.
Exercise
A visitor's browser shows the text "Café" as "Café". Which line in the document is missing or placed too late, and where does it belong?
Show answer
The <meta charset="utf-8"> line is missing or too late. It belongs inside the head, near the top and before the title, so the browser knows the encoding before it reads any text.
Exercise
Why does the skeleton put the viewport meta tag in the head and not in the body?
Show answer
Meta tags describe the page to the browser and are only valid in the head. The browser reads the viewport tag before it draws anything, so it can pick the correct page width on a phone.