Sub topic 11
Make the page responsive
This chapter assumes you have a valid HTML skeleton, a linked stylesheet, and basic Flexbox and Grid knowledge from earlier chapters. You will make the page adapt to phones, tablets and desktops using the viewport meta tag, flexible units, and media queries.
Why pages need help on small screens
A phone browser pretends to be a wide desktop screen unless you tell it otherwise. It renders the page at about 980 pixels wide and then shrinks it to fit. Text becomes tiny and users have to pinch and zoom.
The viewport meta tag turns that behavior off. It goes in the <head>:
<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%. Your skeleton already has this tag. Without it, media queries will not behave as you expect on phones.
Build for small screens first
A common approach is to write the base styles for a narrow screen, then add rules as the screen gets wider. This is called mobile-first. The base styles stay simple, and each media query adds only what a larger screen needs.
Use flexible units instead of fixed pixel widths where you can:
remscales with the root font size.%andfrscale with the container.max-widthlets an element shrink but stops it from growing too large.
The main rule from the earlier chapter already uses max-width: 60rem with margin: 0 auto. On a phone the column fills the screen. On a desktop it stops at 60rem.
Media queries
A media query applies CSS only when a condition is true. The most common condition is the viewport width.
#projects .project-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1.5rem;
}
@media (min-width: 48rem) {
main {
padding-inline: 2rem;
}
}The Grid rule needs no media query. auto-fit with minmax(16rem, 1fr) places as many columns of at least 16rem as fit, so cards go from one column to several as the screen widens. The media query then adds more side padding once the viewport is at least 48rem (768px at the default font size) wide.
min-width queries match "this wide or wider". They fit the mobile-first approach because rules stack upward as the screen grows.
Choosing breakpoints
A breakpoint is the width where your 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 of text getting too long or the navigation wrapping badly.
For a one-page site, one or two breakpoints are usually enough.
Make the navigation adapt
Flexbox can wrap navigation links when space runs out. Adding flex-wrap stops the links from overflowing on narrow phones:
nav ul {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
justify-content: center;
margin: 0;
padding: 1rem;
list-style: none;
}Common errors
- Missing viewport tag. The page looks zoomed out on a phone, and your media queries seem ignored.
- Fixed pixel widths. A
width: 900pxelement forces sideways scrolling on a phone. Usemax-widthinstead. - Mixing units in queries.
min-width: 48remfollows the user's font size setting. A pixel value does not. Pick one unit and use it consistently. - Testing only by resizing the window. Also check on a real phone, or use your browser's device toolbar in the developer tools.
Exercise
A phone shows your page as a tiny, zoomed-out desktop layout, and your @media (min-width: 48rem) rules never seem to apply as intended. What is the most likely cause, and how do you fix it?
Show answer
The viewport meta tag is missing from the <head>. Add <meta name="viewport" content="width=device-width, initial-scale=1"> so the browser uses the real screen width and evaluates media queries against it.
Exercise
Write a mobile-first media query that sets .project-list to a gap of 2rem only when the viewport is at least 60rem wide.
Show answer
@media (min-width: 60rem) { #projects .project-list { gap: 2rem; } }