CSS @when At-Rule
The proposed @when at-rule describes conditional CSS rules in an evolving draft.
It is not a reliable production feature. Use established conditional at-rules such as @media, @supports, and @container for working stylesheets.
Proposed Syntax
/* Proposed syntax - not for production use */
@when media(width >= 700px) {
.panel {
display: grid;
}
}
This is an illustration of the proposed conditional syntax, not a supported production pattern. CSS proposals can change before implementation.
How @when Is Intended to Work
The proposed @when rule would conditionally apply nested CSS based on a query. Related proposals have explored ordered conditional branches using @else.
The proposal is distinct from the established @media, @supports, and @container rules.
Proposal Status
@when is experimental draft syntax rather than a widely implemented at-rule. Browsers that do not recognize it ignore the rule and its nested declarations.
Do not place essential styling exclusively inside a proposed @when block.
Intended Conditional Chains
/* Illustrative draft syntax */
@when media(width >= 900px) {
.layout { grid-template-columns: 1fr 1fr 1fr; }
}
@else {
.layout { grid-template-columns: 1fr; }
}
This illustrates the motivation for mutually exclusive branches. It should not be copied into a production stylesheet because support and final syntax are not established.
Use Supported Alternatives
@media (min-width: 900px) {
.layout {
grid-template-columns: repeat(3, 1fr);
}
}
@supports (display: grid) {
.layout {
display: grid;
}
}
Use @media for viewport or media conditions, @supports for feature queries, and @container for supported container queries.
Progressive Enhancement
.layout {
display: block;
}
@supports (display: grid) {
.layout {
display: grid;
grid-template-columns: 1fr;
gap: 12px;
}
@media (min-width: 900px) {
.layout {
grid-template-columns: repeat(3, 1fr);
}
}
}
The default block layout remains readable when Grid is unavailable. The enhanced layout responds to viewport width in browsers supporting the tested features.
@when Alternative Example
This working example demonstrates the intended responsive result using established CSS rules rather than the unsupported proposal.
HTML
<div class="when-demo-layout">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
CSS
.when-demo-layout {
display: grid;
grid-template-columns: 1fr;
gap: 12px;
}
@media (min-width: 900px) {
.when-demo-layout {
grid-template-columns: repeat(3, 1fr);
}
}
Resize the viewport to see the supported @media condition change the number of columns. This result does not use @when.
Browser Support
@when is not a broadly supported production CSS feature. Browsers generally ignore unrecognized at-rules. Check current compatibility information at Can I Use? . Check compatibility for the supported alternatives individually.
Specifications
The proposed syntax has been discussed in CSS Conditional Rules Module Level 5. Draft material is subject to revision.
