Container style queries: component variants from a custom property
Size container queries ask “how wide is my container?”. Style queries ask a different question: “what is the value of a custom property on my container?”. A card can then switch to its featured layout because a parent section sets --ha-variant: featured, without an extra class on every card and without JavaScript that copies data attributes into classes.
Problem: A block theme has cards that look different in a “featured” section: larger title, accent border, image on the side. Today every card in that section needs the class is-featured, editors forget it, and a script adds it after the page loads, so the layout jumps.
Solution: Set a custom property such as --ha-variant: featured on the section, and style the cards inside with @container style(--ha-variant: featured). Keep the default card style as the fallback for browsers without style query support.
The basics
.ha-section--featured {
--ha-variant: featured;
}
/* default card */
.ha-card {
padding: 1rem;
border: 1px solid #e5e7eb;
}
/* the card inside an element where --ha-variant is "featured" */
@container style(--ha-variant: featured) {
.ha-card {
padding: 1.5rem;
border-inline-start: 4px solid var(--wp--preset--color--primary);
}
.ha-card__title {
font-size: var(--wp--preset--font-size--large);
}
}
Unlike size queries, style queries need no container-type: every element is a style container. The query looks at the nearest ancestor (or the element itself for its children), and because custom properties inherit, a value set on the section reaches all cards inside. The styled element must be a descendant of the container that is queried; a card cannot restyle itself based on its own property, only its children can.
The value from the editor
In a block theme, the section is a Group block. A block style variation gives editors a choice in the sidebar, and the variation only sets the property:
add_action( 'init', function (): void {
register_block_style( 'core/group', array(
'name' => 'ha-featured',
'label' => __( 'Featured section', 'textdomain' ),
'inline_style' => '.wp-block-group.is-style-ha-featured { --ha-variant: featured; }',
) );
} );
Every card in a group with this style gets the featured look, also cards added later by a Query Loop. The variation itself carries no layout rules, so the card component keeps all its styles in one place.
Combine with size queries
.ha-cards {
container-type: inline-size;
}
@container (min-width: 40rem) {
@container style(--ha-variant: featured) {
.ha-card {
display: grid;
grid-template-columns: 2fr 3fr;
}
}
}
The image goes to the side only when the card list is wide enough and the section is featured. Size queries need container-type on the ancestor; how they work: CSS container queries.
Limits
- Only custom properties can be queried in current browsers.
style(display: flex)or other normal properties are part of the specification but not implemented. - The comparison is a string comparison of the computed value:
--ha-variant: featuredand--ha-variant: "featured"are different values. - Ranges like
style(--ha-level > 2)are not supported yet.
NOTE: Style queries for custom properties work in Chrome and Edge 111 (March 2023) and Safari 18 (September 2024); Firefox does not support them yet (early 2025). There the cards keep their default style, which is why the default must be a complete, good-looking card and the featured look only an enhancement. Custom properties in general: CSS custom properties in WordPress themes.