Sub topic 11
Make the page responsive
This chapter assumes you have a valid HTML skeleton with semantic landmarks, a linked stylesheet, a working box model, and Flexbox and Grid layouts from the earlier chapters. You should be comfortable reading CSS rules and using rem units. Here you make the page adapt to phones, tablets and desktops.
What "responsive" means
A responsive page uses one HTML file and one stylesheet, and the layout adjusts to the space available. Three pieces make that work:
- A viewport meta tag, so mobile browsers report their real width.
- Flexible layouts that use relative units,
max-widthand fluid grids. - Media queries, which change rules at widths where the design starts to break.
Images are the fourth piece. They get their own chapter, so this one only touches them where they affect layout.
The viewport meta tag
Mobile browsers have a history of pretending to be wide. Before responsive design, phones rendered pages in a virtual viewport of about 980 CSS pixels and shrank the result to fit the screen. Your page would look like a tiny desktop site.
This line in the <head> turns that behavior off:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-widthsets the layout width to the real screen width in CSS pixels.initial-scale=1sets the starting zoom to 100%.
Without this tag, your media queries still run, but they compare against the virtual 980px width. A rule like @media (max-width: 40rem) would never match on a phone.
Start with the content, not the device
A reliable approach is mobile-first. You write the base styles for a narrow screen, with no media query. Then you add min-width queries that enlarge the layout once there is room.
This works well for three reasons:
- A single-column layout is the natural result of normal document flow, so the base CSS is short.
- Small devices don't have to download and override desktop-only rules.
- Each query only adds things. You rarely need to undo earlier declarations.
The opposite approach is desktop-first with max-width queries. It works, but you end up writing rules for the wide layout and then undoing them for narrow screens.
Media query syntax
A media query wraps rules in a condition:
@media (min-width: 48rem) {
main {
padding-inline: 2rem;
}
}The rules inside apply only while the condition is true. min-width: 48rem means "the viewport is at least 48rem wide". At the default browser font size of 16px, that is 768px.
Some points about the syntax:
- Use
remoremfor breakpoints, notpx. Media queries inemandremunits are based on the browser's default font size, not on your CSS. If a user raises their default text size, the breakpoints move with it, and the layout switches to the roomier version sooner. That is the behavior you want. - You can combine conditions with
and:@media (min-width: 40rem) and (max-width: 60rem). - Media queries add no specificity. A rule inside
@mediahas the same specificity as outside, so the source order still decides ties. Put your queries after the base rules they override.
Modern browsers also support range syntax, such as @media (width >= 48rem). It is easier to read, but the min-width form works everywhere, so this tutorial uses it.
Choosing breakpoints
A breakpoint is a width at which the layout changes. Don't pick breakpoints from a list of device sizes. Devices change every year. Instead, do this:
- Open the page in your browser and narrow the window to about 320px wide.
- Slowly widen the window.
- Note the width where the layout starts to look wrong. Lines of text get too long, gaps look too big, or navigation items start to crowd.
- Add a breakpoint there.
A one-page personal site often needs only one or two breakpoints. Many of the changes in this tutorial need none, because Flexbox wrapping and Grid's auto-fit already adapt without queries.
Fluid layout before queries
Before you write any query, make the layout flexible. Two tools from the earlier chapters do most of the work.
A centered column with max-width
The main rule from the base stylesheet already does this:
main {
max-width: 60rem;
margin: 0 auto;
padding: calc(var(--space) * 2) var(--space);
}On a narrow screen, max-width has no effect and the column fills the viewport, with 1rem of padding on each side. On a wide screen, the column stops growing at 60rem, which keeps lines readable. A comfortable line holds roughly 45 to 75 characters, and 60rem at normal text size is already on the long side.
Grid with auto-fit
The project list adapts through one declaration:
#projects .project-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1.5rem;
}Read it from the inside out. minmax(16rem, 1fr) says each column is at least 16rem and can share leftover space equally. repeat(auto-fit, ...) creates as many such columns as fit. A 20rem-wide phone gets one column. A 40rem-wide tablet gets two. A 60rem column gets three. No media query is involved.
A common failure is a column minimum larger than the screen. If you set minmax(20rem, 1fr) and the viewport is 18rem wide, the grid overflows and the page scrolls sideways. If you need to support very narrow screens, use minmax(min(16rem, 100%), 1fr), which caps the minimum at the container width.
Adapt the navigation
The navigation from the Flexbox chapter is a centered row. With three short links, it fits on any phone. If you add more links later, it can overflow. Letting the items wrap solves it without a query:
nav ul {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
justify-content: center;
margin: 0;
padding: 1rem;
list-style: none;
}
nav a {
display: inline-block;
padding: 0.5rem 0.25rem;
}Here flex-wrap: wrap lets links drop to a second line. The two-value gap sets row spacing first and column spacing second. The padding on the links enlarges the tap target, which should be at least about 44 by 44 CSS pixels for comfortable touch use.
Now use a query for a change that wrapping can't give you. On wider screens, put the navigation on the right and give the page more breathing room:
@media (min-width: 48rem) {
main {
padding-inline: 2rem;
}
nav ul {
justify-content: flex-end;
}
}The nav ul rule inside the query has the same specificity as the base rule. It wins because it appears later in the file.
Fluid type with clamp()
Fixed heading sizes tend to be too large on phones or too small on desktops. The clamp() function takes a minimum, a preferred value and a maximum:
h1 {
font-size: clamp(2rem, 1.5rem + 2.5vw, 3.5rem);
line-height: 1.2;
}The heading scales with the viewport width (vw) but never drops below 2rem or grows past 3.5rem. Because the preferred value includes a rem part, it still responds to the user's text-size settings. A value made only of vw would ignore them, which is an accessibility problem.
Prevent common responsive bugs
Most responsive problems come from a short list of causes.
| Symptom | Likely cause | Fix |
|---|---|---|
| Whole page scrolls sideways | An element wider than the viewport, such as a fixed `width: 600px` or a long unbroken URL | Use `max-width: 100%`, or `overflow-wrap: anywhere` for long strings |
| Layout looks zoomed out on phones | Missing viewport meta tag | Add the tag to the `<head>` |
| Media query never matches | Typo, or the query is placed before a later rule that overrides it | Check the syntax and move the query after the base rules |
| Text looks tiny on phones | Fixed small `px` sizes | Set body text in `rem` and keep it at 1rem or larger |
| Padding makes boxes wider than expected | Default `content-box` sizing | Keep the `box-sizing: border-box` reset |
| Images stretch or overflow | No size limits | Set `max-width: 100%` and `height: auto` |
For images, a short global rule prevents overflow:
img {
max-width: 100%;
height: auto;
}The width and height attributes in the HTML still reserve the correct space. With height: auto, the browser keeps the aspect ratio while the image shrinks.
Test the result
Resize the browser window and watch the layout. Then open your browser's developer tools and switch on the device toolbar, which is usually called responsive design mode. It lets you:
- Enter any width and see the page at that size.
- Emulate common phones and tablets, including touch input.
- Simulate a slower network.
Emulation is an approximation. If you can, also open the page on a real phone. Check that you can read the text without zooming, tap each link without hitting its neighbor, and scroll only vertically.
Also test at 200% browser zoom. Zoom reduces the CSS viewport width, so it triggers your narrow-screen rules. If the content stays readable and nothing is cut off, the layout handles users who enlarge text.
Putting it together
The final responsive rules for the page, in source order:
/* Base: narrow screens */
main {
max-width: 60rem;
margin: 0 auto;
padding: calc(var(--space) * 2) var(--space);
}
h1 {
font-size: clamp(2rem, 1.5rem + 2.5vw, 3.5rem);
line-height: 1.2;
}
img {
max-width: 100%;
height: auto;
}
nav ul {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
justify-content: center;
margin: 0;
padding: 1rem;
list-style: none;
}
#projects .project-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
gap: 1.5rem;
}
/* Wider screens */
@media (min-width: 48rem) {
main {
padding-inline: 2rem;
}
nav ul {
justify-content: flex-end;
}
}Exercise
A page has a media query @media (max-width: 40rem) { .card { padding: 2rem; } }, but on a phone the rule never seems to apply. The HTML <head> contains only a charset tag and a title. What is the most likely cause, and what single line fixes it?
Show answer
The viewport meta tag is missing, so the phone uses a virtual width of about 980px and max-width: 40rem (640px) never matches. Add <meta name="viewport" content="width=device-width, initial-scale=1"> to the <head>.
Exercise
Why is @media (min-width: 48rem) better than @media (min-width: 768px) for a breakpoint, and in what order should the query and the base rule appear in the file?
Show answer
Media queries in rem are based on the browser's default font size, so if a user enlarges their default text, the breakpoint moves with it and the layout switches sooner, leaving room for the larger text. The query adds no specificity, so it must appear after the base rule it overrides.
Exercise
A grid uses grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr)). On a 18rem-wide screen the page scrolls sideways. Rewrite the column definition so it cannot overflow.
Show answer
grid-template-columns: repeat(auto-fit, minmax(min(20rem, 100%), 1fr)); The min() function caps the column minimum at the container width.