Sub topic 5
Headings and paragraphs
This chapter assumes you can write a complete HTML document (doctype, html, head, body) and that you know the parts of an element: start tag, content, end tag and attributes. Headings and paragraphs are the first content elements most pages use, and they set the outline that readers, search engines and assistive technology rely on.
Headings
HTML has six heading elements, h1 to h6. The number is the level. h1 is the most important heading and h6 the least.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Garden Notes</title>
</head>
<body>
<h1>Garden Notes</h1>
<h2>Vegetables</h2>
<h3>Tomatoes</h3>
<h3>Carrots</h3>
<h2>Flowers</h2>
<h3>Roses</h3>
</body>
</html>The headings form an outline, like the indented points in a document:
- Garden Notes
- Vegetables
- Tomatoes
- Carrots
- Flowers
- Roses
Rules for good heading order
- Use one
h1per page. It names the page's main topic. - Do not skip levels. Go from
h2toh3, not fromh2toh4. - Choose a heading by its place in the outline, not by its size. Browsers give headings a default size, but you can change the size with CSS.
- Do not use a heading just to make text big or bold. Use CSS for appearance.
Screen reader users often move through a page by jumping from heading to heading. A logical order makes that possible.
Paragraphs
The p element holds a paragraph of text.
<p>Tomatoes need sun and regular watering.</p>
<p>Plant them after the last frost.</p>Browsers show each paragraph as its own block with space above and below it.
Whitespace collapses
HTML treats any run of spaces, tabs and line breaks as a single space. Pressing Enter in your source file does not start a new line in the browser.
<p>
This text is
spread over several lines,
but it displays as one flowing line.
</p>To start a new paragraph, close the p element and open another. Do not stack empty p elements to add space. Use CSS margins for spacing.
Line breaks and thematic breaks
Two elements are void elements, so they have no content and no end tag.
brforces a line break inside a paragraph. Use it where the line break is part of the content, such as an address or a poem.hrmarks a thematic break, a shift of topic between paragraphs.
<p>
12 Orchard Lane<br>
Springfield<br>
SP1 2AB
</p>
<hr>
<p>A new topic begins here.</p>Do not use br to separate paragraphs. Two paragraphs are two p elements.
Preformatted text
The pre element keeps whitespace and line breaks exactly as written. It uses a monospaced font by default. It is useful for text where spacing matters, such as plain-text tables or code samples.
<pre>
Day Hours
Monday 2
Tuesday 1
</pre>Common errors
| Mistake | Problem | Fix |
|---|---|---|
| Several `h1` elements for styling | The outline becomes unclear | Use one `h1`, then `h2` and below |
| `h1` followed by `h4` | A skipped level confuses navigation | Use the next level down |
| Empty `p` elements for spacing | Adds meaningless blank items for screen readers | Use CSS margin |
| `br br` between blocks of text | Not real paragraphs | Use separate `p` elements |
| Text placed directly in `body` | Has no semantic role | Wrap it in `p` |
Exercise
A page has an h1, then an h3 used because it looks the right size, then a row of three empty p elements to create space. Name the two problems and how to fix each.
Show answer
The h3 skips the h2 level, so use h2 and adjust its size with CSS if needed. The empty p elements are used for spacing, so remove them and use CSS margins instead.