The heading hierarchy: h1 to h6

HTML has six heading levels: <h1>, <h2>, <h3>, <h4>, <h5>, and <h6>. Each represents a different level of importance and size.

  • <h1> โ€” The most important. The main title of the page. Use only one per page.
  • <h2> โ€” Major sections within the page.
  • <h3> โ€” Subsections under h2 sections.
  • <h4>, <h5>, <h6> โ€” Increasingly specific subdivisions. Rarely used on real pages.
headings.html All six heading levels
Golden rule:
Don't skip heading levels. Go h1 โ†’ h2 โ†’ h3, not h1 โ†’ h3. This maintains semantic structure for browsers and screen readers.

Why heading hierarchy matters

Proper heading structure isn't just about visual appearance โ€” it's critical for two reasons:

1. SEO (Search Engine Optimization)

Google and other search engines use headings to understand your page. The <h1> tells search engines what the page is about. Clear heading hierarchy helps your page rank better in search results.

2. Accessibility

People using screen readers navigate pages by jumping between headings. A clear structure lets them understand the page layout instantly. Skipping levels (h1 โ†’ h3) confuses both humans and assistive technology.

Pro tip:
Always use exactly one <h1> per page. It's the page's primary topic. If you have multiple topics, use <h2> sections instead.

Paragraphs and whitespace handling

The <p> tag marks up a paragraph. But here's something tricky: HTML browsers collapse whitespace.

This means multiple spaces, tabs, and line breaks all become a single space. The browser ignores extra whitespace in your HTML code.

whitespace.html How browsers handle whitespace in paragraphs

This "whitespace collapsing" is by design. It frees developers to format HTML code readably without affecting how it renders. But sometimes you need to preserve spaces and line breaks โ€” that's where <pre> comes in.

Preformatted text with <pre>

The <pre> tag (preformatted) preserves whitespace and line breaks exactly as written. Use it for code snippets, ASCII art, or any text where spacing matters.

โœ๏ธ Try It Yourself โ€” Whitespace Demo
Preview

Notice how the paragraph collapses the extra spaces, but the <pre> block preserves exact formatting. This makes <pre> perfect for showing code examples.

Line breaks: <br> vs paragraphs

Sometimes you need a single line break without starting a new paragraph. That's what <br> does. It's a self-closing tag โ€” no closing tag needed.

linebreaks.html When to use <br> vs <p>
Best practice:
Use <p> for separate ideas or paragraphs. Use <br> only for breaks within the same logical block (addresses, poetry, etc.). Don't overuse <br> for spacing โ€” that's CSS's job.

Try it yourself: Build a blog post

Let's put it all together. Build a simple blog post with proper heading hierarchy, paragraphs, and a code block.

โœ๏ธ Try It Yourself โ€” Blog Structure
Preview

Observe the structure: one <h1>, multiple <h2> sections, <h3> subsections, and a <pre> block with code. This is semantic HTML.

Common mistakes

  • Multiple <h1> tags per page โ€” Confuses search engines about your page's main topic. Use only one.
  • Skipping heading levels โ€” Don't jump from h1 to h3. Screen readers expect h1 โ†’ h2 โ†’ h3. Breaks accessibility.
  • Using headings for styling โ€” Don't use <h2> just because you want larger text. Use CSS for styling. Use headings only for structure.
  • Overusing <br> for spacing โ€” <br> is for line breaks, not gaps between sections. Use CSS margins and padding instead.
  • Forgetting whitespace matters in <pre> โ€” If formatting matters, use <pre>. Otherwise, HTML collapses it.

Key takeaways

  • Six heading levels exist โ€” h1 (most important) through h6 (least important).
  • Use one h1 per page โ€” It's your page's primary topic. Good for SEO and accessibility.
  • Don't skip heading levels โ€” Go h1 โ†’ h2 โ†’ h3, not h1 โ†’ h3. Maintains semantic structure.
  • HTML collapses whitespace in paragraphs โ€” Multiple spaces, tabs, and line breaks all become single spaces.
  • Use <pre> for code and formatted text โ€” It preserves exact whitespace and line breaks.
  • <br> is for single breaks โ€” Not for spacing. Use CSS for gaps between sections.

Test your knowledge

Question 1 of 5
How many heading levels does HTML have?
Question 2 of 5
Which heading is the most important and should appear only once per page?
Question 3 of 5
What does the <pre> tag do?
Question 4 of 5
How many <h1> tags should a page have?
Question 5 of 5
What happens to extra spaces inside a <p> tag?
0
/5
Great work! You understand HTML structure.
Challenge: Build a Blog Post

Task: Create a blog post with proper structure. Include:

  • One <h1> as the main title
  • Two <h2> sections (e.g., "Introduction" and "Main Topic")
  • At least two <h3> subsections under one of the h2 sections
  • Multiple paragraphs with proper text
  • A <pre> block with a code snippet

Use the Try It Yourself editor above to build it. Click Run to preview!

Hint: Start with h1, then h2 sections. Add h3 subsections under one h2. Fill with paragraphs. Add a pre block at the end with a simple code example inside it.

Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered semantic HTML.
Ready to build real websites.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’