Sub topic 4
Writing media queries
This chapter assumes you know basic CSS selectors, the box model, and units such as px, rem, and percentages. It also assumes you have read the earlier chapter on the viewport and CSS pixels.
What a media query does
A media query is a condition attached to a block of CSS. The browser applies the block only while the condition is true. The condition is re-evaluated whenever the window resizes or the device rotates, so the layout updates without any JavaScript.
Media queries only match the real screen width if the page declares a device-width viewport. Without that, mobile browsers lay the page out on a wide virtual viewport and your queries see the wrong number.
<meta name="viewport" content="width=device-width, initial-scale=1">Anatomy of a query
A query has an at-rule, an optional media type, and one or more features in parentheses.
@media screen and (min-width: 48em) {
.layout {
grid-template-columns: 1fr 1fr;
}
}@mediastarts the rule.screenis the media type. You can usually omit it, and the query then applies to all types.(min-width: 48em)is the feature test. It is true when the viewport is at least 48em wide.
Selectors inside the block follow the normal cascade. A query adds no specificity, so a rule inside it overrides an earlier rule with the same specificity only because it comes later in the source.
Width features and units
min-width means "this wide or wider". max-width means "this wide or narrower". Use em or rem in the condition, not px. In media queries both units are relative to the browser's default font size, not to any font size set in your CSS. A user who raises their default text size therefore reaches your breakpoints at a smaller window width, which gives them the roomier layout sooner. At the default 16px, 48em is 768px and 80em is 1280px.
Combining conditions
Join conditions with and. Separate alternatives with a comma, which acts as "or". Use not to negate a whole query.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Tablet and up */
@media (min-width: 48em) {
.layout {
grid-template-columns: 1fr 1fr;
}
}
/* Desktop and up */
@media (min-width: 80em) {
.layout {
grid-template-columns: 2fr 1fr 1fr;
max-width: 80rem;
margin-inline: auto;
}
}
/* Only inside the tablet range */
@media (min-width: 48em) and (max-width: 79.99em) {
.layout {
padding: 2rem;
}
}The last query matches only between the two breakpoints. The upper bound is 79.99em instead of 80em so the ranges never overlap at exactly 80em.
Features beyond width
Width is the most common feature, but not the only one. Other features test the device or the user's settings.
| Feature | Tests for | Example |
|---|---|---|
| `orientation` | Portrait or landscape viewport | `(orientation: landscape)` |
| `hover` | Whether the primary input can hover | `(hover: hover)` |
| `pointer` | Precision of the primary input | `(pointer: fine)` |
| `prefers-reduced-motion` | User asked for less animation | `(prefers-reduced-motion: reduce)` |
This query adds hover styling only on devices that can hover precisely:
@media (hover: hover) and (pointer: fine) {
.menu-item:hover {
background: #eef;
}
}Common errors
- Missing viewport tag. Queries seem to ignore the phone's real width.
- Pixel values for breakpoints. They do not respond to the user's default text size.
- Overlapping ranges. Using
max-width: 80emnext tomin-width: 80emmeans both blocks apply at exactly 80em. - Wrong source order. A later, broader rule can override an earlier query block of equal specificity. Place query blocks after the base rules they modify.
Exercise
A stylesheet has .nav { display: none; } followed by @media (min-width: 60em) { .nav { display: flex; } }. At a viewport width of 60em, is the nav visible? What if the media block is placed before .nav { display: none; } in the file?
Show answer
At exactly 60em the query matches, so the nav is display: flex and visible. If the media block comes before the base rule, both rules have equal specificity and the later one wins. The nav would then be hidden at every width, because display: none comes last.