Sub topic 7
Content sections: organizing the main message
This chapter assumes you know basic HTML tags and attributes, basic CSS selectors and properties, and how to inspect a page in your browser's developer tools. It also assumes you have seen the page skeleton from earlier chapters, with its header, main, and footer.
What a content section is
Content sections are the blocks that follow the hero inside main. Each one covers one idea: specialties, pricing, testimonials, a contact prompt. A visitor who scrolls should be able to tell where one idea ends and the next begins.
In HTML, a section element groups content that belongs together under a heading. The hero is itself the first section in main, so content sections continue the same pattern.
The working model: one idea, one section, one heading
A good section has three parts:
- A heading that names the idea.
- Content that supports it, such as paragraphs, lists, or cards.
- Optionally, one next step, such as a link.
The heading matters most. Browsers and assistive technology use it to describe the section. Every section should start with a heading, and its level should fit the outline. The hero holds the page's only h1, so content sections use h2. Parts inside a section use h3.
A worked example
This markup adds two content sections after the hero. The second section holds a grid of cards.
<main id="main">
<section class="hero" aria-labelledby="hero-title">
<h1 id="hero-title">Fresh bread, baked every morning</h1>
<p>Order ahead and pick up warm loaves on your way to work.</p>
<a href="/menu">See the menu</a>
</section>
<section aria-labelledby="specialties-title">
<h2 id="specialties-title">Our specialties</h2>
<p>Sourdough, rye, and seasonal pastries.</p>
</section>
<section aria-labelledby="locations-title">
<h2 id="locations-title">Visit us</h2>
<div class="card-grid">
<article>
<h3>Downtown</h3>
<p>Open 6 a.m. to 6 p.m.</p>
</article>
<article>
<h3>Riverside</h3>
<p>Open 7 a.m. to 4 p.m.</p>
</article>
</div>
</section>
</main>The aria-labelledby attribute points to the heading's id. This gives the section an accessible name, which is the text a screen reader announces for it. A section with an accessible name is exposed as a region landmark. A section without one is not a landmark, so naming sections lets screen reader users jump between them.
The cards use article, because each one could stand on its own. Use div when you need a wrapper only for layout.
Choosing the right element
| Element | Use it when | Example |
|---|---|---|
| `section` | The content is one theme and has a heading | "Our specialties" |
| `article` | The content makes sense on its own | A card, a blog post, a review |
| `aside` | The content is related but not essential | A sidebar tip |
| `div` | You need a wrapper only for styling | A grid container |
Styling the sections
Give sections consistent vertical spacing so the rhythm of the page is predictable. For a grid of cards, let CSS decide how many columns fit.
main > section {
padding: 3rem 1.5rem;
}
.card-grid {
display: grid;
gap: 1.5rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}The minmax(16rem, 1fr) rule asks for columns at least 16rem wide that share the leftover space. On a phone you get one column, and on a wide screen you get several. No media query is needed.
Common errors
- Sections without headings. A
sectionwith no heading has no clear purpose. If you cannot name the idea, use adiv. - Skipping heading levels. Going from
h2toh4becauseh3looks too large breaks the outline. Fix the size with CSS and keep the level correct. - Choosing a heading by its size. Pick the level from the outline, then style it.
- Using
sectionas a generic wrapper. This adds noise for assistive technology. Usedivfor pure layout. - Several sections with the same name. If two sections share a label, screen reader users cannot tell them apart.
Exercise
A page has a section containing a heading "Customer reviews" written as an h4, because the designer wanted smaller text. The previous heading on the page is an h2. What is wrong, and how do you fix it?
Show answer
The heading skips from h2 to h4, which breaks the outline. Change the element to h2 (or h3 if the reviews belong inside the previous h2 section) and make the text smaller with a CSS font-size rule.
Exercise
You add a section with a heading and want screen reader users to find it as a landmark. Which attribute do you add to the section, and what does it reference?
Show answer
Add aria-labelledby to the section, with the id of the section's heading as its value. This gives the section an accessible name, so it is exposed as a region landmark.