In the last lesson you saw one heading, a title on the page, written with <h1>. But a long page, like a recipe or an article, has more than one title: the page title, a title for each section, and sometimes a smaller title inside that section.
For that, HTML has six heading levels (a heading is a title), from <h1> to <h6>. The number in the tag is the level: the smaller the number, the more important the title. <h1> is for the page title, <h2> for a section title, <h3> for a title inside that section, and so on.
Writing one heading
You write a heading like the elements you already know: an opening tag, the text, a closing tag.
<h1>Mrs. Sri's Stall</h1>
<h1>: the opening tag. It means "a level 1 heading starts here".Mrs. Sri's Stall: the text that shows.</h1>: the closing tag. The number must match the opening tag:<h1>closes with</h1>, not</h2>.
In the browser, Mrs. Sri's Stall shows big and bold.
In the exercises, the editor (the box where you write code) sometimes already holds an empty pair of tags, like <h1></h1>. That means the tags are ready, and you only need to fill them in. Put the cursor between > and </, then type the text. You don't need to write <h1> again.
The text an exercise asks for must match exactly, including capital letters and punctuation. To the checker (the program that checks your answer), Fun and fun are two different texts.
Arrange headings like a table of contents
Here are the headings of a recipe page:
<h1>Fried Rice Recipe</h1>
<h2>Ingredients</h2>
<h3>Spice paste</h3>
<h2>Method</h2>
Read them like a book's table of contents:
<h1>is the book's title: Fried Rice Recipe.<h2>are the chapters: Ingredients and Method. Both are sections of the recipe, so they have the same level.<h3>is a part of the chapter above it: Spice paste belongs to Ingredients because it comes after it. Headings don't wrap each other, so all four are simply written one after another.
In the browser all four headings show in bold. <h1> is the biggest, <h2> is smaller, and <h3> is smaller again.
A heading inside a section uses the next number: in an <h2> section, use <h3>, not <h4>.
A page should have only one <h1>. Two kinds of reader depend on headings: Google and screen readers (programs that read pages aloud for blind users). Both use the <h1> to learn the page's title. Google uses the other headings to understand what each section is about. Screen reader users jump from heading to heading to find the section they want, like choosing a chapter from a table of contents. That is why the way you arrange headings matters for two things:
- SEO: so search engines like Google can find your page easily.
- Accessibility: so everyone can use your page, including screen reader users.
In the third exercise, the editor holds the line <!-- Write 3 lines of headings here -->. It's just a note that doesn't show on the page. It is called a comment, and it has its own lesson later. Write your headings on new lines below it, not inside that note.
The level is about importance, not size
Browsers do show <h1> biggest and <h6> smallest, but that's only their default look. CSS can change the size at any time. What doesn't change is the heading's place in the table of contents: page title, section title, or a title inside a section. An <h3> made bigger is still an <h3>.
So choose the level by the heading's place in the table of contents, not because you want big text.
⚠️ What usually goes wrong with headings
- Opening and closing numbers that differ. With
<h1>Main Title</h2>, the browser has to guess which heading you closed. - A heading just to get big text. If the text isn't a section title, use
<p>and make it bigger with CSS. - Skipping a level. Going from
<h2>straight to<h4>leaves one level missing from the table of contents, so it looks like a section is lost. - Two
<h1>s. The page now has two main titles, and readers can't tell which one is the real title.
TL;DR: A heading is a title, and there are six levels, from <h1> to <h6>: the smaller the number, the more important the title. Write the opening tag, the text, then the closing tag with the same number. Arrange headings like a book's table of contents: one <h1> for the page title, <h2> for sections, <h3> for parts of a section, no skipped levels. Choose the level from the heading's place in the table of contents, not from its size. Font size is CSS's job.
⚡ Six levels of heading
Write:
<h1>Fried Rice Recipe</h1>
<h2>Ingredients</h2>
<h3>Spice paste</h3>
<h2>Method</h2>
Meaning: Headings (titles) have six levels, <h1> to <h6>: the smaller the number, the more important the title. Arrange them like a book's table of contents: <h1> for the page title (one is enough), <h2> for chapters, <h3> for parts of the chapter above. Here, Spice paste is a part of Ingredients, so it is an <h3>, not an <h4>. Google and screen readers (programs that read pages aloud) read this order.
⚡ Close with the same number
Guess first: what's wrong with <h1>Main Title</h2>?
The numbers differ. <h1> must be closed with </h1>. Otherwise the browser has to guess which heading you closed.
Meaning: The number in the closing tag always matches the opening tag: <h2> is closed with </h2>.
In the exercises, when the editor (the box where you write code) already holds <h1></h1>, type the text between > and </. The text must match exactly, including capital letters.
⚡ The level is about importance, not size
Guess first: an <h3> made bigger with CSS: what level is it now?
Still <h3>. Its size can change, but its place in the table of contents can't.
Meaning: Browsers show <h1> biggest and <h6> smallest, but CSS can change that size. So choose the level from the heading's place in the table of contents, not from the size you want.