Sub topic 6
The hero: the first message a visitor sees
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 read the chapters on semantic elements and headings.
What a hero does
A hero is the first large section inside main. It tells a visitor three things within a few seconds: what this site is, why it matters to them, and what to do next. It is not a special HTML element. It is a section that you style and fill with a specific set of parts.
A working hero has four parts:
- One
h1that states the main message. - One short supporting sentence.
- One call to action, which is a link or button that leads to the next step.
- Optionally, one image that supports the message.
The working model
The hero belongs to main, not to header. The site header holds identity and navigation on every page. The hero holds the message of this particular page. Putting it inside main also means it appears in the page outline as the start of the page's content.
The hero usually contains the page's only h1. The site name in the header is a link or logo, not a heading, so the page has exactly one top-level heading. Later sections use h2.
The markup
This hero uses one h1, one sentence, one call to action, and one image.
<section class="hero" aria-labelledby="hero-title">
<div class="hero-text">
<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 class="button" href="/menu">See the menu</a>
</div>
<img src="/images/hero-loaf.jpg" alt="A basket of freshly baked loaves" width="800" height="600" fetchpriority="high">
</section>Some details matter here:
aria-labelledby="hero-title"gives the section an accessible name by pointing at theh1. Asectionwith an accessible name is exposed to screen readers as a region landmark.- The call to action is a link because it navigates to
/menu. Use abuttonelement only when the control performs an action on the current page, such as submitting a form. - The
alttext describes what the image shows. If the image were purely decorative, you would usealt=""instead. widthandheightlet the browser reserve space for the image before it downloads, which prevents the content below from jumping when the image arrives. The image-loading chapter coversfetchpriorityin more detail.
The layout
On a narrow screen, the hero is one column: text first, then the image. On a wide screen, it becomes two columns.
.hero {
display: grid;
gap: 2rem;
padding: 3rem 1.5rem;
}
.hero img {
max-width: 100%;
height: auto;
}
@media (min-width: 48rem) {
.hero {
grid-template-columns: 1fr 1fr;
align-items: center;
}
}The base rules are written for small screens, and the media query adds the two-column layout from 48rem upward. This is the mobile-first approach. max-width: 100% with height: auto stops the image from overflowing its column while keeping its proportions.
Common errors
| Mistake | Why it hurts | Fix |
|---|---|---|
| Several `h1` elements, or an `h1` in the header logo | The page has no single clear main topic | Use one `h1` in the hero |
| Several equal call-to-action buttons | The visitor has no clear next step | Pick one primary action; make others plain links |
| Text placed inside the image | It cannot be resized, selected, or read by a screen reader | Keep the message in HTML text |
| Missing `width` and `height` on the image | The layout shifts when the image loads | Set both attributes and use `height: auto` in CSS |
| Vague `alt` such as "image" | Screen reader users learn nothing | Describe what the image shows |
| Hero placed outside `main` | The page structure is misleading | Make it the first section inside `main` |
Exercise
A hero contains an h1, a paragraph, and a link styled as a button. A teammate wants to add a second h1 for the tagline and a second button labelled "Learn more" next to the first. What would you change instead, and why?
Show answer
Make the tagline the supporting paragraph, so the page keeps one h1 and the heading outline stays clear. Keep one primary call to action. If "Learn more" is needed, make it a plain text link so the visitor sees one obvious next step.
Exercise
The hero image loads slowly, and the text below it jumps down when it appears. Which two attributes on the img fix the jump, and which CSS rule keeps the image from overflowing on small screens?
Show answer
Add width and height attributes so the browser reserves space before the image loads. Use max-width: 100% with height: auto on the image so it scales within its column.