Open an online shop on a laptop: the products are shown in three columns. Open the same site on a phone: the products stack into one column and are still easy to read. The HTML is the same. What changes is the CSS, depending on the screen width.
A media query (@media) makes that change: it is CSS that only applies when the screen meets a condition. Where @container asks about the width of a wrapper, @media asks about the width of the viewport: the screen or the browser window.
Change the layout on narrow screens
This example has three boxes inside .container, then the CSS:
<div class="container">
<div class="box">A</div>
<div class="box">B</div>
<div class="box">C</div>
</div>
<style>
.container {
display: flex;
gap: 16px;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
</style>
- The first
.container { ... }rule is outside the media query, so it applies on every screen: the three boxes sit side by side. @media (max-width: 768px)is the condition: "is the screen 768px wide or less?"max-widthmeans the largest width.- Inside the curly braces of
@media, the selector is written again:.container { ... }. The reason:@mediaon its own selects no element. flex-direction: columnonly applies when the condition is met.
The result: on screens wider than 768px, boxes A, B and C sit side by side. On screens 768px wide or less, the three boxes stack from top to bottom. On an 800px screen, for example, the query does not apply, because 800px is wider than 768px.
The rules outside the query still apply on narrow screens. The query only overrides (replaces) the properties written inside it. So display: flex and gap still apply, and only the direction of the boxes changes.
How to write the condition
- The condition goes in parentheses. A colon separates the name and the number:
(max-width: 768px). - The number must have a unit:
768px, not768. Forms like@media max-width(768)or@screenare not valid. min-width(the smallest width) is the opposite ofmax-width:@media (min-width: 768px)applies on screens 768px wide or more.
A limit like 768px is called a breakpoint. Common ones: 480px for small phones, 768px for tablets, 1024px for laptops.
Besides the width, a media query can ask about other things:
(prefers-color-scheme: dark): the user has dark mode on.(prefers-reduced-motion: reduce): the user wants less movement on the screen.(hover: hover): the device has a mouse, so:hovereffects can be used.
Mobile-first: start from the small screen
The recommended way: write the CSS for phones first, then add @media (min-width: ...) for wider screens. For example:
.container {
display: flex;
flex-direction: column;
}
@media (min-width: 769px) {
.container {
flex-direction: row;
}
}
At every screen width, the boxes go in the same direction as in the first example. But the CSS is written in the reverse order: the base rule (the one outside the query) is one column, then the boxes sit side by side from a screen width of 769px. So mobile-first usually uses min-width, and desktop-first (starting from big screens) uses max-width.
The width condition also has a newer form with comparison signs, so the number 769 is no longer needed. @media (width > 768px) applies on screens wider than 768px. @media (width <= 768px) is the same as (max-width: 768px), because <= means less than or equal to. The breakpoint is written as one number, 768px, in both queries. Modern browsers support this form.
⚠️ Three quiet ways a media query fails
- No selector inside the query.
@media (max-width: 768px) { flex-direction: column; }does nothing, because no selector selects an element. Write.container { ... }inside the query. - Moving the base rules into the query. Then wide screens lose those styles. Keep the base rules outside the query. The query only overrides.
- Forgetting the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">in the<head>, a phone browser acts as if the screen were about 980px wide, then shrinks the page. So the conditionmax-width: 768pxis never met.
TL;DR: A media query (@media) is a part of your CSS that only applies when the screen meets a condition. @media (max-width: 768px) { .container { ... } } applies on screens 768px wide or less. Write the selector again inside the query, and keep the base rules (the ones for every screen) outside it. The number in the condition must have a unit. Mobile-first means writing the base rules for phones, then adding @media (min-width: ...) for wider screens.
⚡ Stacking on narrow screens
Write:
.container {
display: flex;
gap: 16px;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
Meaning: The first rule applies on every screen: the boxes inside .container sit side by side. @media (max-width: 768px) is a media query: the rule inside it only applies when the screen is 768px wide or less. On that screen, the boxes stack from top to bottom. Inside @media, the selector is written again, because @media on its own selects no element.
⚡ The query only overrides
Guess first: on a narrow screen, do the display: flex and gap written outside the query still apply?
Yes. The query only overrides (replaces) the properties written inside it, so only the direction of the boxes changes.
Meaning: The base rules (the ones for every screen) stay outside the query. The number in the parentheses of @media must have a unit: 768px, not 768.
⚡ Start from the phone
Write:
.container {
display: flex;
flex-direction: column;
}
@media (min-width: 769px) {
.container {
flex-direction: row;
}
}
Meaning: This way is called mobile-first: base rules for phones (one column), then min-width for wider screens. Here the boxes sit side by side from a screen width of 769px. Desktop-first is the opposite: base rules for big screens, then max-width for narrower screens.