Sub topic 10
Make the hero work from phone to desktop
A hero that reads well on a laptop can fall apart on a phone. The headline wraps into six lines, the button falls below the first screen, and the image pushes everything down. This chapter covers the working model for a responsive hero: how the layout changes with width, which content moves, and which mistakes break the offer on small screens.
Prerequisites
This chapter assumes you know basic HTML and CSS, including the box model and @media rules. It also assumes you have the hero elements from earlier chapters: a headline, a subheadline, one primary call to action (CTA) and a supporting visual. CSS Grid is used, but you only need the basics.
The working model: content first, then widths
Responsive design for a hero rests on one rule. The visitor must still read the offer and see the CTA without scrolling, whatever the screen size. Layout is the tool you use to keep that true.
The usual approach is mobile first:
- Write the CSS for the narrowest screen with no media query. This is a single column.
- Add
min-widthmedia queries that enhance the layout as space grows. - Test at the widths where the design breaks, not at the widths of specific devices.
Mobile first has a practical benefit. A phone downloads only the rules it needs to start, and the default state is the simplest one.
Start with the single-column default
The hero from the earlier chapter already follows this model. Here is the base CSS again, without the media query:
.hero {
display: grid;
gap: 2rem;
align-items: center;
padding: 3rem 1.25rem;
max-width: 72rem;
margin-inline: auto;
}
.hero h1 {
font-size: clamp(2rem, 5vw, 3.5rem);
line-height: 1.1;
margin: 0 0 1rem;
}
.hero__sub {
font-size: 1.125rem;
max-width: 40ch;
margin: 0 0 1.5rem;
}
.hero__cta {
display: inline-block;
padding: 0.875rem 1.5rem;
background: #0b5fff;
color: #fff;
border-radius: 0.5rem;
font-weight: 600;
text-decoration: none;
}
.hero__image {
width: 100%;
height: auto;
}With display: grid and no column template, the grid has one column. Items stack in source order: the text block first, then the image. That order matches the visual hierarchy you want on a phone. The offer comes first and the image follows.
Three details deserve attention.
padding: 3rem 1.25remgives the text side margins so it never touches the screen edge. Without it, text on a phone sits flush against the glass.max-width: 72remwithmargin-inline: autostops the hero from stretching across a very wide monitor, and centers it.width: 100%; height: autoon the image makes it shrink to fit its container. Because the HTML also setswidthandheightattributes, the browser knows the aspect ratio before the image loads and reserves the space.
Scale the headline with clamp()
Fixed font sizes force a choice. A size that suits a desktop is too large on a phone, and the reverse is also true. The clamp() function takes a minimum, a preferred value and a maximum:
font-size: clamp(2rem, 5vw, 3.5rem);Here the headline is never smaller than 2rem and never larger than 3.5rem. In between, it is 5vw, which means 5% of the viewport width. At 400px wide, 5vw is 20px, so the minimum of 32px (2rem at the default size) applies. At 1000px, 5vw is 50px, which falls inside the range. At 1400px, 5vw is 70px, so the maximum of 56px applies.
The minimum matters most. A hero headline of two to three lines on a 360px phone is acceptable. Five lines is a sign that the headline is too long or the minimum is too big.
Add the two-column layout at a breakpoint
A breakpoint is a viewport width where the layout changes. The earlier chapter's CSS uses one:
@media (min-width: 48rem) {
.hero {
grid-template-columns: 1fr 1fr;
}
}48rem is 768px at the default font size. Using rem instead of px in the media query means the breakpoint also moves when a user raises their default font size. Someone with large text then gets the single-column layout at a wider screen, which is usually what they need.
Choose breakpoints by looking at your own design. Drag the browser window narrower and wider and note where something goes wrong: lines become too long, the image dwarfs the text, or the CTA drops out of view. Put the breakpoint there.
1fr 1fr gives two equal columns. If the text needs more room, use a ratio such as 1.2fr 1fr. Check that the headline stays within three lines in the text column.
Control the order of elements
On a phone, you may want the image to appear above or below the text. The source order in the HTML decides the default, and for the hero that order should put the text first. Screen reader users and keyboard users move through the page in source order, so the offer and CTA should come before the image there too.
If a design calls for the image on the left at desktop widths, change the visual order only with CSS:
@media (min-width: 48rem) {
.hero__image {
order: -1;
}
}This is safe for the layout, but be aware that focus order and visual order can then disagree. The hero has one focusable element, the CTA, so the risk is small here. In a hero with several links, avoid reordering.
Keep the CTA in view on small screens
The phone's first screen is short. A common phone viewport is around 360 by 640 CSS pixels, and the browser's address bar takes part of it. Add up the vertical space the hero uses before the CTA:
| Element | Approximate height on a phone |
|---|---|
| Top padding | 48px |
| Headline, three lines at 32px | 105px |
| Subheadline, four lines | 110px |
| CTA button | 50px |
| Sticky header | 56px |
The total is about 370px, which fits within the first screen with room to spare. If your subheadline runs to eight lines, the CTA moves down and the image arrives before the button. Shorten the copy, reduce padding or tighten the line height.
A useful test is to open the page at 360px width in your browser's device tools and check that the CTA is visible without scrolling. Repeat at 320px, which is the narrowest width worth supporting.
Handle the image on small screens
The image is the biggest layout risk on a phone. There are three common choices:
- Stack it below the text. This is the default above. The CTA stays visible and the image appears as the visitor scrolls.
- Crop it. Use
aspect-ratioandobject-fit: coverto give the image a shorter shape on narrow screens. - Hide it. Use this only when the image is purely decorative. If it shows the product, hiding it removes evidence for the offer.
Here is a cropped version:
@media (max-width: 47.99rem) {
.hero__image {
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: top left;
}
}This crops a 640 by 400 screenshot to a wider, shorter shape. object-position decides which part stays visible. For an editor screenshot, the top left usually holds the most recognisable part of the interface.
Avoid display: none on an img to save space. The browser may still download the file, so you pay for it in loading time without showing it. If a different picture is right for phones, use <picture> with source elements so the browser downloads only one.
Common errors
| Error | What happens | Fix |
|---|---|---|
| Missing viewport meta tag | The phone renders the page at about 980px wide and shrinks it, so the layout looks like a small desktop page | Add `<meta name="viewport" content="width=device-width, initial-scale=1">` to the `head` |
| Fixed pixel widths on text blocks | Horizontal scrolling appears on narrow screens | Use `max-width` and relative units |
| `height: 100vh` on the hero | On phones the address bar changes the viewport height, so the CTA can be pushed off screen or the page jumps | Use `min-height` and, if needed, `svh` units: `min-height: 80svh` |
| Breakpoints at device widths only | Tablets in between or resized windows look broken | Test across a continuous range of widths |
| Very long headline words | A long word overflows its column | Add `overflow-wrap: break-word` to the headline, and shorten the copy |
| Tiny tap target | The CTA is hard to press with a thumb | Keep the button at least 44px tall; the example's padding gives about 50px |
The viewport meta tag is the error that causes the most confusion. Without it, none of your media queries behave as you expect on a real phone, even though they work in a narrowed desktop window.
Check your work
A quick routine for each hero change:
- Open device tools and set the width to 320px, then 360px, 768px and 1280px.
- At each width, confirm the headline, subheadline and CTA are visible, and the headline takes no more than three or four lines.
- Zoom the browser to 200% and confirm nothing overlaps or scrolls sideways.
- Confirm the image does not cause layout shift as it loads.
Exercise
A hero uses font-size: clamp(2rem, 5vw, 3.5rem). The browser default is 16px. What is the headline size at a viewport width of 500px, and at a width of 1600px?
Show answer
At 500px, 5vw is 25px, which is below the 32px minimum (2rem), so the headline is 32px. At 1600px, 5vw is 80px, which is above the 56px maximum (3.5rem), so the headline is 56px.
Exercise
A phone visitor reports that the hero button only appears after scrolling past a large image. The HTML puts the image before the text block. Name the problem and give two ways to fix it.
Show answer
The source order places the image above the offer, so on a single-column layout it pushes the CTA down the page. Fix it by moving the text block before the image in the HTML, so the default stack shows the offer first. Also reduce the image's height on narrow screens with aspect-ratio and object-fit: cover, or by shortening the copy and padding.