Sub topic 5
Combining conditions with and, comma, and not
This chapter assumes you can write a basic @media rule with min-width or max-width, and that you know the cascade and the box model. Here you will combine several conditions in one query. You will also learn how and, the comma, not, and only are evaluated, and where they behave in surprising ways.
The three combinators at a glance
A single feature test such as (min-width: 40em) is often not enough. You may want a rule that applies only to screens, only within a width range, or only when one of several situations holds. CSS gives you these tools:
| Tool | Meaning | Example |
|---|---|---|
| `and` | Every condition must be true | `screen and (min-width: 30em)` |
| `,` (comma) | At least one query in the list must be true | `(max-width: 30em), print` |
| `not` | Inverts the result of the query | `not print` |
| `only` | Hides the query from very old browsers | `only screen and (min-width: 30em)` |
| `or` | At least one condition must be true (Media Queries Level 4) | `(hover: none) or (pointer: coarse)` |
The comma and and look similar but work at different levels. The comma separates complete queries. and joins parts inside one query.
Requiring every condition with and
Use and when all conditions must hold at once. This is the rule from the chapter's example. It hides a sidebar on screens between 30em and just under 60em wide:
@media screen and (min-width: 30em) and (max-width: 59.99em) {
.sidebar {
display: none;
}
}Three things to know:
- Each feature test goes inside its own parentheses.
screen and min-width: 30emis invalid, and the browser drops the whole rule. - An optional media type such as
screenorprintcomes first. After it you can chain as manyandparts as you need. andneeds whitespace on both sides.(min-width: 30em)and (max-width: 50em)does not parse reliably.
The 59.99em upper bound avoids an overlap. At exactly 60em, a rule written with max-width: 60em and another written with min-width: 60em would both match. The .01em gap works but looks odd. The range syntax chapter shows a cleaner form, width < 60em.
Matching any of several queries with a comma
A comma separates independent queries. The rule applies if any one of them matches:
@media (max-width: 30em), print {
.toolbar {
display: none;
}
}This hides the toolbar on narrow screens and also when printing. Each item in the list is evaluated on its own, so you can mix unrelated conditions.
The comma binds loosely
and groups more tightly than the comma. The following query is a common source of mistakes:
@media screen, print and (orientation: landscape) {
body {
font-size: 1.125rem;
}
}A reader might expect "screen or print, both in landscape". The browser reads two queries: screen (any orientation) and print and (orientation: landscape). To apply the orientation test to both, repeat it:
@media screen and (orientation: landscape),
print and (orientation: landscape) {
body {
font-size: 1.125rem;
}
}Inverting a query with not
In the traditional syntax, not goes at the very start of a query. It negates the whole query, not just the part that follows it:
@media not print {
.watermark {
display: block;
}
}This applies on every media type except print. The rule is simple here, but combined with and it trips people up:
@media not screen and (min-width: 40em) {
.promo {
display: none;
}
}This does not mean "not a screen, and at least 40em". It means not (screen and (min-width: 40em)). The rule applies to print, to narrow screens, and to anything else that does not match both parts.
Two more constraints apply:
notcannot appear in the middle of a traditional query.screen and not (color)is invalid there.notapplies to one query only. In a list, each comma-separated query needs its ownnot:
@media not print, not (min-width: 40em) {
.promo {
display: none;
}
}The only keyword
only is a historical guard. Browsers that did not understand media queries could misread @media screen and (min-width: 30em) as @media screen and apply the styles everywhere. Prefixing the query with only made those browsers ignore it:
@media only screen and (min-width: 30em) {
.nav {
display: flex;
}
}Every browser in current use handles media queries, so only changes nothing. It is harmless, and you will see it in older code. You do not need to add it. It must be followed by a media type, so only (min-width: 30em) is invalid.
Level 4 syntax: or and not inside parentheses
Media Queries Level 4 removes several of the limits above. It adds an or keyword and lets you put not in front of a single condition:
@media (hover: none) or (pointer: coarse) {
.menu-item {
padding-block: 0.75rem;
}
}
@media not (min-width: 40em) {
.sidebar {
display: none;
}
}The first rule applies to devices that cannot hover or whose primary pointer is coarse, such as a finger. The second applies below 40em. Browsers have supported this syntax for several years, but check your support targets before relying on it.
Level 4 also has strict grouping rules, which prevent the precedence confusion described earlier:
- You cannot mix
andandorat the same level. Wrap each group in parentheses. notcannot be followed byandororat the same level. Wrap the inner condition.
@media ((min-width: 40em) and (orientation: landscape)) or (min-width: 80em) {
.hero {
min-height: 60vh;
}
}
@media not ((min-width: 40em) and (orientation: landscape)) {
.hero {
min-height: 30vh;
}
}If you write (min-width: 40em) and (orientation: landscape) or (min-width: 80em) without the extra parentheses, the whole @media rule is invalid and ignored. Nothing in the console tells you.
A combined example
This stylesheet uses the combinators for different jobs. The page is a single column by default. It adds a sidebar layout on wide screens, and it enlarges tap targets on touch devices:
.layout {
display: grid;
gap: 1rem;
}
.sidebar {
display: none;
}
@media screen and (min-width: 48em) and (orientation: landscape) {
.layout {
grid-template-columns: 14rem 1fr;
}
.sidebar {
display: block;
}
}
@media (hover: none), (pointer: coarse) {
.layout a {
display: inline-block;
padding: 0.75rem 1rem;
}
}
@media print {
.sidebar {
display: none;
}
}Each block uses the combinator that matches its purpose:
- The layout block uses
andbecause all three conditions must hold. - The touch block uses a comma because either signal is enough to identify a touch-first device.
- The print block uses a single media type.
Media queries add no specificity. The print block wins over the earlier display: block for .sidebar only because it comes later in the source with equal specificity. Order matters when several queries can match at once.
Common mistakes
- Missing parentheses. Write
(min-width: 30em), nevermin-width: 30emafterand. - Reading a comma as "and". A comma means "or". If you need both conditions, use
and. - Negating more than intended.
notapplies to the entire query, including everything afterand. - Overlapping or gapping ranges. Check each boundary value so exactly one rule matches at that width.
- Mixing
andandorwithout grouping. The query becomes invalid and is dropped. - Using
orwithout checking support. For older browsers, write a comma list instead, which does the same thing.
Exercise
A stylesheet contains @media not screen and (min-width: 40em) { .promo { display: none; } }. On which devices is .promo hidden? How would you rewrite the query so it hides .promo only on screens narrower than 40em, using traditional syntax?
Show answer
The query means not (screen and min-width 40em). So .promo is hidden on print and other non-screen media types, and also on screens narrower than 40em. It stays visible only on screens 40em or wider. To hide it only on narrow screens, drop not and use max-width: @media screen and (max-width: 39.99em) { .promo { display: none; } }. The Level 4 form @media screen and (width < 40em) also works.
Exercise
Write one media query, using a comma list in traditional syntax, that applies to print and also to landscape screens at least 50em wide. Then explain why @media print, screen and (min-width: 50em) and (orientation: landscape) is correct, while @media screen and (min-width: 50em), print and (orientation: landscape) is not what you want.
Show answer
@media print, screen and (min-width: 50em) and (orientation: landscape). The comma splits the list into print and the full screen query, so the width and orientation tests apply only to screens. In the second form, the first query is screen and (min-width: 50em) with no orientation test. The second query is print and (orientation: landscape), which limits printing to landscape pages. Both conditions end up attached to the wrong media types.