Sub topic 11
Preserving hierarchy on small and large screens
This chapter assumes you know basic CSS (custom properties, media queries, rem units) and the earlier chapters on type scales, contrast, and position. Here you will keep one hierarchy working as the viewport changes from a phone to a wide monitor.
What changes between screen sizes
The hierarchy you designed is a set of relationships: the main heading is larger than the subheading, the primary action is more prominent than the secondary one, and related items sit close together. Resizing the screen can break those relationships even when every element still looks fine alone.
Three things commonly go wrong:
- A heading that is dominant at 1280 px wraps over four lines on a phone and pushes all other content out of view.
- A two-column layout collapses to one column, and the side content ends up above the main content.
- Fixed large sizes on a small screen make body text and headings nearly the same size relative to the viewport.
Scale type with limits
A fixed font-size can't adapt. A pure viewport unit such as 4vw adapts but has no limits. It can become tiny on a phone or huge on a monitor. The clamp() function takes a minimum, a preferred value, and a maximum.
h1 {
font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem);
}
.hero {
display: grid;
gap: 1rem;
}
@media (min-width: 48rem) {
.hero {
grid-template-columns: 2fr 1fr;
gap: 2rem;
}
}With a 16 px root size, the heading stays at 1.75rem (28 px) until the viewport is about 352 px wide. It grows smoothly after that and stops at 3rem (48 px) at about 1152 px. The body text stays at 1rem the whole time, so the heading is always clearly larger than the body text.
The preferred value mixes rem and vw on purpose. A value made only of vw ignores the browser's text zoom, so users who enlarge their text would see no change. Including a rem term keeps zoom working.
Use this markup with the CSS above:
<section class="hero">
<div class="hero__main">
<h1>Plan your week</h1>
<p>Organize tasks, set reminders, and share lists.</p>
<a class="button button--primary" href="/signup">Start free</a>
</div>
<aside class="hero__side">
<p>Used by 20,000 teams.</p>
</aside>
</section>Change the layout, not the order
On a narrow screen the grid has one column, so items stack in source order: hero__main, then hero__side. The main content and primary action come first, which is the priority you want. At 48rem and wider, the 2fr 1fr columns give the main content twice the width of the side content. Position and size now say the same thing.
Avoid reordering with CSS order or grid-area to fix a small-screen problem. A reordered layout makes the visual order differ from the DOM order, which breaks the keyboard and screen reader consistency covered in the previous chapter. Write the HTML in priority order and let the layout adapt around it.
Keep contrast and targets intact
Small screens often add glare, so check contrast on mobile as well. Dropping a color from the design to save space can remove the cue that separates primary from secondary actions. Keep the filled primary button and the outlined secondary button at every width.
Touch targets need room too. Buttons should stay large enough to tap, so keep the padding rather than shrinking buttons to fit.
Common errors
| Error | Result | Fix |
|---|---|---|
| Heading size in `vw` only | Unreadable on small screens, ignores text zoom | Use `clamp()` with a `rem` term |
| Side content placed first in the HTML | It appears first on phones | Put main content first in the source |
| Hiding the primary action on mobile | The key task becomes hard to find | Keep it visible, possibly full width |
| Testing only one width | Hierarchy breaks between breakpoints | Resize continuously and check several widths |
Exercise
A page sets h1 { font-size: 5vw; }. On a 320 px wide phone the heading is 16 px, the same size as the body text. Rewrite the rule with clamp() so the heading is never smaller than 1.75rem or larger than 3rem, and explain why the preferred value should include a rem term.
Show answer
For example: h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); }. The minimum keeps the heading larger than the 1rem body text on small screens, and the maximum stops it growing too large on wide screens. The rem term in the preferred value lets the size respond when users zoom or enlarge their browser text, which a value made only of vw would not do.