Sub topic 11
Preserving Hierarchy on Small and Large Screens
A hierarchy that works on a laptop can fall apart on a phone. Headings wrap awkwardly, the sidebar lands in the wrong place, and the main button is lost among other elements. This chapter shows how to keep the same order of importance as the viewport changes.
It assumes you have read the earlier chapters on size, contrast, position, and the type scale. It also assumes you know basic CSS: custom properties, media queries, and CSS Grid.
What changes between screen sizes
The hierarchy itself should stay the same. The page has one primary element and a clear second and third level, whatever the screen width. What changes is how you express that hierarchy.
| Cue | What can go wrong on a small screen | What can go wrong on a large screen |
|---|---|---|
| Size | Large headings take up most of the screen or wrap one word per line | Headings look small and the page seems empty |
| Contrast | Glare and small text make weak contrast harder to read | Wide areas of one color make the primary element blend in |
| Position | Content stacks into one column and the order changes | A wide layout spreads related items far apart |
| Spacing | Tight spacing merges groups | Generous spacing separates items that belong together |
Your goal is to adjust the cues so the same ranking of importance holds at each width.
Fluid type with clamp()
A fixed type scale uses the same sizes everywhere. That is fine for body text, but a large heading is a problem. A 2.441rem heading is about 39px, which fits on a desktop but wraps heavily on a 320px phone.
The clamp() function sets a minimum, a preferred value, and a maximum:
h1 {
font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem);
}The browser picks the preferred value, 1.2rem + 2.5vw, unless it falls below 1.75rem or above 3rem. Assuming a 16px root size:
- At a 320px viewport, the preferred value is 19.2 + 8 = 27.2px. That is below the 28px minimum, so the heading is 28px.
- At 1000px, the preferred value is 19.2 + 25 = 44.2px, which is inside the range.
- At 1440px, the preferred value is 19.2 + 36 = 55.2px. That is above the 48px maximum, so the heading is 48px.
The maximum matters as much as the minimum. Without it, a heading on an ultra-wide monitor keeps growing and overpowers everything else.
The preferred value mixes rem and vw on purpose. If you use only vw, text does not respond to the user's zoom or font-size setting, which fails WCAG's resize-text requirement. Adding a rem part keeps the text scalable.
Keeping the ratio between levels
What makes the hierarchy readable is the difference between levels, not the exact pixel values. On a small screen, the same 1.25 ratio can make the top levels too big. A common fix is to use a smaller ratio on narrow screens and the full ratio on wide ones. Redefine the custom properties from the type-scale chapter inside a media query:
:root {
/* small screens: ratio of about 1.2 */
--step-0: 1rem;
--step-1: 1.2rem;
--step-2: 1.44rem;
--step-3: 1.728rem;
--step-4: 2.074rem;
}
@media (min-width: 48rem) {
:root {
/* larger screens: ratio of about 1.25 */
--step-1: 1.25rem;
--step-2: 1.563rem;
--step-3: 1.953rem;
--step-4: 2.441rem;
}
}
body { font-size: var(--step-0); line-height: 1.5; }
h3 { font-size: var(--step-1); }
h2 { font-size: var(--step-2); }
h1 { font-size: var(--step-4); line-height: 1.15; }Components never need to change, because they refer to var(--step-n) and the values change in one place. The cost is that sizes jump at the breakpoint instead of changing smoothly. Choose by the trade-off:
- Use stepped values when you want predictable, testable sizes and few moving parts.
- Use
clamp()when you want smooth scaling and fewer breakpoints. - Mix the two if you like:
clamp()forh1and stepped values for the smaller levels.
Whichever you choose, check that every pair of neighboring levels is still visibly different. If h2 and h3 come out within a few pixels of each other, readers can no longer tell them apart, and the hierarchy has collapsed at that width.
Layout: from one column to two
On a narrow screen, a two-column layout does not fit, so content stacks. The stacking order is the new reading order, so decide it deliberately. This example is mobile-first, which means the base styles are for small screens and min-width queries add complexity for larger ones:
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;
}
}Three details are worth noting:
- The base layout is one column. The heading and the main message come first, as they should, because the DOM order is the reading order.
- The
2fr 1frsplit keeps a clear dominant side on wide screens. The larger column gets about two thirds of the width, so it reads first. - The breakpoint is in
rem, notpx. Users who increase their default font size then get the roomier layout sooner, which suits their larger text.
Avoid the temptation to fix a mobile order problem with CSS order or by placing grid items in a different sequence. If the sidebar comes before the main content in the HTML only so it can appear on the left on desktop, mobile and screen reader users meet it first. Put the main content first in the HTML and let the grid place the sidebar visually. Keep in mind the earlier chapter on visual order versus source order: the two should agree.
Position on small screens
On a phone, there is room for one or two things at a time, so position carries more weight. Some guidelines:
- Put the primary message and primary action within the first screen of content. Few readers scroll past a first screen that does not tell them what the page is for.
- Place the main action where a thumb can reach it, and make it big enough to hit. Full-width buttons in a stacked layout are common for this reason.
- Do not rely on a "right-hand column" for anything essential. On a phone, the right column is the content after everything else.
Here is a button group that stacks on narrow screens and sits in a row on wider ones. The primary and secondary buttons use the same styles as before:
.button {
min-height: 2.75rem;
padding: 0.6rem 1.2rem;
border-radius: 4px;
font: inherit;
text-align: center;
cursor: pointer;
}
.button--primary {
background: #0b57d0;
color: #ffffff;
border: 2px solid #0b57d0;
font-weight: 700;
}
.button--secondary {
background: transparent;
color: #0b57d0;
border: 2px solid #0b57d0;
font-weight: 400;
}
.actions {
display: grid;
gap: 0.75rem;
}
@media (min-width: 48rem) {
.actions {
display: flex;
gap: 1rem;
}
}The min-height of 2.75rem is 44px at the default size, a common touch-target recommendation. The filled and outlined styles still carry the contrast and weight difference that marks the primary action, so the hierarchy is the same in both layouts.
Contrast and large screens
WCAG contrast ratios do not change with screen size, so a color pair that passes on desktop passes on a phone. In practice, though, phones are used in bright light, which makes borderline pairs harder to read. Aim above the minimum for small text: 4.5:1 is the floor for normal text, and body copy that sits right at that line is more fragile outdoors.
Large screens bring a different problem: lines get too long. When a paragraph runs the width of a 1920px monitor, readers lose their place at the end of each line, and the text block becomes a wall that outweighs the headings. Cap the measure:
.prose {
max-width: 65ch;
}The ch unit is about the width of the "0" character, so 65ch gives roughly 65 characters per line, which is within the commonly recommended 45 to 75 range. A capped column also leaves white space around the text. That space is not wasted. It lets the heading and the primary element stand out against a calm background.
Common failures
These are the problems that most often break a hierarchy as the width changes:
- Heading overflow. A long word in a large heading runs past the edge of a narrow screen. Lower the minimum in
clamp()or addoverflow-wrap: anywhereto the heading. - Collapsed levels. The scale becomes so compressed on mobile that
h2andh3look the same. Compare them at 320px. - Reordered content. The visual order changed through CSS, but the DOM order did not, so keyboard focus jumps around.
- Lost grouping. Spacing tuned for desktop shrinks, and headings no longer sit closer to their own content than to the previous section. Keep the space above a heading larger than the space below it.
- Hidden primary action. The main button sits below a long block of text on mobile. Move it up in the source, or repeat it after the intro.
- Zoom breakage. At 200% zoom, a desktop layout should reflow into the single-column layout. If it scrolls sideways instead, the breakpoints are in
pxor the layout has fixed widths.
Checking at several widths
Test at least four widths: about 320px, 768px, 1280px, and 1920px. Also test at 200% browser zoom. At each width:
- Run the squint test. Blur your vision or the screenshot and confirm that the same primary element still stands out.
- Press Tab through the page and confirm that focus follows the visual order.
- Check that the smallest text and the main button still meet their contrast ratios. The contrast function from the earlier chapter works here, because the colors do not change with width.
- Confirm that no two neighboring heading levels look the same.
A quick way to do this in the browser is the device toolbar in your developer tools. Resize the viewport slowly, not only at the preset sizes, because a layout often breaks between the common breakpoints.
Exercise
A page uses font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem) on its h1, with a 16px root font size. What size is the heading at a 600px viewport, and why does the rule use both rem and vw in the preferred value?
Show answer
At 600px, the preferred value is 1.2 × 16 + 0.025 × 600 = 19.2 + 15 = 34.2px. That is above the 28px minimum (1.75rem) and below the 48px maximum (3rem), so the heading is 34.2px. The rem part keeps the size responding to the user's font-size setting and zoom, while the vw part makes it grow with the window. A value using only vw would ignore zoom.
Exercise
A two-column page puts its sidebar first in the HTML so it appears on the left on desktop. On mobile, the sidebar appears above the article. What is the hierarchy problem, and how do you fix it without changing the desktop look?
Show answer
The secondary content now comes before the primary content in the reading order for phone, keyboard, and screen reader users, which reverses the intended priority. Put the article first in the HTML and use a CSS Grid layout (for example, explicit column placement in a min-width media query) to show the sidebar on the left on wide screens. Then the DOM order and the mobile order both put the article first.