Open any textbook and you'll see the same pattern: a chapter title, section headings, sub-section headings, and paragraphs of text under each. Web pages work the same way. In HTML, this structure is built with heading elements (<h1> to <h6>) and the paragraph element (<p>).
These are probably the elements you will use most often. In this chapter we'll learn how to use them correctly, why heading levels matter far more than font size, how the browser handles spaces and line breaks in your source code, and the two small void elements <br> and <hr> that we met briefly in Elements & Attributes.
Heading Elements: <h1> to <h6>
HTML has six heading elements,
<h1>through<h6>. Each one represents a heading for the content that follows it.<h1>is the highest (most important) level and<h6>is the lowest.
<h1>Heading level 1</h1>
<h2>Heading level 2</h2>
<h3>Heading level 3</h3>
<h4>Heading level 4</h4>
<h5>Heading level 5</h5>
<h6>Heading level 6</h6>
Output in Chrome (default styles):
Heading level 1 ← largest, bold
Heading level 2
Heading level 3
Heading level 4 ← same size as normal text, but bold
Heading level 5 ← smaller than normal text
Heading level 6 ← smallest
Every heading is bold, starts on a new line, and has some space above and below. The typical default font sizes, relative to the surrounding text, are:
| Element | Default font size | At a 16px base |
|---|---|---|
h1 | 2em | 32px |
h2 | 1.5em | 24px |
h3 | 1.17em | ≈ 18.7px |
h4 | 1em | 16px |
h5 | 0.83em | ≈ 13.3px |
h6 | 0.67em | ≈ 10.7px |
But here is the most important idea of this chapter: these sizes are just default decoration. With CSS you can make an <h3> look bigger than an <h1>. What a heading level really expresses is its rank in the document's hierarchy.
Headings Describe Structure, Not Size
Think of the table of contents of a textbook. Heading levels build exactly that kind of outline:
<h1>Operating Systems</h1>
<h2>Process Management</h2>
<h3>Process States</h3>
<h3>CPU Scheduling</h3>
<h4>FCFS</h4>
<h4>Round Robin</h4>
<h2>Memory Management</h2>
<h3>Paging</h3>
<h3>Segmentation</h3>
The outline (hierarchy) this creates:
Operating Systems (h1)
├── Process Management (h2)
│ ├── Process States (h3)
│ └── CPU Scheduling (h3)
│ ├── FCFS (h4)
│ └── Round Robin (h4)
└── Memory Management (h2)
├── Paging (h3)
└── Segmentation (h3)
Each heading "owns" the content after it, until the next heading of the same or higher level. So "Round Robin" belongs to "CPU Scheduling", which belongs to "Process Management".
Why does the hierarchy matter?
- Accessibility: Screen reader users very commonly navigate a page by jumping from heading to heading (or by listing all headings), much like sighted users skim bold titles. A logical hierarchy lets them understand the page without reading every word.
- SEO: Search engines use headings to understand what a page and its sections are about.
- Automatic tools: Table-of-contents generators (like the one on this site) and reader modes build their navigation from headings.
- Maintainability: Other developers instantly understand the page's structure.
Rules and Best Practices for Headings
1. Use one <h1> per page
The <h1> is the title of the page's main content — similar to the <title> in the head. Multiple <h1> elements are technically allowed, but the widely accepted best practice (recommended by MDN and many accessibility experts) is to have a single <h1> that describes the page.
2. Don't skip levels when going deeper
<!-- Bad: jumps from h2 straight to h4 -->
<h2>Sorting</h2>
<h4>Merge Sort</h4>
<!-- Good -->
<h2>Sorting</h2>
<h3>Merge Sort</h3>
A screen reader user who hears "heading level 4" right after a level 2 may wonder whether they missed a level-3 section. Note that going back up several levels is perfectly fine — an <h2> after an <h4> simply starts a new major section.
3. Never pick a heading level for its size
If you want small bold text, don't use <h5> just because it looks right — use CSS. If you want a big heading that is logically a sub-section, use <h2> and enlarge it with CSS.
4. Don't use headings for text that isn't a heading
Taglines, highlighted quotes or "big text" are not headings. Making them headings pollutes the outline for screen readers and search engines.
A note on <h1> inside sections (recent change)
Older tutorials (from the HTML5 era) claimed you could use an <h1> in every section and browsers would automatically work out the level from nesting — the so-called "document outline algorithm". No browser or screen reader ever implemented this, and it was removed from the HTML standard in 2022. In 2025, the standard and the major browsers (Firefox, then Chrome) also dropped the old default style that made an <h1> nested inside <article>, <section>, <aside> or <nav> look smaller — so a nested <h1> now looks just as big as the main one. The rule today is simple: choose the heading level explicitly based on the hierarchy. (Sectioning elements are covered in Semantic HTML.)
The Paragraph Element: <p>
The
<p>element represents a paragraph — a block of related text (one idea, one point). Browsers display each paragraph on its own line, with vertical space (by default, a margin of 1em) above and below it.
<h2>What is a Stack?</h2>
<p>A stack is a linear data structure that follows the LIFO principle.</p>
<p>The two main operations are push and pop. Both run in O(1) time.</p>
Output:
What is a Stack?
A stack is a linear data structure that follows the LIFO principle.
The two main operations are push and pop. Both run in O(1) time.
Use a new <p> for each new idea, just as you would in an essay. Long text with no paragraph breaks is hard to read on screens.
How Browsers Treat Whitespace
This surprises almost every beginner. Look at this code:
<p>This sentence has many spaces
and a line break in the source, but the browser
shows it on one line with single spaces.</p>
Output:
This sentence has many spaces and a line break in the source, but the browser shows it on one line with single spaces.
Whitespace collapsing: In normal text, the browser treats any sequence of spaces, tabs and line breaks as a single space. Whitespace at the beginning and end of a line is removed. The text then wraps automatically to fit the width of the window.
Why does HTML work this way? Because it lets you indent and format your source code neatly without affecting what the reader sees. The visual layout is supposed to come from elements and CSS, not from how you typed the file.
Consequences:
- Pressing Enter in your source does not create a new line on the page.
- Typing ten spaces does not create a wide gap.
- Blank lines between paragraphs in your code do not add extra space on the page.
When you do need whitespace preserved exactly — for example, to show program code or ASCII art — there is the <pre> (preformatted text) element, which we cover in Text Formatting. CSS can also control whitespace with the white-space property.
Line Breaks: <br>
The
<br>void element produces a line break inside text. Use it only where the line break is part of the content, such as in poems, song lyrics or postal addresses.
<p>Roses are red,<br>
Violets are blue,<br>
HTML is easy,<br>
And so are you.</p>
Output:
Roses are red,
Violets are blue,
HTML is easy,
And so are you.
Another good use — an address, where each line has meaning:
<p>Indian Institute of Technology Bombay<br>
Powai, Mumbai<br>
Maharashtra 400076</p>
When NOT to use <br>
<!-- Bad: using br to separate paragraphs -->
<p>First idea.<br><br>Second idea.</p>
<!-- Good: two separate paragraphs -->
<p>First idea.</p>
<p>Second idea.</p>
Stacking <br><br><br> to create vertical space is a common bad habit. Screen readers may announce these as blank lines, and the spacing can't be adjusted consistently. Use separate elements for structure and CSS margin for spacing.
Thematic Breaks: <hr>
The
<hr>void element represents a thematic break between paragraph-level content — a change of topic or scene within a section. Browsers draw it as a horizontal line by default.
<p>Chapter 1 ends with the hero leaving the village.</p>
<hr>
<p>Ten years later, a stranger arrives in the city.</p>
Output:
Chapter 1 ends with the hero leaving the village.
────────────────────────────────────────────────────
Ten years later, a stranger arrives in the city.
Notice the name: historically "horizontal rule", but in modern HTML its meaning is a thematic break. If you only want a decorative line, a CSS border is the better tool. Old attributes like size, width, noshade and align on <hr> are obsolete — style it with CSS instead.
A Complete Worked Example
Let's build a small notes page that uses everything from this chapter:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Computer Networks – Unit 1 Notes</title>
</head>
<body>
<h1>Computer Networks: Unit 1</h1>
<p>These notes cover the basics of networks and network models.</p>
<h2>Types of Networks</h2>
<p>Networks are classified by the area they cover.</p>
<h3>LAN</h3>
<p>A Local Area Network covers a small area such as a lab or a building.</p>
<h3>WAN</h3>
<p>A Wide Area Network covers a large geographical area. The Internet is the largest WAN.</p>
<h2>Network Models</h2>
<p>The OSI model has 7 layers, while the TCP/IP model is usually described with 4 layers.</p>
<hr>
<p>Prepared by: CSE Department<br>
Semester 5, 2026</p>
</body>
</html>
Output:
Computer Networks: Unit 1 (h1)
These notes cover the basics of networks and network models.
Types of Networks (h2)
Networks are classified by the area they cover.
LAN (h3)
A Local Area Network covers a small area such as a lab or a building.
WAN (h3)
A Wide Area Network covers a large geographical area. The Internet is the largest WAN.
Network Models (h2)
The OSI model has 7 layers, while the TCP/IP model is usually described with 4 layers.
──────────────────────────────────────────────────────────────────
Prepared by: CSE Department
Semester 5, 2026
Its heading outline is clean: one h1, two h2 sections, and h3 sub-sections only under an h2, with no skipped levels.
A Hidden Trap: Paragraphs Can't Contain Block Elements
A paragraph may contain only phrasing content — text and inline-level elements like <strong>, <em>, <a>, <br>. It must not contain headings, lists, other paragraphs or <div>s. What happens if you try?
<p>Intro<div>Box</div></p>
We loaded this in Chrome. The DOM it built:
<p>Intro</p><div>Box</div><p></p>
When the parser saw <div>, it automatically closed the paragraph. Then the stray </p> at the end had no open paragraph to close, so the parser created a new, empty paragraph. You end up with three elements instead of one — which breaks CSS rules and JavaScript that expect the div to be inside the paragraph. The same happens if you put a heading or a list inside <p>. (Block vs inline elements are explained fully in Block vs Inline Elements: div & span.)
Common Mistakes
| Mistake | Problem | Better approach |
|---|---|---|
Choosing <h4> because "h2 looks too big" | Breaks the outline | Use the correct level; change size with CSS |
Skipping from h1 to h3 | Confusing for screen reader navigation | Go one level deeper at a time |
Several <h1>s for every big title | Page purpose becomes unclear | One h1, then h2s |
Bold paragraph used as a heading: <p><strong>Introduction</strong></p> | Looks like a heading but isn't one to assistive technology | <h2>Introduction</h2> |
<br><br> or empty <p></p> for spacing | Fake structure, inconsistent spacing | CSS margin |
| Expecting Enter or extra spaces in the source to show | Whitespace collapses | Use separate elements, <br> where meaningful, or <pre> |
<div>, list or heading inside <p> | Parser closes the paragraph early | Put block elements outside paragraphs |
Using <hr> only as decoration | It means "topic change" | Use a CSS border for decoration |
Interview / Exam Questions
1. How many heading levels does HTML have? Which is the most important?
Six: <h1> to <h6>. <h1> is the highest level (most important) and <h6> the lowest.
2. Can a page have more than one <h1>? Is it a good idea?
It is allowed by the standard, but the recommended practice is a single <h1> that describes the page's main content, with <h2>–<h6> for sub-sections. Do not rely on the old "document outline algorithm" — it was never implemented and has been removed from the standard.
3. Why shouldn't you choose a heading level based on its font size?
Because the level communicates the document's hierarchy to screen readers, search engines and tools. Visual size is a presentational concern and should be set with CSS.
4. What is whitespace collapsing?
In normal HTML text, any run of spaces, tabs and newlines is rendered as a single space, and leading/trailing whitespace on lines is removed. That's why formatting your source code doesn't change the rendered layout.
5. What is the difference between <br> and <p>?
<br> is a void element that inserts a line break inside a block of text (for poems, addresses). <p> is a container that represents a whole paragraph and has spacing around it. Use separate <p> elements for separate ideas, not <br><br>.
6. What does <hr> represent in modern HTML?
A thematic break — a shift of topic between paragraph-level content. Browsers display it as a horizontal line by default, but its meaning is semantic.
7. What DOM is produced by <p>Intro<div>Box</div></p>?
<p>Intro</p><div>Box</div><p></p>. The <div> start tag implicitly closes the open paragraph, and the stray </p> creates an empty paragraph.
Summary
<h1>–<h6>define a hierarchy of headings; their default sizes are just decoration.- Use one
<h1>per page, don't skip levels when going deeper, and never choose a level for its size. <p>represents a paragraph — one idea per paragraph.- Browsers collapse whitespace: extra spaces and newlines in the source become a single space.
<br>is for meaningful line breaks (poems, addresses), not for spacing.<hr>is a thematic break; use CSS for decorative lines.- Paragraphs can contain only phrasing content; a block element inside
<p>makes the parser close the paragraph early.
In the next chapter, Text Formatting, we'll learn how to mark up words inside paragraphs — importance, emphasis, code, quotations and more.