Sub topic 5
Headings and paragraphs
This chapter assumes you can create and save an HTML file, open it in a browser, and write the document skeleton from the earlier chapters (doctype, html, head, body). Here you will learn how to mark up the text that makes up most pages: headings and paragraphs.
Why headings and paragraphs matter
Most of a web page is prose. HTML gives that prose two basic building blocks:
- The
pelement marks a paragraph. - The
h1toh6elements mark headings.
Both describe what the text is. They do not describe how it looks. Browsers apply default styles, such as large bold headings and space between paragraphs, but those defaults are only a starting point that CSS can change. The meaning stays in the markup and is used by:
- Screen readers, which let users jump from heading to heading.
- Search engines, which use headings to understand a page.
- Browser reader modes and other tools that build an outline of the page.
Paragraphs
A paragraph is a p element. It contains text and inline elements such as a, em and strong.
<p>HTML describes the structure of a page.</p>
<p>Each paragraph is a separate block. Browsers add space between them.</p>Whitespace is collapsed
Browsers collapse runs of spaces, tabs and line breaks inside text into a single space. Your source formatting does not control what the reader sees.
<p>
This text
is split across several
lines in the source.
</p>This renders as one line of text: "This text is split across several lines in the source." Use this to your advantage. You can wrap long lines in your editor without changing the output.
Do not use empty paragraphs for spacing
Writing <p></p> or <p> </p> to push content down is a common mistake. Empty paragraphs add noise for assistive technology, and the spacing breaks as soon as the styles change. Control spacing with CSS margins instead.
Paragraphs end automatically
The end tag </p> is technically optional in some cases. The browser closes an open p when it meets another block-level element such as a heading, a list or a table. Do not rely on this, because it leads to surprises:
<p>Intro text
<ul>
<li>One</li>
</ul>
</p>The ul closes the first paragraph, so the final </p> has no open paragraph to close. The browser creates an empty paragraph from it. A p cannot contain a list, a heading, a table, a div or another p. Always write the end tag yourself and close the paragraph before starting a block.
<p>Intro text</p>
<ul>
<li>One</li>
</ul>Headings
HTML has six heading levels, from h1 (the most important) to h6 (the least).
<h1>Garden Notes</h1>
<h2>Growing tomatoes</h2>
<h3>Choosing a variety</h3>
<h3>Watering</h3>
<h2>Growing herbs</h2>Levels express hierarchy. An h3 is a subsection of the nearest h2 before it, and that h2 is a subsection of the h1. The page above has this outline:
Choose levels by structure, not by size
The default size of each level is a styling detail. These are the wrong reasons to pick a heading:
- "I want smaller text, so I will use
h4." - "I want bold text, so I will use
h2."
Pick the level that matches the position of the section in the outline. If you need a different size, use CSS. If you only need bold text without structure, use strong or a styled span.
Do not skip levels
Going from h2 straight to h4 leaves a gap in the outline. Screen reader users who list headings will wonder whether content is missing. You may go back up by any amount, such as from an h4 to an h2 when a new major section starts. You should only go down one level at a time.
| Pattern | Valid outline? | Comment |
|---|---|---|
| `h1`, `h2`, `h3`, `h2` | Yes | Steps down one level at a time and goes back up |
| `h1`, `h2`, `h3`, `h1` | Allowed, unusual | A second `h1` is discouraged on most pages |
| `h1`, `h3` | Poor | Skips `h2` |
| `h2`, `h1` | Poor | The page starts below the top level |
How many h1 elements?
The usual rule is one h1 per page, naming the page's main topic. The HTML standard does not forbid more than one. However, the outline algorithm that was meant to adjust heading levels by nesting depth was never implemented by browsers, so every h1 is treated as top level in practice. Several h1 elements flatten your structure. Use one, and make it similar to the title in the head, though it does not need to be identical.
Headings inside sectioning elements
A heading usually introduces a section of the page. Later chapters cover section, article and the other layout elements. For now, note that headings keep their numbered level wherever they sit. An h2 inside an article is still an h2. Choose the level from the overall outline of the page, not from how deeply the element is nested in the markup.
Subtitles and taglines
A heading element should hold the heading and nothing else. A tagline or subtitle is not a new heading level. Mark it up as a paragraph after the heading:
<h1>Garden Notes</h1>
<p>Practical advice for small plots.</p>The hgroup element exists to group a heading with a subtitle or tagline, but a plain p is enough for most pages.
Line breaks, rules and preformatted text
Three related elements are often confused with paragraphs.
bris a void element that inserts a line break inside a paragraph. Use it where the line break is part of the content, such as a poem or a postal address. Do not use it to separate paragraphs or add vertical space.hris a void element that marks a thematic break between paragraph-level content. It draws a horizontal line by default, but its meaning is "the topic shifts here".prekeeps whitespace and line breaks exactly as written, in a monospaced font. Use it for text where layout matters, such as code listings or ASCII tables.
<p>
221B Baker Street<br>
London<br>
NW1 6XE
</p>
<hr>
<pre>
Name Score
Ada 92
Grace 88
</pre>A worked example
This short article uses a heading hierarchy, paragraphs and one line break correctly:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Garden Notes: Tomatoes</title>
</head>
<body>
<h1>Growing tomatoes</h1>
<p>Tomatoes need sun and regular watering.</p>
<h2>Choosing a variety</h2>
<p>Cherry tomatoes ripen early. Beefsteak tomatoes need a longer season.</p>
<h2>Watering</h2>
<p>Water at the base of the plant, in the morning.</p>
<h3>Common mistakes</h3>
<p>Wet leaves can spread disease.<br>
Keep the water on the soil.</p>
</body>
</html>To check the outline, open the page in a browser and press Tab to move through it. You can also use a screen reader's heading list, or an extension that shows the heading structure. You should see one h1, two h2 sections and one h3 under "Watering".
Common errors
- Choosing a heading level for its size.
- Skipping levels, such as
h2followed byh4. - Using several
h1elements to make everything look important. - Putting a block element such as a list,
divortableinside ap. - Using empty paragraphs or repeated
brelements for vertical spacing. - Wrapping a whole page section in a heading element. Headings should contain short text.
- Leaving out the
pelement and putting bare text directly in thebody. This works, but the text has no paragraph meaning.
Summary
Use p for each paragraph and close it explicitly. Pick heading levels from the outline, keep one h1 per page and never skip levels going down. Use CSS for size and spacing, and reserve br, hr and pre for the cases where their meaning applies.
Exercise
A page has these headings in order: h1 "Recipes", h2 "Soups", h4 "Tomato soup", h2 "Desserts". Name the problem and fix the markup. Then explain why <p>Ingredients<ul><li>Salt</li></ul></p> produces unexpected results.
Show answer
The h4 skips the h3 level, so the outline has a gap. Change it to an h3, which makes "Tomato soup" a subsection of "Soups". In the second snippet, the ul is a block element, so the browser closes the paragraph when it reaches the ul. The final </p> then has no open paragraph, and the browser creates an empty one. The fix is <p>Ingredients</p><ul><li>Salt</li></ul>.
Exercise
A designer asks you to make a short tagline under the page title look small and grey. Which element should hold the tagline, and where should the size and colour come from?
Show answer
Put the tagline in a p element after the h1, because a tagline is not a new section of the outline. The size and colour should come from CSS, not from choosing a different heading level.