Sub topic 5
Size conditions, range syntax, and and/or/not
This chapter assumes you can already declare a query container with container-type and write a basic @container rule. You should also know the @media range syntax, because container queries reuse it.
What a size condition tests
A size condition compares a feature of the container's content box against a value. The features you can test are width, height, inline-size, block-size, and aspect-ratio. orientation also works.
The container's container-type limits which of these you can query:
| `container-type` | Queryable features |
|---|---|
| `inline-size` | `width`, `inline-size` (in horizontal writing modes, the inline axis is the horizontal one) |
| `size` | all size features, including `height` and `block-size` |
| `normal` | none (the container only supports style queries) |
If you query a feature the container does not expose, the condition is false. It does not raise an error, so the rule silently never applies.
The legacy min and max form
The min- and max- prefixes work the same way as in media queries.
@container (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}This matches when the container is 400px wide or wider. The comparison is inclusive.
Range syntax
Range syntax uses comparison operators instead of prefixes: <, <=, >, >=, and =. It is easier to read, and it can express exclusive bounds, which min- and max- cannot.
@container (width > 700px) {
.card {
padding: 2rem;
}
}You can also put the feature between two bounds to describe an interval.
@container (400px <= width <= 700px) {
.card {
padding: 1.5rem;
}
}Both bounds must point the same way. (400px <= width >= 700px) is invalid and the whole query is ignored.
Combining conditions with and, or, and not
Logical keywords let you build larger conditions. Each simple condition stays in its own parentheses.
@container (width > 700px) or (height > 500px) {
.card {
padding: 2rem;
}
}
@container not (width < 400px) {
.card h2 {
font-size: 1.5rem;
}
}The height test in the first rule only works if the container uses container-type: size. With inline-size, that half is false, so the rule behaves as if it only tested the width.
@container (min-width: 300px) and (max-width: 600px) {
.card {
gap: 0.5rem;
}
}The grammar has one strict rule: you cannot mix and and or at the same level. (a) and (b) or (c) is invalid. Add parentheses to make the grouping explicit.
@container ((width > 400px) and (height > 300px)) or (aspect-ratio > 2) {
.card {
padding: 2rem;
}
}not applies to one condition only and cannot be chained with and or or at the same level. Write not ((a) and (b)) instead of not (a) and (b).
How a query is evaluated
Common errors
- Querying height with
inline-size. The condition is false. Switch tocontainer-type: sizeonly if you need it, becausesizecontainment requires the container to have an explicit height. - Mixing
andwithor. The query is invalid. Add parentheses. - Missing parentheses around a single test.
@container width > 400pxis invalid. Write@container (width > 400px). - Styling the container itself. An
@containerrule cannot change the element that is the container. It only affects descendants.
Exercise
A container uses container-type: inline-size. Will @container (width > 700px) or (height > 500px) apply to a descendant when the container is 600px wide and 800px tall? Explain.
Show answer
No. The height feature is not available with inline-size, so that half of the condition is false. The width is 600px, which is not greater than 700px, so both halves are false and the rule does not apply. Use container-type: size to make the height test work.
Exercise
Rewrite (min-width: 400px) and (max-width: 700px) in range syntax, then change it so that exactly 700px is excluded.
Show answer
(400px <= width <= 700px) is the equivalent. To exclude 700px, use (400px <= width < 700px).