The CSS if() function: conditional values inside a declaration
Until now, CSS chose between values only through selectors and at-rules: a class, a media query, an @supports block, each repeating the property. The if() function puts the condition into the value itself. A component can switch a value based on a custom property, a media query or a feature test, in one declaration.
Problem: A theme’s button component has three sizes and two density modes, controlled by custom properties. Every combination needs its own selector, and the CSS for one button is 60 lines of repetition.
Solution: Use if( style( --ha-size: large ): 1.25rem; else: 1rem ) for values that depend on a custom property, if( media( ... ) ) for values that depend on the viewport, and keep a normal declaration before it as the fallback for browsers without if().
Conditions on custom properties
.ha-button {
--ha-size: medium;
padding-block: .5em; /* fallback */
padding-block: if(
style(--ha-size: small): .25em;
style(--ha-size: large): .75em;
else: .5em
);
font-size: 1rem;
font-size: if(
style(--ha-size: small): .875rem;
style(--ha-size: large): 1.25rem;
else: 1rem
);
}
.ha-button.is-small { --ha-size: small; }
.ha-button.is-large { --ha-size: large; }
The conditions are checked in order; the first true one wins, else is the default. style( --name: value ) compares the computed value of a custom property on the element, the same test as in container style queries, but without needing a container: the element tests itself. Container style queries for comparison: container style queries.
Media and feature conditions
.ha-card {
padding: 1rem;
padding: if( media(width >= 48rem): 2rem; else: 1rem );
background: #f3f4f6;
background: if(
supports(color: oklch(0 0 0)): oklch(96% .005 250);
else: #f3f4f6
);
}
media() and supports() inside if() work like @media and @supports, but only for this one value. For several properties that change together at a breakpoint, a normal media query stays more readable.
The fallback pattern
A browser that does not know if() treats the whole declaration as invalid at parse time and keeps the previous declaration of the same property. That is why every example above starts with a plain value: older browsers use it, newer ones override it. Do not put if() into a custom property and use it elsewhere: there the invalid value is only detected at computed time, and the property falls back to its initial value instead of the previous declaration.
In block themes
Block style variations can set the controlling custom property, and the component CSS reacts with if():
register_block_style( 'core/button', array(
'name' => 'ha-large',
'label' => __( 'Large', 'textdomain' ),
'inline_style' => '.wp-block-button.is-style-ha-large .wp-block-button__link { --ha-size: large; }',
) );
NOTE: if() shipped in Chrome and Edge 137 (May 2025). Check current support in other browsers before relying on it; with the fallback declaration first, browsers without support show the default values, so the function is safe to use as an enhancement. Typed custom properties that make conditions on numbers more predictable: CSS @property.