Lesson 23 of 50
cssResponsive Design – Media Queries
10 mins
Course progress46%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 23 of 50
css10 mins
Keep these ideas in mind as you work through the lesson.
Media queries let you apply CSS conditionally based on the capabilities of the viewer’s device.
@media (min-width:768px) {
/* styles for viewports 768 px and larger */
}
The rule inside only runs when the viewport width meets the condition.
| Device | Breakpoint |
|---|---|
| Phones | max-width: 599px |
| Tablets | min-width:600px and max-width:1199px |
| Desktops | min-width:1200px |
Why mobile‑first?
min-width queries adds styles as the screen grows, rather than overriding a huge mobile stylesheet.@supportsDetect whether a browser understands a given property before using it:
@supports (display:grid) {
.container { display:grid; }
}
prefers-color-scheme – Detect dark or light mode.prefers-reduced-motion – Respect users who request less motion.prefers-contrast – High‑contrast mode./* Base – hidden on mobile */
.nav { display:none; }
/* Show on tablets & larger */
@media (min-width:600px) {
.nav { display:flex; gap:2rem; }
}
Define a breakpoint variable once and reuse it:
:root { --bp-tablet: 600px; }
@media (min-width: var(--bp-tablet)) { … }
Now you only need to change the value in one place when your design evolves.