Thuta Learning
HTML
BasicWeb DevelopmentBeginner

Headings

HTMLLesson 820 min

What you'll walk away with

  • Choose a heading level from the structure of the content, not from its font size
  • Write one clear <h1> and nest <h2> and <h3> in order without skipping levels
  • Change a heading's size with CSS while keeping its level correct
  • Check a page's outline by reading only its headings

Explain it simply

Explain it simply

Think of the table of contents in a book. There is one book title, there are chapters, and inside each chapter there are smaller sections. HTML headings are that table of contents for a web page: <h1> is the book title, <h2> is a chapter, <h3> is a section inside a chapter. There are six levels, down to <h6>.

Browsers show headings big and bold by default, but that size is only a side effect. The real job of a heading is to announce: "this text is the title of the content that follows, and it sits at this level".

Problem → Why → Solution

The problem: picking levels for their looks

The author of the page below wanted small text for "Related posts", so they picked <h5>. They wanted the "Free shipping" banner large, so they added a second <h1>, and they wrote "Ingredients" as a bold paragraph instead of a heading. To a sighted reader the page looks roughly fine.

recipe.html
<h1>Free shipping on all orders!</h1>
<h1>Coconut Noodle Soup</h1>
<p><b>Ingredients</b></p>
<p>Noodles, coconut milk, chicken, onion.</p>
<h5>Related posts</h5>

Why is this a problem?

Many screen reader users navigate a page through its list of headings, jumping from one heading to the next with a keyboard shortcut. On this page they hear two main titles (one of them an advert), "Ingredients" is missing from the list entirely, and "Related posts" suddenly appears at level 5 with no levels 2, 3 or 4 before it. Search engines and tools that generate a table of contents automatically trust the same broken structure.

Core concept: a level is a rank, not a size

There are six heading elements: <h1>, <h2>, <h3>, <h4>, <h5> and <h6>. The number is a rank. An <h2> starts a new part of the page, and that part continues until the next <h2> or a higher heading. Any <h3> in between is a subsection of that <h2>. In this way the headings turn the page into a tree-shaped outline.

<h1> is the title of the whole page: what this page is about. The HTML standard allows more than one <h1>, but accessibility guides and most teams recommend a single main <h1> per page. Older versions of the spec described an "outline algorithm" that would have demoted an <h1> nested inside <section>, but browsers and screen readers never implemented it and it has been removed from the spec. So you write the level you mean, explicitly.

When going deeper, go one step at a time: a subsection after <h2> is <h3>, not <h5>. Going back up may jump: after an <h4> you can start a new major part with <h2>, which simply says the subsections are finished.

The browser's default stylesheet gives <h1> a size of 2em and <h2> 1.5em, and shows <h5> and <h6> smaller than body text. These are only defaults. CSS can make an <h2> small or an <h4> large, so "I want this text smaller" should never be the reason for picking a heading level.

Keep the text inside a heading short and descriptive: it is a label for what the following part contains. A slogan, a tagline or a subtitle under a title is not a heading, so write it with <p>. Use headings only for text that really titles a part of the page.

Mental model: write the table of contents
  1. One <h1> names the page topic
  2. Each major part gets an <h2>
  3. A subtopic inside a part gets <h3>
  4. Size and colour come from CSS

If reading only the headings gives a sensible table of contents, the levels are right.

General pattern

html
<h1>Page topic</h1>
  <h2>Major part</h2>
    <h3>Subtopic</h3>
    <h3>Subtopic</h3>
  <h2>Next major part</h2>
    <h3>Subtopic</h3>
      <h4>Detail</h4>

The indentation is only there to make the outline visible; the browser ignores it. Each step deeper adds exactly one to the number, and a new part may jump back up to a higher level.

Example: the outline of a tutorial page

This page has one <h1>, two <h2> headings and three <h3> headings, each followed by a paragraph of content for that part. Open it in a browser and read only the headings: you get a table of contents.

headings.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>HTML Full Tutorial</title>
</head>
<body>
  <h1>HTML Full Tutorial</h1>
  <p>Learn to build web pages from the ground up.</p>

  <h2>HTML Basics</h2>
  <h3>Elements and Tags</h3>
  <p>An element is a start tag, some content and an end tag.</p>
  <h3>Attributes</h3>
  <p>Attributes add extra information to an element.</p>

  <h2>Text Content</h2>
  <h3>Headings</h3>
  <p>Headings give the page an outline.</p>
</body>
</html>

Code breakdown

  1. Line 8<h1>HTML Full Tutorial</h1>

    The single <h1> names the topic of the whole page. It often matches the <title>, but <title> shows in the browser tab while <h1> shows on the page itself.

  2. Line 9<p>Learn to build web pages from the ground up.</p>

    The tagline is not a title, so it is a <p>. Making it an <h2> would add a fake part to the outline.

  3. Lines 11-12<h2>HTML Basics</h2> <h3>Elements and Tags</h3>

    The <h2> opens a major part and the <h3> immediately opens a subsection inside it. It goes down exactly one level, so the outline has no gap.

  4. Line 14<h3>Attributes</h3>

    The second <h3> ends the first subsection and starts a new one, still under "HTML Basics". The two <h3> headings are siblings.

  5. Line 17<h2>Text Content</h2>

    Moving from <h3> back up to <h2> says the "HTML Basics" part is over. Going back up like this is correct.

