Sub topic 11
Adapt the layout to small screens
This chapter assumes you can write basic HTML and CSS selectors, and that you know the box model. It also assumes you have the horizontal Flexbox navigation bar from the earlier chapters. Here you will make that bar work on narrow screens.
Why the bar breaks on a phone
The bar is a flex row. Flex items shrink only so far, because a link cannot get narrower than its longest word. With four or five links, the row runs out of room on a phone and the links overflow or squeeze together. The tap targets also get small.
The fix is to change the layout when the screen is narrow. CSS does this with a media query.
Tell the phone its real width
Mobile browsers pretend to be a wide desktop screen unless the page says otherwise. Without the tag below, your media queries will not match as you expect on a phone. Add it to the <head> of every page.
<meta name="viewport" content="width=device-width, initial-scale=1">Write a media query
A media query is a CSS rule that applies only when a condition is true. The most common condition is the viewport width, which is the width of the browser window.
@media (max-width: 40rem) {
.nav-list {
flex-direction: column;
}
}The rules inside the braces apply only when the viewport is 40rem wide or narrower. At the default browser font size, 40rem is 640 pixels. Above that width, the bar stays a row.
The flex-direction: column declaration changes the main axis of the flex container from horizontal to vertical. The links stack, and each one is a full-width row because display: block on the links makes them fill their list items.
flowchart TDThe diagram below shows the decision the browser makes.
Write the base styles first
Write the wide-screen styles outside any media query, then override only what changes. The query then contains a small number of declarations, which are easy to read and debug.
.nav-list {
display: flex;
gap: 0.25rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}
@media (max-width: 40rem) {
.nav-list {
flex-direction: column;
gap: 0;
}
.nav-list a {
padding: 1rem;
}
}The extra padding on small screens gives each link a taller tap target. Touch targets of about 44 pixels in height are a common guideline.
Choose a breakpoint
A breakpoint is the width where the layout changes. Pick one by resizing the browser and noting where the row starts to look cramped. Do not pick it by matching a device model, because device sizes change often. Your own links decide the right value: a bar with seven long labels needs a wider breakpoint than a bar with three short ones.
You will sometimes see the mobile-first approach, where the base styles are for small screens and min-width queries add the wide layout. Either works. This tutorial uses max-width because the bar is already written for desktop.
Hiding the menu comes with a button
Many sites collapse the links behind a "Menu" button on small screens. Setting display: none on the list in a media query is not enough on its own. If you hide the list and provide no button, nobody on a phone can reach the links. The next chapter adds the button, with aria-expanded and a few lines of JavaScript. For now, keep the stacked links visible.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Missing viewport meta tag | Phone shows a shrunken desktop page and the query never matches | Add the meta tag to the `<head>` |
| Media query placed before the base rule | Override is ignored | Put the query after the rules it overrides, because equal-specificity rules are decided by order |
| Breakpoint chosen from a device model | Layout breaks on other screens | Choose it from where your own content gets cramped |
| Small padding on touch screens | Links are hard to tap | Increase link padding inside the query |
Exercise
Your navigation bar is a flex row on desktop. On a 500px-wide phone it looks correct, but the stacked layout from your @media (max-width: 40rem) rule never appears, and the page looks like a zoomed-out desktop site. What is the most likely cause, and how do you fix it?
Show answer
The page is missing <meta name="viewport" content="width=device-width, initial-scale=1"> in the <head>. Without it, the mobile browser lays the page out at a desktop width (about 980px), so the viewport never counts as 40rem or narrower. Adding the tag makes the layout viewport match the device width, and the media query then applies.