Responsive design & media queries
Make a layout work from phone to desktop with a mobile-first approach.
By Invicly Tech, , in Frontend with React & Next.js. About 9 minutes to read.
In short
Responsive design makes one layout adapt from phones to desktops. A mobile-first approach writes base styles for small screens, then adds min-width media queries for larger ones.
Explained simply
A responsive page adapts to the screen it is on. The same HTML shows one column on a phone, two on a tablet and three on a laptop. You do not build separate sites; you write CSS that bends.
Phones first need one line in the HTML head: the viewport meta tag. Without it, a phone pretends to be a 980px wide desktop and shrinks everything until it is unreadable. With it, the page uses the real device width.
Mobile-first means you write the simple phone layout as your default, then add rules for bigger screens. It is like packing a small suitcase first, then adding extras when you have a bigger car. Small screens load less CSS logic, and the default always works.
Media queries are the "if" statements of CSS: if the screen is at least 48rem wide, use two columns. Fluid tools like percentages, fr units and clamp() let sizes flow smoothly between those points so you need fewer of them.
Press Play on a diagram to walk through it one step at a time.
- 1Viewport meta tag
Stops phones from rendering a zoomed-out desktop page
- 2Base styles
Single column layout for small screens
- 3Fluid sizing
Percentages, max-width and clamp() scale smoothly
- 4min-width queries
Add columns as space grows, e.g. at 48rem
- 5Test many widths
Use DevTools device mode and resize the window
- Default CSS = phone layout
- Add features with min-width
- Simple base, extras layered on
- Phones never parse overrides they need
- Default CSS = wide layout
- Undo features with max-width
- Many overrides to strip things away
- Easy to forget a phone case
- 1Fluid units
%, fr, vw adapt with no rules
- 2Intrinsic layout
auto-fill grids and flex-wrap reflow
- 3clamp(), min(), max()
Smooth sizes with limits
- 4Container queries
Components adapt to their parent
- 5Media queries
Page-level changes at breakpoints
In depth
What the viewport tag fixes
Early smartphones met a web built for desktops. To show those pages, mobile browsers render into a virtual window about 980px wide, then zoom out. That is why an old site looks tiny on a phone.
The meta tag name="viewport" content="width=device-width, initial-scale=1" tells the browser: my page is responsive, use the real device width in CSS pixels. A typical phone then reports about 360 to 430px, and your media queries fire as you expect. Never add maximum-scale=1 or user-scalable=no; that blocks people who need to pinch-zoom.
- Renders into a ~980px virtual window
- Zooms out to fit the screen
- Text looks tiny
- Media queries see about 980px
- Uses the real device width
- Phones report about 360 to 430px
- Text is readable size
- Media queries fire as expected
Choosing breakpoints from content
There is no official list of device sizes, and new devices appear every year. Instead, let the content choose. Start narrow, slowly widen the browser and add a breakpoint when the layout starts to look wrong: lines too long, cards too stretched, empty space.
Most sites end up with two or three breakpoints, often around 40rem, 64rem and 80rem. Using rem in media queries means the layout also adapts when a user increases their font size, not just when the window changes.
- Write min-width queries for mobile-first
- Use rem in queries so zoomed text gets more room
- Keep line length near 45 to 75 characters
- Two or three breakpoints are usually enough
Fluid sizing with clamp()
clamp(min, preferred, max) picks the preferred value but keeps it between the limits. For headings, clamp(1.75rem, 1rem + 3vw, 3rem) grows smoothly with the screen width, never smaller than 1.75rem, never bigger than 3rem. Mixing rem into the middle value keeps it responsive to user font settings.
You can use clamp for padding and gaps too, which removes many small media queries. For widths, the pattern width: min(100% - 2rem, 70rem) gives a centered container with side padding on phones and a maximum width on desktops.
Container queries
Media queries ask about the whole screen. But a card might sit in a wide main area on one page and a narrow sidebar on another. Container queries let a component ask about its own parent instead.
You mark a parent with container-type: inline-size, then write @container (min-width: 30rem) { ... } for the children. Now the card switches to a horizontal layout whenever its container is wide enough, wherever it is placed. All modern browsers support this.
- Asks about the whole viewport
- Same answer for every card on the page
- Good for page-level layout
- Asks about the parent's width
- Card adapts wherever it is placed
- Parent needs container-type: inline-size
- Good for reusable components
Responsive images
Large images are the heaviest part of most pages. Add max-width: 100% and height: auto so images never overflow their box. Then use srcset and sizes so the browser downloads a small file on phones and a big one on large screens. The browser picks the best file based on screen width and pixel density.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Add the viewport meta tag~45m
- 2Write mobile-first media queries~45m
- 3Use fluid widths and clamp()~45m
- 4Test layouts at several widths~45m
Key points
- Without the viewport meta tag, phones show a tiny desktop layout.
- Mobile-first queries use min-width and only add complexity as screens grow.
- clamp(min, preferred, max) gives fluid type and spacing without many breakpoints.
- Set breakpoints where your content breaks, not at specific device sizes.
Key terms
Tap a card to flip it. 0/4 known
Examples
/* In HTML head: <meta name="viewport" content="width=device-width, initial-scale=1"> */
.layout { display: grid; gap: 1rem; }
h1 { font-size: clamp(1.75rem, 4vw, 3rem); }
@media (min-width: 48rem) {
.layout { grid-template-columns: 16rem 1fr; }
}Single column by default, sidebar from 48rem up.
Mobile-first article layout
Step 1: by default, the article and its sidebar stack in one column, and the container has a 1rem gap on each side. Step 2: from 48rem (768px at the default font size) the sidebar moves to the right as a 16rem column. Step 3: the heading scales smoothly with clamp, so there is no jump at the breakpoint.
.page {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
display: grid;
gap: 2rem;
}
h1 { font-size: clamp(1.75rem, 1rem + 3vw, 3rem); }
@media (min-width: 48rem) {
.page { grid-template-columns: 1fr 16rem; }
}Serving the right image size
A hero image is 2400px wide (900 KB). On a 390px phone that is wasted data. Step 1: export three versions: 600, 1200 and 2400px. Step 2: list them in srcset with their widths. Step 3: sizes tells the browser the image fills the screen width up to 70rem. A phone with 3x density picks the 1200px file; a laptop picks 2400px.
<img
src="hero-1200.jpg"
srcset="hero-600.jpg 600w, hero-1200.jpg 1200w, hero-2400.jpg 2400w"
sizes="(min-width: 70rem) 70rem, 100vw"
alt="Team working around a table"
width="2400" height="1200"
/>Quick reference
- meta name="viewport" content="width=device-width, initial-scale=1"
- Mobile-first: base styles, then @media (min-width: ...)
- Use rem breakpoints such as 40rem, 64rem, 80rem
- img { max-width: 100%; height: auto; }
- Centered container: width: min(100% - 2rem, 70rem); margin-inline: auto;
- Fluid type: font-size: clamp(min, rem + vw, max)
- Never disable pinch-zoom with user-scalable=no
Common mistakes
- Writing desktop styles first, then overriding everything for mobile.
- Setting fixed pixel widths that cause horizontal scrolling on phones.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Make a list of feature boxes stack in one column on phones and show three columns from 64rem up.
- Exercise 2medium
Make the page padding and h1 size scale smoothly: padding from 1rem to 3rem, h1 from 2rem to 3.5rem, with no media queries.
- Exercise 3hard
A .product card should be vertical (image on top) when its container is narrow, and horizontal (image left, text right) when its container is at least 32rem wide, in any part of the site.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What breakpoints should I use?
- Add breakpoints where your content breaks, not for specific devices. Two or three, such as 40rem, 64rem and 80rem, cover most designs.
- Do I still need media queries with Flexbox and Grid?
- Fewer than before. auto-fill grids, flex-wrap and clamp() handle a lot, but big structural changes like moving a sidebar still need a media or container query.
- How do I test responsive layouts?
- Use the device toolbar in browser DevTools to try many widths, then check on at least one real phone, because touch, fonts and the address bar behave differently there.