Sub topic 7
Nesting @media and other at-rules
This chapter assumes you know media queries, the @supports rule, and how a style rule's selector decides which elements it styles. You should also know the & nesting selector from earlier chapters.
Conditional at-rules inside a style rule
Native nesting lets you put a conditional at-rule inside a style rule. Without nesting, a component's responsive styles sit far from its base styles:
.panel {
display: block;
}
@media (min-width: 40rem) {
.panel {
display: grid;
grid-template-columns: 1fr 2fr;
}
}With nesting, the condition lives inside the rule it changes:
.panel {
display: block;
@media (min-width: 40rem) {
display: grid;
grid-template-columns: 1fr 2fr;
}
}Both versions produce the same result. Inside the nested @media, bare declarations apply to the parent selector, .panel. You don't write & { ... } around them, although that form also works.
Which at-rules can nest
The at-rules that hold style rules or declarations can nest inside a style rule. The common ones are:
@media@supports@container@layer@scope
Rules that define global resources, such as @font-face, @keyframes, and @import, cannot nest. Keep them at the top level.
Nested selectors inside an at-rule
An at-rule can hold both declarations and nested style rules. The nested rules are scoped to the parent selector as usual:
.card {
padding: 1rem;
@media (min-width: 40rem) {
padding: 2rem;
& .title {
font-size: 1.5rem;
}
}
}At 40rem and wider, .card gets padding: 2rem, and .card .title gets the larger font size.
Conditions can stack
You can nest at-rules inside each other. Each level adds a condition, and every condition must hold for the declarations to apply:
.panel {
display: block;
@media (min-width: 40rem) {
display: grid;
@supports (container-type: inline-size) {
container-type: inline-size;
}
}
}Here container-type applies only when the viewport is at least 40rem wide and the browser supports container-type.
Order still matters
Nested at-rules follow the normal cascade. The nested block does not get extra weight because of where it sits. Its declarations have the same specificity as the parent rule's declarations, so the later one wins when both apply.
.banner {
@media (min-width: 40rem) {
background: navy;
}
background: gray;
}At 40rem and wider, the banner is gray. Both declarations have the same specificity, and background: gray comes later in source order. Put base declarations first and the conditional blocks after them.
Common errors
- Writing the selector again inside
@media. Repeating.panel { ... }inside the nested block works, but it is redundant. Use bare declarations. - Nesting
@keyframesor@font-face. These are not valid inside a style rule and the browser ignores them. - Putting the base styles last. A later base declaration overrides an earlier nested one with equal specificity.
- Expecting
@mediato change the selector. The at-rule adds a condition. It never changes which elements the rule matches.
Exercises
Exercise
In the rule below, what is the background color at a viewport width of 50rem, and why? .box { @media (min-width: 40rem) { background: green; } background: red; }
Show answer
Red. Both declarations apply to .box with the same specificity, so the later one in source order wins. To make green win at 40rem and wider, put the base declaration first and the nested @media after it.
Exercise
Rewrite this CSS using nesting so the @media block lives inside the .menu rule. .menu { display: none; } @media (min-width: 60rem) { .menu { display: flex; } }
Show answer
.menu { display: none; @media (min-width: 60rem) { display: flex; } }