Sub topic 6
The hero: the first message a visitor sees
What the hero is and what you should assume
The hero is the first large block of content inside <main>. It sits directly under the header, and it is usually the first thing a visitor reads. Its job is to answer three questions in a few seconds: where am I, what does this offer, and what should I do next.
This chapter assumes you already know basic HTML tags and attributes, basic CSS selectors and properties, and how to open a page and use your browser's developer tools. It builds on the earlier chapters on semantic elements, the heading outline, and the header with its navigation.
HTML has no <hero> element. A hero is a pattern, built from ordinary elements. By convention it is a <section> with a class such as hero. Because it is a pattern, you have to supply the structure and the accessibility details yourself.
The parts of a hero
A well-formed hero has four parts:
- One
<h1>that states the main message of the page. - One short supporting paragraph that adds detail.
- One call to action (CTA), usually a link styled as a button.
- Optionally, an image that reinforces the message.
The restraint matters. A hero with three headings, four buttons, and a long paragraph gives the visitor nothing to focus on.
<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>The sections below explain each decision in this markup.
Where the hero sits in the page
The hero is the first <section> inside <main>. It is not inside <header>. Beginners often put the large banner in the header because it appears visually near the top. Semantically, though, the header holds the site identity and navigation, and the hero is page content. If you put the <h1> inside the site header, the header's role as a repeated, site-wide element becomes muddled, because the <h1> is specific to this page.
<body>
<header>
<a href="/">Acme Bakery</a>
<nav aria-label="Primary">...</nav>
</header>
<main>
<section class="hero" aria-labelledby="hero-title">
...
</section>
<section>
<h2>Our specialties</h2>
...
</section>
</main>
</body>One h1, and why it belongs to the hero
The hero heading is normally the page's only <h1>. The heading outline you built in the previous chapter relies on this. Screen reader users can list a page's headings and jump to the <h1> to learn what the page is about. A page with several <h1> elements, or none, weakens that shortcut.
Two common mistakes follow from this:
- Using the site name in the header as an
<h1>on every page. The site name is the same everywhere, so it tells the visitor nothing about the current page. Use a plain link for the site name, as the examples here do. - Choosing a heading level for its size. If the hero title looks too large or too small, change it with CSS. Never switch to
<h3>or<h2>to change the look.
The sections after the hero then use <h2>, which keeps the outline in order.
Linking the section to its heading
The example uses aria-labelledby="hero-title", which points to the id of the <h1>. This gives the <section> an accessible name. A <section> element is exposed to assistive technology as a region landmark only when it has an accessible name. Without one, it is treated as a generic grouping and does not appear in the landmark list.
Is the hero worth listing as a landmark? Many teams say yes, because it lets a screen reader user jump straight to it. Others leave it unnamed to avoid crowding the landmark list, especially on pages with many sections. Both choices are valid. What matters is that you decide on purpose, and that you do not label every section by reflex.
The call to action
The CTA is the one action you most want the visitor to take. Keep these points in mind:
- Use a link when it navigates, a button when it acts. "See the menu" goes to another page, so it is an
<a href>. A control that submits a form or opens a dialog should be a<button>. Styling does not change which one is correct. - Use one primary CTA. A second, quieter link is acceptable, but two equally loud buttons split attention.
- Write link text that makes sense out of context. Screen reader users often list all links on a page. "Click here" is useless in that list. "See the menu" works.
A minimal button style for the link:
.button {
display: inline-block;
padding: 0.75rem 1.5rem;
background: #1a4d8f;
color: #fff;
text-decoration: none;
border-radius: 0.25rem;
}
.button:focus-visible {
outline: 3px solid #ffbf47;
outline-offset: 2px;
}The :focus-visible rule gives keyboard users a clear indicator. Do not remove the focus outline without providing a replacement. Also check that the text color against the background meets a contrast ratio of at least 4.5:1 for normal-size text.
Laying out the hero
On a narrow screen, the hero should be a single column: text first, then the image. On a wide screen, it can become two columns. CSS grid handles both with one media query.
.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;
}
}This is mobile-first. The base rules describe the narrow layout, and the media query adds the two-column arrangement only when the viewport is at least 48rem wide (about 768px at default font size). Using rem instead of px in the query means the breakpoint also responds when users enlarge their default font size.
Two details are easy to miss:
max-width: 100%together withheight: autostops the image from overflowing its column and keeps its proportions.align-items: centervertically centers the text against the image. Without it, grid items stretch to equal height, and the text sits at the top.
Order in the source and order on screen
In the markup, the text comes before the image. This is deliberate. Screen reader users and keyboard users move through the page in source order, so the <h1> and the CTA should come first. If you want the image on the left in the wide layout, do not reorder the HTML. You can use the order property on a grid child instead, but understand that this changes the visual order only. The keyboard focus order still follows the source. In a hero with just one link, the mismatch is harmless. In a more complex block, it can confuse keyboard users.
The hero image and page speed
The hero image is often the largest visible element on first load, so it strongly affects perceived speed. Three attributes in the example matter:
| Attribute | What it does |
|---|---|
| `width` and `height` | The browser computes the aspect ratio before the file arrives and reserves space, so the text below does not jump when the image loads. |
| `fetchpriority="high"` | Tells the browser this image is important and should be fetched early. |
| `alt` | Describes the image for screen reader users and appears if the image fails to load. |
Do not add loading="lazy" to the hero image. Lazy loading delays images until they are near the viewport. The hero is already in the viewport, so lazy loading only makes it arrive later. Reserve lazy loading for images further down the page.
Also write alt text carefully. If the image is purely decorative and repeats nothing the text does not already say, use an empty alt="" so screen readers skip it. If the image carries meaning, as a photo of the product might, describe what it shows in a short phrase. Do not start with "Image of" because the screen reader already announces that it is an image.
Background images: a trade-off
Many heroes use a CSS background-image with text on top. This is common, but it has costs:
- A background image has no
alttext, so it cannot convey meaning. - You cannot set
fetchpriorityor intrinsicwidthandheight, so you lose control over loading and layout shift. - Text over a photo can fail contrast checks in some areas of the picture.
If the picture matters, use an <img>. If it is purely atmospheric, a background is acceptable, provided you set a solid background-color fallback and verify the text contrast.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Layout jumps when the image loads | Missing `width` and `height` | Add both attributes and keep `height: auto` in CSS |
| Image overflows on phones | No `max-width: 100%` | Add `max-width: 100%; height: auto;` |
| Screen reader lists two `<h1>` headings | Site name in the header is also an `<h1>` | Make the site name a link, keep one `<h1>` in the hero |
| Hero is not in the landmark list | `<section>` has no accessible name | Add `aria-labelledby` pointing to the heading id |
| Hero image loads late | `loading="lazy"` on the hero image | Remove it and add `fetchpriority="high"` |
| Button text hard to read | Low contrast | Pick colors with at least 4.5:1 contrast |
Checking your work
Open the page in your browser and use the developer tools:
- In the accessibility panel, find the hero and confirm it has the name you expect.
- Resize the window across the
48rembreakpoint and watch the columns change. - Throttle the network in the Network panel and reload. Confirm the layout does not jump when the image arrives.
- Press Tab from the top of the page. Focus should reach the CTA in a sensible order and be clearly visible.
Exercise
A teammate's hero puts <h1>Acme Bakery</h1> in the header, adds <h1>Fresh bread, baked every morning</h1> in the hero, and sets loading="lazy" on the hero image. The image has no width or height. List the three problems and give the fix for each.
Show answer
First, there are two <h1> elements. Make the site name a plain link in the header and keep the single <h1> in the hero, so the outline has one top-level heading that describes the page. Second, loading="lazy" delays an image that is already in the viewport. Remove it and add fetchpriority="high". Third, the missing width and height mean the browser cannot reserve space, so content shifts when the image loads. Add both attributes matching the image's intrinsic size, and keep max-width: 100%; height: auto; in the CSS.
Exercise
You want the hero image to appear on the left on wide screens, while the text stays first in the HTML. What CSS property can do this, and what is the accessibility cost?
Show answer
Set order: -1 on the image (or place the items explicitly with grid placement) inside the @media (min-width: 48rem) rule. This changes only the visual order. Keyboard focus and screen reader reading order still follow the source, so if the moved element contained links, the focus would appear to jump around. For an image with no links, the cost is negligible.