You should see
"HTML Full Tutorial" appears as the largest bold text, "HTML Basics" and "Text Content" are a step smaller, and "Elements and Tags", "Attributes" and "Headings" smaller again, each heading followed by its paragraph. Reading only the headings gives: HTML Full Tutorial > HTML Basics (Elements and Tags, Attributes) > Text Content (Headings).

Changing the look without changing the level

Say the design wants the "Related posts" title in a sidebar to be small. The right answer is to write the level the structure calls for (here <h2>), give it a class, and change its size in CSS. HTML says what it is; CSS says how it looks.

sidebar.html
<style>
  .sidebar-title {
    font-size: 1rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
</style>

<h2 class="sidebar-title">Related posts</h2>
<p>Five tips for cleaner HTML</p>
LevelTypical use
<h1>The topic of the whole page (one)
<h2>The major parts of the page
<h3>Subsections inside one <h2>
<h4> - <h6>Deep structures such as long documentation

In real projects

Where this shows up in real projects

Most documentation sites and blog engines collect the <h2> and <h3> headings of an article to build an "On this page" table of contents automatically. Wrong levels produce a wrong navigation menu. The "Heading 2" and "Heading 3" options in CMS editors are asking you to pick exactly these levels.

Accessibility audit tools such as axe or Lighthouse flag skipped heading levels and pages with no <h1>. Screen readers can open a list of headings and jump straight to the part you want, so on long pages headings are the main means of navigation. Search engines also use heading text as one signal for what a page is about. When teams review a new page, they often use a browser extension to print the heading outline and check that it reads like a table of contents.

Common mistakes

Common mistakes

1. Choosing a level for its font size

Writing <h5> after an <h2> because you want smaller text removes levels 3 and 4 from the outline. Write the <h3> the structure needs and reduce its font-size in CSS.

2. Faking a heading with a bold paragraph

<p><b>Ingredients</b></p> looks like a heading but is missing from the heading list, so a screen reader user cannot jump to that part. If it titles a part of the page, use a real heading element such as <h2>.

3. Making every big piece of text an <h1>

Giving the promo banner, the logo and every card title an <h1> hides what the page is actually about. Keep <h1> for the main topic and write banner text as a <p> styled with CSS.

Best practices

Best practices

  • Sketch the heading outline before writing content; a clear outline makes a clear page.
  • Write heading text that tells the reader what the part contains ("Shipping costs", not "More").
  • Keep all heading styling in CSS and think only about the level in HTML.

Mini challenge

Mini challenge: outline a recipe page

Write a recipe page with correct heading levels. The "Steps" part has two subsections, and the sidebar title must be made small with CSS, not with a lower level.

  • One <h1> with the dish name
  • <h2> for Ingredients, Steps and Related recipes
  • <h3> for Prepare and Cook inside Steps
  • Make Related recipes look small using a class and CSS
recipe.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Recipe</title>
  <style>
    /* TODO: a class rule that makes the sidebar heading small */
  </style>
</head>
<body>
  <!-- TODO: the dish name as the page's only <h1> -->

  <!-- TODO: an "Ingredients" part with a short paragraph -->

  <!-- TODO: a "Steps" part with "Prepare" and "Cook" subsections -->

  <!-- TODO: a "Related recipes" part using the small class -->
</body>
</html>

Expected: The heading outline reads Coconut Noodle Soup > Ingredients, Steps (Prepare, Cook), Related recipes, and "Related recipes" is an <h2> that renders at roughly body-text size.

Show hint

Write the headings alone first and read them as a table of contents, then fill in the paragraphs. "Related recipes" is a major part, so its level is <h2>; making it small is CSS's job.

Show solution
recipe.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Coconut Noodle Soup</title>
  <style>
    .small-heading {
      font-size: 1rem;
      color: #555;
    }
  </style>
</head>
<body>
  <h1>Coconut Noodle Soup</h1>
  <p>A rich noodle soup ready in about forty minutes.</p>

  <h2>Ingredients</h2>
  <p>Egg noodles, coconut milk, chicken, onion, chickpea flour.</p>

  <h2>Steps</h2>
  <h3>Prepare</h3>
  <p>Slice the onion and cut the chicken into small pieces.</p>
  <h3>Cook</h3>
  <p>Simmer the chicken in coconut milk, then serve over noodles.</p>

  <h2 class="small-heading">Related recipes</h2>
  <p>Fish noodle soup, chicken curry.</p>
</body>
</html>

Quick quiz 1 / 3

A page has an <h1>, and under an <h2> called "Pricing" you want a subheading "Monthly plan". Which level should it be?

Quick quiz 2 / 3

A designer wants the "Comments" title at the bottom of an article to look small. It is a major part of the article. What is the best fix?

Quick quiz 3 / 3

What do correct heading levels mainly give a screen reader user?

What to remember

  • The number in <h1> to <h6> is a rank, not a font size.
  • Use one <h1> for the page's main topic and step down one level at a time.
  • Change size and colour with CSS; never change the level to get a look.
  • If the headings alone read like a good table of contents, the structure is right.

HTML Living Standard — Sections and Semantics — WHATWG

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Headings | Thuta Learning