Responsive Design for Phones
Use the viewport tag, flexible widths and media queries so your website looks right on a phone, a tablet and a laptop without separate versions.
Welcome back!
Step 1 of 12Learn
Most visitors are on a phone
Priya finished her art portfolio on a laptop and sent the link to her friends. Every reply said the same thing: "It's tiny on my phone." The text was microscopic and she had to pinch and zoom to read anything. Her site wasn't broken; it just wasn't responsive.
More than half of all web traffic worldwide comes from phones, and for teens that share is much higher. A responsive site uses one set of HTML and CSS that adapts its layout to the screen it's on. Three ingredients do almost all the work:
- The viewport meta tag, so phones don't fake a desktop screen.
- Flexible sizes, so boxes and images shrink with the screen instead of having fixed pixel widths.
- Media queries, so you can change the layout at certain screen widths.
Start with the first one, because without it nothing else works. Add this line to the <head> of every page:
<meta name="viewport" content="width=device-width, initial-scale=1">Without it, phone browsers pretend the screen is about 980 pixels wide and shrink the whole page to fit, which is exactly what happened to Priya. With it, the page uses the phone's real width, and your 18-pixel text stays readable.
Step 2 of 12Chart
Worldwide web traffic by device
| Item | Share of page views |
|---|---|
| Phones | 62% |
| Desktops and laptops | 36% |
| Tablets | 2% |
Step 3 of 12Practice · Choose one
Priya's page appears zoomed out and tiny on phones even though her CSS is fine. What is the most likely missing piece?
Step 4 of 12Learn
Flexible widths and images
Fixed widths are the enemy of small screens. A box with width: 800px is wider than every phone, so visitors have to scroll sideways. Two habits fix most problems.
Use max-width instead of width. max-width: 800px means "up to 800 pixels, but shrink if the screen is narrower." Combined with margin: 0 auto, it gives a centered column that fits any screen.
Use relative units. Percentages are relative to the parent: width: 50% is always half. The rem unit is relative to the root font size, usually 16px, so padding: 1.5rem is 24px and scales if the visitor increases their browser's text size. Many developers size text and spacing in rem for exactly that reason.
Make images flexible. Add this rule once:
img { max-width: 100%; height: auto; }An image will never be wider than its container, and height: auto keeps its proportions so it doesn't get stretched.
A good test: open your page on a laptop and slowly drag the browser window narrower. If a horizontal scroll bar appears at any point, something has a fixed width that's too big. Developer tools also have a device mode that previews common phone sizes.
Step 5 of 12Compare
Fixed vs flexible widths
| Aspect | Fixed | Flexible |
|---|---|---|
| Box | width: 800px | max-width: 800px |
| Image | width: 600px | max-width: 100%; height: auto |
| Spacing | padding: 40px everywhere | padding in rem, smaller on phones |
| On a 375px phone | Sideways scrolling | Fits the screen |
Step 6 of 12Practice · Work it out
The root font size is 16px. How many pixels is a padding of 1.5rem?
Step 7 of 12Learn
Media queries change the layout
Sometimes shrinking isn't enough and the layout itself should change: three columns on a laptop, one column on a phone. A media query wraps rules that apply only when a condition is true:
.cards { display: flex; flex-direction: column; gap: 16px; }
@media (min-width: 700px) {
.cards { flex-direction: row; }
}Read it as: "Normally, stack the cards. When the screen is at least 700 pixels wide, put them in a row." The point where the layout changes is called a breakpoint.
Notice the order. The basic styles are written for phones first, and the media query adds changes for bigger screens. This mobile-first approach keeps the CSS simpler, because small screens get the plainest layout and you only add complexity where there's room for it.
Pick breakpoints where your design starts to look awkward, not where a particular phone model happens to be. Drag the window narrower and add a breakpoint at the width where things start to feel cramped. Most small sites need only one or two.
Step 8 of 12Practice · Diagram
One breakpoint at 700px
Tap the screen that sees only the base, mobile-first rules.
Tap the shape that answers the question, then press Check.
- Phone
375 px
- Tablet
768 px
- Laptop
1,366 px
Step 9 of 12Practice · Sort into groups
With mobile-first CSS and a min-width 700px media query, which styles apply on which screen?
Tap an item, then tap its group (or drag it there).
Step 10 of 12Note
Step 11 of 12Practice · True or false
In a mobile-first style sheet, the rules for phones usually go inside media queries and the rules for laptops go outside them.
Step 12 of 12Practice · Fill in the blanks
Fill in the blanks
The CSS rule img { max-width: ; height: ; } stops images from overflowing their container while keeping their proportions.
Lesson complete!