Sub topic 10
Make the hero work from phone to desktop
This chapter assumes you know basic HTML and CSS, including media queries and CSS Grid. It builds on the hero markup from earlier chapters: one h1, a subheadline, one call to action (CTA) link, and a product image.
Start with the narrow screen
Responsive behavior means the hero keeps stating the offer at every screen width. The safest approach is mobile first. Write the layout for a phone, then add rules for wider screens with min-width media queries.
On a phone, a single column works well. The reading order is the same as the source order: headline, subheadline, CTA, note, then image. The offer appears first and the visitor does not need to scroll to find the button.
The base layout
The base rules need no media query. A grid with one column stacks the children. The gap property sets the space between them.
.hero {
display: grid;
gap: 2rem;
align-items: center;
padding: 3rem 1.25rem;
max-width: 72rem;
margin-inline: auto;
}
.hero__image {
width: 100%;
height: auto;
}Setting width: 100% and height: auto on the image lets it shrink to fit the column. The width and height attributes in the HTML still give the browser the aspect ratio. It reserves the right amount of space before the image loads, so the text does not jump.
Scale the headline with clamp()
A fixed headline size is too large on a phone or too small on a wide monitor. The clamp() function takes a minimum, a preferred value and a maximum.
.hero h1 {
font-size: clamp(2rem, 5vw, 3.5rem);
line-height: 1.1;
margin: 0 0 1rem;
}Here the size is 5% of the viewport width, but never below 2rem and never above 3.5rem. Use rem for the limits so the text still respects the user's browser font setting.
Keep line length in check as well. The rule max-width: 40ch on the subheadline limits it to about 40 characters per line, which keeps it readable on wide screens.
Add the two-column layout
At wider widths there is room for the text and the image side by side. One media query changes the grid.
@media (min-width: 48rem) {
.hero {
grid-template-columns: 1fr 1fr;
}
}48rem is 768px at the default font size. Using rem for breakpoints means the layout also switches when a user enlarges their text. Choose a breakpoint where the content starts to look cramped or stretched, not one tied to a specific device.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Page scrolls sideways on a phone | Image or element has a fixed width larger than the screen | Use `width: 100%` and `height: auto` |
| Page looks zoomed out on a phone | Missing viewport meta tag | Add the meta tag shown below |
| Image pushes the CTA below the first screen | Image placed before the text in the source | Keep the text first in the HTML |
| Layout jumps while loading | Image has no `width` and `height` attributes | Add both attributes |
| Button is hard to tap | Padding is too small | Keep roughly 44px of tappable height |
The viewport meta tag goes in the head of the page. Without it, mobile browsers pretend the screen is about 980px wide and shrink everything.
<meta name="viewport" content="width=device-width, initial-scale=1">Check the result
Open the hero at 360px wide. You should see the headline, subheadline and button without scrolling far. At 1024px you should see two balanced columns. At 1400px the content should stay centered at 72rem and not stretch to the edges.
Exercise
Your hero looks fine on a laptop, but on a phone the headline is huge and the image causes sideways scrolling. Name one CSS change for each problem.
Show answer
For the headline, use clamp(), for example font-size: clamp(2rem, 5vw, 3.5rem), so the size has a lower and upper limit. For the image, set width: 100% and height: auto so it shrinks to fit its column instead of keeping a fixed width.
Exercise
Why is it better to write the one-column layout first and add a min-width media query for two columns than to start with two columns and override it for phones?
Show answer
The base styles then work on the smallest screens without overrides, and wider screens only add what they need. This means less code, fewer conflicting rules, and a phone never has to download or parse layout rules meant for a desktop.