Sub topic 5
Size conditions, range syntax, and and/or/not
This chapter covers the condition that goes after @container. You pick which size features to test, write the comparison in the legacy or the range form, and combine tests with and, or, and not. It assumes you can already declare a container with container-type and write a basic @container rule. It also assumes you know @media queries and CSS length units.
What a size condition tests
A size condition compares a measurement of the query container's content box to a value. The container is the nearest matching ancestor, not the element you are styling. The styles inside an @container block apply to descendants of the container only. The container itself cannot restyle itself this way.
These size features are available:
| Feature | What it measures | Needs `container-type` |
|---|---|---|
| `width` | Physical width of the content box | `size` or `inline-size` (in horizontal writing modes) |
| `height` | Physical height of the content box | `size` |
| `inline-size` | Size along the inline axis | `size` or `inline-size` |
| `block-size` | Size along the block axis | `size` |
| `aspect-ratio` | Width divided by height | `size` |
| `orientation` | `portrait` or `landscape` | `size` |
Prefer inline-size and block-size when your layout should survive a change of writing mode. Use width and height when you mean the physical axes.
The legacy form: min-width and max-width
The min- and max- prefixes work the same way as in @media:
.card-wrapper {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
}
@container (min-width: 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}
@container (max-width: 399.98px) {
.card {
font-size: 0.9rem;
}
}min-width: 400px is inclusive, so it matches at exactly 400px. max-width is inclusive too. That is why the second rule above uses 399.98px. If you write max-width: 400px, both rules apply at exactly 400px. Their declarations then fight, and source order decides which one wins.
Range syntax
Range syntax uses comparison operators, so you can avoid the off-by-a-fraction workaround:
| Operator | Meaning |
|---|---|
| `<` and `>` | Strictly less or greater than |
| `<=` and `>=` | Less or greater than, or equal |
| `=` | Equal |
@container (width >= 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}
@container (width < 400px) {
.card {
font-size: 0.9rem;
}
}The two rules above never overlap. At 400px only the first applies, and below 400px only the second does.
You can also write a two-sided range in one condition. The feature goes in the middle, and both operators must point the same way:
@container (400px <= width <= 700px) {
.card {
padding: 1.5rem;
}
}
@container (400px < width < 700px) {
.card {
padding: 1.25rem;
}
}Mixing directions, such as (400px < width > 700px), is invalid. The browser drops the whole rule.
The two forms are interchangeable. The table shows how to translate between them.
| Legacy | Range |
|---|---|
| `(min-width: 400px)` | `(width >= 400px)` |
| `(max-width: 700px)` | `(width <= 700px)` |
| `(min-width: 400px) and (max-width: 700px)` | `(400px <= width <= 700px)` |
| `(min-height: 300px)` | `(height >= 300px)` |
Pick one form per codebase. The range form reads more clearly, and it is the only form that gives you exclusive bounds.
Combining conditions with and, or, not
Each condition goes in parentheses, and the keywords join them.
.card-wrapper {
container-type: size;
}
@container (width > 400px) and (height > 200px) {
.card {
grid-template-columns: 8rem 1fr;
}
}
@container (width > 700px) or (height > 500px) {
.card {
padding: 2rem;
}
}
@container not (width < 400px) {
.card h2 {
font-size: 1.5rem;
}
}This example uses container-type: size, because two of the conditions test height. With inline-size the height tests could not be evaluated.
The mixing rule
You cannot put and and or side by side at the same level. The browser treats this as invalid and ignores the rule:
/* Invalid: and and or at the same level */
@container (width > 400px) and (height > 200px) or (width > 900px) {
.card {
padding: 2rem;
}
}Add parentheses to make the grouping explicit:
@container ((width > 400px) and (height > 200px)) or (width > 900px) {
.card {
padding: 2rem;
}
}The same applies to not. It negates exactly one condition. To negate a pair, wrap the pair first:
@container not ((width > 400px) and (height > 200px)) {
.card {
padding: 0.5rem;
}
}Writing not (a) and (b) is invalid.
Units inside conditions
Conditions accept any length. Relative units resolve against the query container, not the element being styled:
@container (width >= 30em) {
.card {
grid-template-columns: 8rem 1fr;
}
}Here em uses the container's font size. rem still uses the root font size. If you want the breakpoint to follow the user's text size, rem is the more predictable choice. Percentages are not useful here, because there is no basis to resolve them against.
Ratio and orientation
On a container-type: size container you can also test the shape of the box:
.media-wrapper {
container-type: size;
width: 100%;
height: 20rem;
}
@container (aspect-ratio > 1 / 1) {
.media {
flex-direction: row;
}
}
@container (orientation: portrait) {
.media {
flex-direction: column;
}
}Be careful with container-type: size. It requires you to give the container an explicit height from outside, because the container's size can no longer depend on its children in either axis. A later chapter covers this in detail.
How the browser evaluates a rule
The check runs again whenever the container's size changes. This includes resizing caused by sibling content, not only window resizing.
A complete example
This version uses three tiers of layout. It combines a range, an or, and a negated condition, and it is safe to paste into a page:
.card-wrapper {
container-type: inline-size;
}
.card {
display: grid;
gap: 1rem;
padding: 1rem;
}
.card h2 {
font-size: 1.1rem;
}
@container (width >= 400px) {
.card {
grid-template-columns: 8rem 1fr;
}
}
@container (500px <= width < 800px) {
.card {
padding: 1.5rem;
}
}
@container (width >= 800px) {
.card {
padding: 2rem;
gap: 2rem;
}
}
@container not (width < 600px) {
.card h2 {
font-size: 1.5rem;
}
}The ranges 500px <= width < 800px and width >= 800px touch at 800px but do not overlap. The not (width < 600px) rule is the same as width >= 600px. In real code you would write the second form, and not is more useful for negating a compound condition.
Common mistakes
- Querying
heightin aninline-sizecontainer. The rule never matches. - Mixing
andwithorwithout parentheses. The browser silently drops the rule. - Writing
(min-width: 400px) (max-width: 700px)with no keyword between the conditions. This is invalid. Useand, or switch to a range. - Expecting the container to restyle itself. Move the styled element one level down.
- Using overlapping inclusive bounds and then wondering which declaration wins at the boundary.
Exercise
A wrapper has container-type: inline-size. You write @container (width > 700px) or (height > 500px) { .card { padding: 2rem; } }. A teammate says the height half can never be true. Why, and how do you fix it? Also, rewrite (min-width: 400px) and (max-width: 700px) in range syntax with an exclusive upper bound.
Show answer
An inline-size container only has containment on the inline axis, so it cannot answer height questions. That half of the condition is unknown and never contributes a match. To make height queryable, change the wrapper to container-type: size and give it an explicit height. The range form with an exclusive upper bound is (400px <= width < 700px).
Exercise
Is @container (width > 400px) and (height > 200px) or (width > 900px) valid? If not, write a valid version that applies when the container is wider than 400px and taller than 200px, or wider than 900px.
Show answer
It is invalid, because and and or cannot appear at the same level without parentheses. A valid version is @container ((width > 400px) and (height > 200px)) or (width > 900px). The container must use container-type: size so that height can be evaluated.