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.
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.
<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.
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.
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.
<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.
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
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.
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 โ