Sub topic 1
Start with a valid HTML document
Prerequisites
This chapter assumes you can do the following:
- Create and save a plain text file in a text editor, with a
.htmlextension. - Open an HTML file in a web browser, either by double-clicking it or by using File > Open.
- Work with files, folders and file extensions. Your operating system may hide extensions by default, so check that
index.htmlis not secretly saved asindex.html.txt.
You do not need any earlier HTML or CSS knowledge. This chapter builds the skeleton that every later chapter adds to.
What a valid document is
A browser accepts almost any text you give it and tries to guess what you meant. That forgiveness hides mistakes. A page that looks fine in one browser can break in another, or confuse a screen reader, because the browser repaired your markup in a way you did not expect.
A valid HTML document follows the rules of the HTML Living Standard. It has a known structure: a document type declaration, one root element, a head and a body. When you start from a correct skeleton, later problems with layout, accessibility and search are much easier to trace, because you can rule out the basics.
In this tutorial you will build a single-page personal site. This chapter produces the empty but correct frame it will sit in.
The complete skeleton
Create a folder named my-site and a file inside it named index.html. The name index.html matters. Most static hosts and web servers serve a folder's index.html when a visitor requests the folder's address, so this file becomes your home page.
Paste in this skeleton:
<!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>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<nav aria-label="Main">
<ul>
<li><a href="#about">About</a></li>
<li><a href="#projects">Projects</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<main>
<section id="about">
<h1>Jane Doe</h1>
<p>I build small, fast websites.</p>
</section>
<section id="projects">
<h2>Projects</h2>
</section>
<section id="contact">
<h2>Contact</h2>
<p><a href="mailto:[email protected]">[email protected]</a></p>
</section>
</main>
<footer>
<p>© 2026 Jane Doe</p>
</footer>
</body>
</html>This is the full page you will refine over the tutorial. In this chapter we focus on everything down to the opening <body> tag, because that is the document frame. The landmark elements inside the body (header, nav, main, footer) get their own chapter.
The styles.css file does not exist yet. The page still loads, and the browser quietly ignores the missing stylesheet. The next chapters create it.
Reading the skeleton line by line
The doctype
<!DOCTYPE html>The doctype tells the browser to use standards mode. Without it, browsers fall back to quirks mode, a set of legacy behaviors kept for very old pages. In quirks mode, sizing rules such as the box model can differ from what modern CSS expects, and you will see layouts that do not match any tutorial.
The doctype is not an element and has no closing tag. It is case-insensitive, but <!DOCTYPE html> is the conventional spelling. It must be the very first thing in the file. Even a blank line or a comment before it can trigger quirks mode in some older browsers.
The root element and the lang attribute
<html lang="en">The html element is the root. Everything else in the document is inside it. The lang attribute declares the main language of the page, using a language code such as en, fr or pt-BR.
This one attribute does real work:
- Screen readers use it to choose the correct pronunciation rules. Without it, an English page may be read with the wrong accent or voice.
- Browsers use it to pick hyphenation rules, quotation marks and fonts for the language.
- Translation tools use it to decide whether to offer a translation.
- Validators warn when it is missing.
If part of the page is in another language, put lang on that element only, for example <q lang="fr">Bonjour</q>.
The head and the body
The root has exactly two children, in this order: head and body.
- The
headholds information about the page. Most of it is not displayed in the page area. - The
bodyholds everything the visitor sees and interacts with.
The table below shows where common elements belong.
| Element | Goes in | Purpose |
|---|---|---|
| `meta charset` | head | Sets the text encoding |
| `meta viewport` | head | Controls scaling on small screens |
| `title` | head | Names the page in tabs, history and search results |
| `link rel="stylesheet"` | head | Loads a CSS file |
| `header`, `main`, `footer` | body | Visible page structure |
Character encoding
<meta charset="utf-8">This tells the browser how to turn the bytes in your file into characters. UTF-8 covers essentially every written language and symbols such as the copyright sign and curly quotes.
Two rules prevent most encoding bugs:
- Put this tag first in the
head, and keep it within the first 1024 bytes of the file. The browser has to know the encoding before it reads text that depends on it. - Save the file as UTF-8 in your editor. The tag describes the file, but it does not convert it. If your editor saves in another encoding, you will see garbled characters like
©where you expected©.
The skeleton uses © in the footer. That is a character reference, which writes the symbol using plain ASCII characters and works even if encoding goes wrong. With a correct UTF-8 setup you could also type © directly.
The viewport meta tag
<meta name="viewport" content="width=device-width, initial-scale=1">Phones historically pretended to have a wide screen, around 980 pixels, and shrank the page to fit. That kept old desktop sites readable, but it makes responsive CSS useless, because media queries would measure the fake wide screen.
This tag tells the browser to set the layout width to the real device width and to start at normal zoom. You need it for the responsive work in a later chapter. Avoid adding user-scalable=no or maximum-scale=1. They block pinch zoom, which people with low vision rely on.
The title
<title>Jane Doe | Web Developer</title>The title is required in a valid document, and it appears in several places: the browser tab, bookmarks, the history list and search engine results. Screen reader users hear it first when a page loads, so it should describe this page on its own.
A good pattern is the specific part first, then the site or owner name. For a one-page site, your name and role works well. Avoid placeholders like "Untitled" or "Home".
The stylesheet link
<link rel="stylesheet" href="styles.css">This loads an external CSS file. The href is a path relative to the HTML file, so styles.css must be in the same folder as index.html. A path such as css/styles.css would point into a subfolder instead.
Placing the link in the head means the browser loads the styles before it paints the body. That avoids a flash of unstyled content.
How the browser reads the document
The browser parses your file from top to bottom and builds a tree called the DOM (Document Object Model). Each element becomes a node, and nesting becomes parent and child relationships.
Because the structure is a tree, nesting must be correct. An element opened inside another must be closed before its parent closes. In the skeleton, every li closes before its ul, and the ul closes before the nav.
Rules for valid markup
Several habits keep the document valid:
- Lowercase names. HTML is not case-sensitive for tag names, but lowercase is the convention and keeps files consistent.
- Quote attribute values.
href="#about"is safe. Unquoted values break as soon as they contain a space. - Close what needs closing. Elements like
p,liandsectionneed end tags in this tutorial's style. Browsers can sometimes infer a missing</p>or</li>, but relying on that makes the code harder to read. - Void elements have no end tag.
meta,linkandimgdo not wrap content, so they never have a closing tag. Writing<meta charset="utf-8">is correct. A trailing slash is allowed but unnecessary. - Unique ids. Each
idvalue must appear once per page. Later chapters use ids as link targets, so duplicates cause links to jump to the wrong place. - Use one
h1. The skeleton has a singleh1for the page's main subject.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Page looks different from the tutorial, odd box sizes | Missing or misplaced doctype, so quirks mode | Put `<!DOCTYPE html>` on line 1 |
| Garbled characters such as `’` | File saved in a different encoding than UTF-8 | Re-save as UTF-8 and keep the charset tag first in `head` |
| Page renders tiny on a phone | Missing viewport tag | Add the viewport meta tag |
| Styles do not apply | Wrong `href` path, wrong file name, or missing `rel` | Check the path and spelling, then check the browser's network panel |
| Browser shows `index.html.txt` or downloads the file | Hidden extension | Show file extensions and rename to `index.html` |
You can check which mode the browser chose. In the browser console, type document.compatMode. The value "CSS1Compat" means standards mode, and "BackCompat" means quirks mode.
Checking your work
Validate the file early and often. The W3C Markup Validator at validator.w3.org accepts a pasted snippet, an uploaded file or a URL. Paste in the skeleton and confirm it reports no errors. A warning that the stylesheet could not be found is not an error in the HTML itself.
Validate after each chapter. A small error found right after you introduce it is far easier to fix than a dozen found at the end, and the final chapter returns to validation before publishing.
Exercises
Exercise
You remove the <!DOCTYPE html> line, and your layout widths now behave differently. Which mode is the browser in, and how can you confirm it from the console?
Show answer
The browser switches to quirks mode. Running document.compatMode in the console returns "BackCompat" in quirks mode and "CSS1Compat" in standards mode.
Exercise
A visitor on a phone sees your page shrunk to a tiny desktop layout, even though your CSS has a media query for narrow screens. Which line is probably missing, and why does its absence break the media query?
Show answer
The viewport meta tag, <meta name="viewport" content="width=device-width, initial-scale=1">. Without it, the phone reports a fake width of around 980 pixels, so the media query measures that width and never matches a narrow screen.
Exercise
Your footer shows "©" instead of "©". Name two ways to fix it.
Show answer
Save the file as UTF-8 and keep <meta charset="utf-8"> first in the head, or write the symbol as the character reference ©, which uses only ASCII characters.