Lesson 23 of 296
Responsive design and media queries
What you will learn
- What responsive design is
- The mobile-first approach
- Writing media queries
- Breakpoints
- Responsive images
- Media queries for user preferences
- Container queries
On this page
What is responsive design?
Responsive design means one website that adapts to every screen size: phones, tablets, laptops and large monitors. More than half of web traffic comes from phones, so this is not optional.
The three basic tools:
- The viewport meta tag (Lesson 7). Without it, nothing else works on phones.
- Flexible layouts: Flexbox, Grid, percentages,
fr,max-width. - Media queries: CSS rules that apply only on certain screen sizes or settings.
Media queries
@media (min-width: 768px) {
.menu { display: flex; }
}The rules inside only apply when the screen is at least 768px wide.
| Condition | Meaning |
|---|---|
(min-width: 768px) | 768px wide or wider |
(max-width: 767px) | 767px wide or narrower |
(min-width: 768px) and (max-width: 1023px) | Between the two |
(width >= 768px) | Newer, easier syntax for the same as min-width. Supported in modern browsers. |
(orientation: landscape) | The screen is wider than it is tall |
Mobile first
Mobile first means: write the styles for small screens first, with no media query. Then add min-width media queries to add or change styles as the screen gets bigger.
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 640px) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1024px) {
.cards { grid-template-columns: repeat(3, 1fr); }
}Why mobile first?
- Small-screen styles are usually simpler, so the base CSS stays small.
- Phones, often on slow connections, do not have to undo desktop styles.
- It makes you decide what content really matters.
Breakpoints
A breakpoint is a width where the layout changes. Common values (also used by Tailwind):
| Name | Width |
|---|---|
| sm | 640px |
| md | 768px |
| lg | 1024px |
| xl | 1280px |
The best rule: add a breakpoint where your design breaks, not for a specific phone model. Make the browser window narrower and wider, and add a breakpoint where things start to look wrong.
Responsive images
Images that never overflow their container:
img {
max-width: 100%;
height: auto;
}Most projects put this in their base CSS.
Different files for different screens, so phones do not download huge images:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 1024px) 50vw, 100vw"
alt="Our office in Leeds">srcsetlists the files and their real widths.sizestells the browser how wide the image will be on screen.- The browser chooses the smallest file that still looks sharp.
Keeping a shape:
.thumb {
aspect-ratio: 16 / 9;
object-fit: cover;
width: 100%;
}aspect-ratio keeps the box at 16:9. object-fit: cover fills the box with the image and crops the edges instead of stretching it.
Media queries for user preferences
Dark mode:
@media (prefers-color-scheme: dark) {
body { background: #111; color: #eee; }
}Reduced motion (for people who get dizzy or sick from animations):
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}Touch screens and mouse:
@media (hover: hover) {
.card:hover { transform: translateY(-4px); }
}Only devices that can really hover get the hover effect. On phones, hover effects can "stick" after a tap.
Container queries
A media query looks at the screen size. A container query looks at the size of the container the component is in. This matters for reusable components: the same card might be in a wide main area or a narrow sidebar.
.card-wrap { container-type: inline-size; }
@container (min-width: 400px) {
.card { display: flex; }
}The card switches to a side-by-side layout when its container is at least 400px wide, whatever the screen size. Container queries are supported in all modern browsers.
Touch targets
On phones, buttons and links must be big enough to tap. Aim for at least 44 by 44 pixels. WCAG 2.2 sets a minimum of 24 by 24 pixels at level AA.
Testing
- Chrome dev tools has a device toolbar (the phone icon) to test many sizes.
- Drag the window narrow and wide and watch for breaking points.
- Test on a real phone before launch. Emulators miss things like the moving address bar and real touch.
Common mistakes
- Forgetting the viewport meta tag.
- Desktop-first CSS full of
max-widthoverrides that are hard to follow. - Fixed widths like
width: 1200pxthat cause sideways scrolling on phones. - Breakpoints chosen for one device model.
- Hover-only features that phone users cannot reach.
- Tiny tap targets placed close together.
How to say it in an interview
"Responsive design means one site that adapts to any screen. I work mobile first: base styles for small screens, then min-width media queries to enhance for larger ones, with breakpoints set where the design breaks. I use flexible Grid and Flexbox layouts, max-width 100% on images, and srcset so phones get smaller files. I also respect user preferences like reduced motion and dark mode, and I use container queries when a component should respond to its own space rather than the screen."
Key words
| Word | Meaning |
|---|---|
| Responsive design | A site that adapts to every screen size |
| Media query | CSS that applies only under certain conditions |
| Breakpoint | A width where the layout changes |
| Mobile first | Small-screen styles first, then larger screens |
| srcset | A list of image files at different sizes |
| Container query | Styles based on the size of a component's container |
| prefers-reduced-motion | A user setting to reduce animation |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.