Cheat sheet #4

HTML document structure: headings

A cheat sheet for web design students. Learn why every page needs a single h1, how the six heading levels build a logical content outline, and why heading levels are only ever used for structure, never for size.

The short version

Use one h1 per page, then work down one level at a time, with h2 under h1 and h3 under h2. Never skip a level, and never choose a heading for the size of its text.

h1 Page title h2 Section h3 Subsection h2 Next section
On this cheat sheet

The h1 element

Every page needs one h1, and only one

The h1 is the top-level heading. It states what this page is about, in the same way that a book has a single title on its cover, and every web page should have exactly one.

Do<h1>Coffee menu</h1>
Don’t<h1>Hill Café</h1> <h1>Coffee menu</h1> <h1>Opening times</h1>

Why it matters:

A note on accuracy. The HTML specification doesn’t actually forbid more than one h1. A promised “outline algorithm” that would have let you reuse h1 inside sections was never implemented by browsers, so the dependable approach, and the rule we use on this course, is one h1 per page.

The h1 and the title element are different things

<title><h1>
Where it livesIn the <head>In the <body>, inside <main>
Where people see itBrowser tab, bookmarks, search resultsAt the top of the page content
Typical formPage topic, then site nameThe page topic only
ExampleCoffee menu | Hill CaféCoffee menu

They should agree about the topic of the page, but they don’t need to be identical, word for word.

The h1 and SEO

Write an h1 that says what the page is about

Search engines read the text of the h1 to understand what a page is about. There’s no secret ranking trick here. The aim is simply to describe the page clearly, to people and to search engines at the same time.

Weak h1WhyBetter h1
WelcomeSays nothing about the contentHill Café: coffee and cake in Leeds
Hill Café (on every page)Not unique, doesn’t describe the pageOur coffee menu
Coffee coffee best coffee cheap coffeeKeyword stuffingFreshly roasted coffee
Click here to find out more about our servicesLong and vagueCatering services

Building a content outline with headings

The six levels

HTML has six heading levels, h1 to h6. The number represents the heading’s rank in the hierarchy, where 1 is the most important. It is not a font size.

LevelRoleUse
h1Title of the pageExactly one
h2Main sections of the pageAs many as needed
h3Subsections of an h2As needed
h4 to h6Further subdivisionsRare. If you reach h5, consider splitting the page.

Think of a table of contents

Headings form an outline of your page, much like the contents page of a book, with its chapters, sections and subsections. Here’s a good test. If you removed everything except the headings, would what remains read as a sensible summary of the page?

The matching HTML:

<h1>Coffee menu</h1> <h2>Hot drinks</h2> <h3>Espresso-based</h3> <h3>Filter and brewed</h3> <h2>Cold drinks</h2> <h3>Iced coffee</h3> <h3>Smoothies</h3> <h2>Allergy information</h2>

The indentation here is only used to show the hierarchy. In your real files, each heading will be followed by paragraphs and other content.

The logic: go down one level at a time

Each heading is the child of the nearest heading above it with a higher rank. That means the next heading can do one of three things:

What it must never do is skip a level on the way down. An h3 straight after an h1 would be a subsection of a section that doesn’t exist, and that breaks the hierarchy.

After this heading…Go deeper toStay atGo back up to
h1h2 onlyNone (no second h1)–
h2h3h2–
h3h4h3h2
h4h5h4h3 or h2
h5h6h5h4, h3 or h2
h6–h6h5 down to h2
Valid orderh1 h2 h3 h3 h2 h3 h4 h2
Broken orderh1 h3 ← skips h2 h2 h4 ← skips h3 h2 ← before the h1 h1 ← a second h1
Do<h1>Coffee menu</h1> <h2>Hot drinks</h2> <h3>Espresso-based</h3>
Don’t<h1>Coffee menu</h1> <h3>Hot drinks</h3> <h5>Espresso-based</h5>

Good heading habits

Semantics, not size

Heading levels describe structure, not appearance

Browsers display h1 large and h6 small by default, so beginners are often tempted to choose a heading level for the size they want. This is known as presentational markup, and it’s wrong. The heading level must reflect where the heading sits in the hierarchy, and setting its size is the job of CSS.

Do: choose the level by structure, set the size in CSS<h2 class="small-heading">Allergy information</h2> .small-heading { font-size: 1.1rem; }
Don’t: pick the level by size<h1>Coffee menu</h1> <h4>Allergy information</h4> <!-- h4 chosen because it's smaller -->
Do: use a real heading<h2>Opening times</h2>
Don’t: fake a heading with styling<p><strong>Opening times</strong></p> <div class="heading">Opening times</div> <h1 style="font-size:12px">Opening times</h1>

Two mistakes, one principle. Don’t use a heading element just to get big or small text, and don’t use bold p, div or span elements to make text look like a heading. In either case, the structure that assistive technologies and search engines read no longer matches what the page shows.

Quick check: choosing between a heading and styled text

QuestionIf yes
Does this text introduce a section of content?Use a heading, and pick the level from the outline
Do I just want it to look big or bold?Use CSS on a suitable element (p, span)
Do I want a smaller or larger heading than the default?Keep the correct level and change its size with CSS

Where headings sit in the document

Headings work inside a semantic page structure

Headings organise the content, while structural elements organise the page. Together, they give assistive technologies both a map of the page (the landmarks) and a table of contents (the headings).

ElementPurpose
<header>Introductory content: logo, site name, tagline
<nav>A block of main navigation links
<main>The unique content of this page. Use one per page. The h1 goes here.
<section>A themed group of content, normally introduced by a heading
<article>A self-contained item, such as a blog post
<aside>Related but secondary content, such as a sidebar
<footer>Closing content: contact details, copyright
<body> <header> <p>Hill Café</p> <!-- site name: not an h1 --> <nav>…</nav> </header> <main> <h1>Coffee menu</h1> <!-- the one h1 --> <section> <h2>Hot drinks</h2> <p>…</p> <h3>Espresso-based</h3> <p>…</p> </section> <section> <h2>Cold drinks</h2> <p>…</p> </section> </main> <footer>…</footer> </body>

Heading levels don’t restart inside a section or article. Keep counting down through a single, page-wide outline, so an h2 inside a section still means “the second level of the page”.

Common mistakes at a glance

MistakeFix
No h1 on the pageAdd one that names the page
Several h1 elementsKeep one, and demote the others to h2 where they are sections
Jumping from h2 to h4Use h3, or restructure the content
Choosing a level because of its sizeChoose it by hierarchy, then resize with CSS
Bold p or div posing as a headingUse a real heading element
Same h1 on every pageWrite a unique, descriptive h1 for each page
Vague headings such as “Info”Say what the section covers

Check before you upload

Learn more

Tip: browser extensions such as HeadingsMap, or the W3C Markup Validator, can show you the heading outline of any page, so you can check its hierarchy at a glance.