Sub topic 10
Adapting the Layout to Screen Size
This chapter assumes you know basic HTML and CSS selectors, and that you can open a page in a browser and use its developer tools. You should also have seen the page skeleton, with its header, hero, content sections, and footer, and the grid and flexbox basics from the previous chapter.
A page that looks good on a laptop can break on a phone. Responsive design means the same HTML adapts its layout to the width of the screen. You do not write a second page. You write CSS that changes how the existing parts are arranged.
Start with the viewport meta tag
Phones pretend to be wide by default and then shrink the page to fit. To turn that off, the head of the page needs this tag:
<meta name="viewport" content="width=device-width, initial-scale=1">Without it, your media queries will not behave as expected on mobile devices. The skeleton from earlier chapters already includes it.
Mobile first: one column by default
The simplest approach is to write the narrow-screen layout first. Stacked content in one column needs little CSS, because block elements already stack. You then add rules for wider screens.
A media query is a CSS rule that applies only when a condition is true, such as the viewport being at least a certain width. The min-width form is the usual choice for mobile first, because each query adds to the layout you already have.
.hero {
display: grid;
gap: 2rem;
padding: 3rem 1.5rem;
}
.hero img {
max-width: 100%;
height: auto;
}
@media (min-width: 48rem) {
.hero {
grid-template-columns: 1fr 1fr;
align-items: center;
}
}Below 48rem (768px at the default font size), the hero is one column: text, then image. From 48rem upward, it becomes two columns with the content vertically centered. The rule max-width: 100% with height: auto stops the image from overflowing its column and keeps its proportions.
Using rem units for breakpoints ties them to the user's font size. Someone who enlarges their text gets the narrow layout sooner, which usually helps readability.
Choosing breakpoints
A breakpoint is the width at which the layout changes. Do not pick breakpoints from a list of device sizes. Instead, resize your browser window slowly and add a breakpoint where the design starts to look wrong, such as lines becoming too long or columns too cramped.
Grids that adapt without media queries
Some layouts need no breakpoint at all. A card grid in the content sections can use auto-fit and minmax():
.card-grid {
display: grid;
gap: 1.5rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Each column is at least 16rem wide and can grow to share leftover space. The browser fits as many columns as the width allows. A narrow phone gets one column, a tablet two or three, and a desktop more, all from one declaration.
Adapting the header navigation
The header uses flexbox to place the site name and the links in a row. On a very narrow screen, the links may not fit. One simple fix is to let the list wrap:
header nav ul {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}Wrapping keeps every link visible and needs no JavaScript. A collapsible menu is an option for long navigation lists, but it adds complexity, so use it only when wrapping is not enough.
Common errors
- Forgetting the viewport tag. The layout looks like a shrunken desktop page on phones.
- Using
max-widthqueries for the base styles. You end up overriding desktop styles on mobile, which is harder to maintain. - Fixed widths in
px. An element withwidth: 900pxforces horizontal scrolling on a small screen. Prefermax-widthor flexible units. - Images without
max-width: 100%. Large images push the layout wider than the screen. - Testing only by resizing the desktop window. Also use the device toolbar in your browser's developer tools and, when you can, a real phone.
Exercise
You wrote .card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); }. The container is 40rem wide and the gap is 0. How many columns appear, and does the layout need a media query to change on a phone?
Show answer
Two columns appear, because two 16rem columns fit in 40rem and three would need 48rem. The two columns then grow to 20rem each to share the space. No media query is needed. On a phone narrower than 32rem, only one column fits, so the grid becomes a single column automatically.
Exercise
Why is @media (min-width: 48rem) a better fit for a mobile-first stylesheet than @media (max-width: 48rem)?
Show answer
With min-width, the base styles describe the simple narrow layout, and the query only adds rules for wider screens. With max-width, the base styles describe the desktop layout and the query must undo them on small screens, which means more overrides and harder maintenance.