Sub topic 10
Adapting the layout to screen size
A page that looks right on a 1440-pixel laptop can break on a 360-pixel phone. The header's links wrap badly, the hero image overflows, and the cards shrink until the text is unreadable. This chapter shows how to make the header, hero, content sections, and footer adapt to the space they have.
This chapter assumes you can write basic HTML and CSS, and that you have read the chapters on semantic elements and on laying out the page with grid and flexbox. You should also know how to open your browser's developer tools.
Responsive design starts with the viewport
Responsive design is a way of writing one page whose layout changes with the width of the screen. You don't build a separate mobile site.
Mobile browsers start from a wrong assumption. Unless told otherwise, they pretend the screen is about 980 pixels wide and then shrink the result to fit. Your media queries would then see a "980px" screen and the page would look like a tiny desktop site. The viewport meta tag in the <head> turns this off:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-width tells the browser to use the real screen width. initial-scale=1 sets the starting zoom to 100%. Every skeleton in this tutorial includes this line. Without it, none of the CSS below works as described on a phone.
Avoid adding user-scalable=no or maximum-scale=1 to this tag. They stop people from pinch-zooming, which is an accessibility failure for anyone with low vision.
Three tools, three jobs
You will use three techniques together. Each handles a different kind of change.
| Technique | What it does | Example in this page |
|---|---|---|
| Flexible sizing | Lets elements grow and shrink with their container | `max-width: 100%` on the hero image |
| Intrinsic grids | Lets the browser choose the column count from the available width | `repeat(auto-fit, minmax(...))` on the card grid |
| Media queries | Switches whole layout rules at a chosen width | Hero changes from one column to two |
Prefer the first two when they can do the job. They need no breakpoints, so they have fewer cases to test. Use a media query when the layout needs a structural change that sizing alone cannot produce, such as placing the hero text beside the image.
Mobile first: write the narrow layout as the default
In a mobile-first stylesheet, the rules outside any media query describe the narrowest layout. You then add min-width media queries that enhance the layout as space becomes available.
This order works well for three reasons. A single column needs very little CSS, so the base rules stay short. Small devices don't have to parse and override a pile of desktop rules. And the layered min-width queries read in order from small to large.
/* Base: narrow screens. One column, no media query needed. */
.hero {
display: grid;
gap: 2rem;
padding: 3rem 1.5rem;
}
.hero img {
max-width: 100%;
height: auto;
}
/* Wider screens: add a second column. */
@media (min-width: 48rem) {
.hero {
grid-template-columns: 1fr 1fr;
align-items: center;
}
}The max-width: 100% and height: auto pair on the image is what keeps it inside its container. The image never grows past its container, and its height follows its width, so it keeps its proportions.
Why breakpoints are in rem
A breakpoint is the width at which a media query switches the layout. The example uses 48rem, not 768px. In media queries, rem and em are measured against the browser's default font size, usually 16px, so 48rem is 768px for most people. For someone who has raised their default font size to 24px, 48rem becomes 1152px. Their layout stays single-column longer, which is what you want, because text at that size needs more room per line.
Choosing breakpoints
Don't choose breakpoints by naming devices such as "iPhone" or "iPad". Device sizes change every year. Instead, start with the narrow layout, widen the browser window slowly, and add a breakpoint at the width where the design starts to look wrong. Content should decide the breakpoint.
A grid that needs no breakpoints
For the content sections, a media query is often unnecessary. This rule lets the browser pick the number of columns:
.card-grid {
display: grid;
gap: 1.5rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Read it from the inside out:
minmax(16rem, 1fr)means each column is at least 16rem wide and may grow to share leftover space equally.repeat(auto-fit, ...)means "create as many such columns as fit in the row".- The browser divides the width by the minimum, rounds down, and stretches the columns to fill the row.
auto-fit and auto-fill
auto-fill and auto-fit behave the same when the row is full. They differ when there are fewer cards than columns that could fit. With auto-fill, the empty column tracks stay and keep their width, so two cards sit at the left with blank space on the right. With auto-fit, the empty tracks collapse to zero and the existing cards stretch to fill the row. Use auto-fit when you want a few cards to stretch across the row. Use auto-fill when you want a consistent column width.
The narrow-screen overflow edge case
There is a failure mode in that rule. On a phone narrower than about 16rem plus page padding, the 16rem minimum is wider than the container. The column then overflows and the page scrolls sideways. This is rare on modern phones, but it does happen with small windows, split-screen mode, or high zoom. The fix is to cap the minimum at the container width:
.card-grid {
display: grid;
gap: 1.5rem;
grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
}min(16rem, 100%) picks the smaller of the two values, so the column minimum can never exceed the container.
Adapting the header and footer
The header holds the site name and the navigation. On a narrow screen there may not be room for both in one row. Allowing the flex row to wrap solves this without a media query:
header {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 0.75rem 2rem;
padding: 1rem 1.5rem;
}
header nav ul {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}When the navigation no longer fits beside the site name, it drops to a second line. The gap shorthand takes the row gap first, then the column gap, so the wrapped lines stay 0.5rem apart while the links stay 1.5rem apart horizontally.
This works for three to five links. A menu with many links needs a different pattern, usually a button that opens a list. That pattern needs JavaScript to toggle state and to manage focus, so it belongs with the accessibility work.
Footers adapt the same way. Put footer columns in a flex-wrap: wrap row or in the same auto-fit grid, and they stack when space is short.
Fluid text and spacing
Large headings can look right on a desktop and fill half of a phone screen. clamp() gives a value that scales between a minimum and a maximum:
.hero h1 {
font-size: clamp(2rem, 1.25rem + 3vw, 3.5rem);
line-height: 1.15;
}The three arguments are the smallest allowed size, the preferred size, and the largest allowed size. The preferred value mixes a rem part with a vw part (1vw is 1% of the viewport width). Including the rem part matters. A size written purely in vw ignores the user's zoom setting, which can fail accessibility requirements for resizing text.
Testing responsive behavior
Use the device toolbar in your browser's developer tools to resize the viewport. Check these things:
- Drag the width slowly from about 320px to 1600px and watch for the moments when something breaks, not only the common phone and desktop widths.
- Look for horizontal scrolling at every width. A sideways scrollbar nearly always means some element has a fixed width or a minimum that is too large.
- Zoom the browser to 200% at desktop width. This is equivalent to a narrow viewport and tests the same rules.
- Rotate to landscape. A phone in landscape can be wider than your tablet breakpoint but have very little height, so check that a tall hero does not push all other content off the screen.
Common errors
- Missing viewport tag. Symptom: the phone shows a shrunken desktop layout and your media queries never fire.
- Desktop-first overrides. Starting with a wide layout and undoing it with
max-widthqueries produces long, hard-to-read override chains. Start narrow instead. - Fixed pixel widths. A
width: 600pxon a container breaks on any screen narrower than 600px. Usemax-widthinstead. - Mixing breakpoint units. If some queries use
pxand othersrem, they switch at different widths when users change their font size. Pick one unit, and preferrem. - Hiding content on small screens. Setting
display: noneon content to "simplify" mobile removes it for phone users. Reorder or reflow it instead.
Summary
Add the viewport meta tag, write the single-column layout as the default, and add min-width queries in rem only where the content needs a structural change. Let max-width: 100%, flex-wrap, and repeat(auto-fit, minmax(min(16rem, 100%), 1fr)) handle everything that can adapt without a breakpoint.
Exercise
A card grid uses grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));. Visitors on a 280px-wide phone report a horizontal scrollbar. Explain why this happens and write the corrected declaration.
Show answer
The 20rem minimum (320px at the default font size) is wider than the 280px container, so the single column overflows and the page scrolls sideways. Cap the minimum at the container width with min(): grid-template-columns: repeat(auto-fit, minmax(min(20rem, 100%), 1fr));
Exercise
Your hero switches to two columns with @media (max-width: 768px) { ... } written after a two-column base rule. Rewrite the approach mobile-first and say why 48rem is a better unit than 768px for the breakpoint.
Show answer
Make the single-column layout the base rule, then add @media (min-width: 48rem) { .hero { grid-template-columns: 1fr 1fr; } }. Media queries in rem scale with the user's default font size, so a visitor with larger text keeps the single-column layout until there is enough width for the larger text. A px breakpoint ignores that setting.