Inline conditional styling arrives

Chrome 137 ships the CSS if() function. That is a quick turnaround for a feature the CSSWG resolved to add less than a year ago, since constructs unlike anything else in CSS usually spend years in development before reaching browsers.

Conditionals are nothing new in CSS — at-rules and selector matching have always been conditional, and Lea Verou has noted that every selector is essentially a conditional. What was missing is a way to test multiple conditions on a single line and return a result from that test.

.element {
  color: if(style(--theme: dark): oklch(52% 0.18 140); else: oklch(65% 0.05 220));
}

Syntax and evaluation

The earliest proposal from 2021 used the following form:

<if()> = if( <container-query>, [<declaration-value>]{1, 2} )

What shipped looks like this:

<if()> = if(
  [<if-statement>: <result>]*;
  <if-statement>: <result> ;?
)

The leading <if-statement> entries hold conditions wrapped in style(), media() or supports(), and there is no cap on how many you write. The closing else statement supplies the default value when none of the preceding conditions match. The spec's formal notation is denser:

<if()> = if( [ <if-branch> ; ]* <if-branch> ;? )
<if-branch> = <if-condition> : <declaration-value>?
<if-condition> = <boolean-expr[ <if-test> ]> | else
<if-test> =
  supports( [ <ident> : <declaration-value> ] | <supports-condition> )
  media( <media-feature> | <media-condition> ) |
  style( <style-query> )

A basic example: --theme is compared against a value inside a style() wrapper, and the property resolves to whatever the matching branch Returns.

.element {
  padding: if(style(--theme: dark): 2rem; else: 3rem);
}

Here the padding is 2rem if --theme is dark, otherwise it falls back to 3rem.

Readability takes work

Nested conditions grow unreadable quickly — even three stacked statements are hard to scan:

:root {
  --height: 12.5rem;
  --width: 4rem;
  --weight: 2rem;
}

.element {
  height: if(
    style(--height: 3rem): 14.5rem; style(--width: 7rem): 10rem; style(--weight: 100rem): 2rem; else: var(--height)
  );
}

Formatting each branch on its own line makes the logic legible at a glance:

:root {
  --height: 12.5rem;
  --width: 4rem;
  --weight: 2rem;
}

/* This is much cleaner, don't you think? */
.element {
  height: if(
    style(--height: 3rem): 14.5rem; 
    style(--width: 7rem): 10rem; 
    style(--weight: 100rem): 2rem; 
    else: var(--height)
  );
}

Expect style conventions (or prettier tooling) to settle around this soon. A demo with an animated ball whose width shifts according to custom variable values shows several conditionals in action; it requires Chrome 137+ at the time of writing.

supports() and media() inside if()

supports() behaves conceptually like the @supports at-rule, except it returns a value rather than gating a block. Its argument is a property name plus value, such as display: flex. To test for backdrop-filter: blur(), the at-rule form is:

/* Fallback in case the browser doesn't support backdrop-filter */
.card {
  backdrop-filter: unset;
  background-color: oklch(20% 50% 40% / 0.8);
}

@supports (backdrop-filter: blur(10px)) {
  .card {
    backdrop-filter: blur(10px);
    background-color: oklch(20% 50% 40% / 0.8);
  }
}

With if(), the same check reads:

.card {
  backdrop-filter: if(
    supports(backdrop-filter: blur(10px)): blur(10px);
    else: unset
  );
}

Here unset acts as a graceful-degradation fallback. Multiple conditions can be chained across supports() and the other wrapper functions:

.card {
  backdrop-filter: if(
    supports(backdrop-filter: blur(10px)): blur(10px);
    supports(backdrop-filter: invert(50%)): invert(50%);
    supports(backdrop-filter: hue-rotate(230deg)): hue-rotate(230deg);;
    else: unset
  );
}

media() mirrors the @media at-rule. Its formal syntax from the spec uses the same <media-query> grammar:

/* formal syntax for @media */
@media <media-query-list> {
  <rule-list>
}

/* formal syntax for media() */
media( <media-feature> | <media-condition> )

Rather than wrapping a block of declarations as @media does, the function contributes a value inline. A width check looks like this:

h1 {
  font-size: if(
    media(width >= 1200px): 3rem;
    media(width >= 768px): 2.5rem;
    else: 2rem
  );
}

A simple screen-width query illustrates the idea:

h1 {
  font-size: 2rem;
}

@media (min-width: 768px) {
  h1 {
    font-size: 2.5rem;
  }
}

@media (min-width: 1200px) {
  h1 {
    font-size: 3rem;
  }
}

Single-browser support for now

Only the latest Chrome update supports if() as of writing; no timeline exists for other engines. Early experimentation is already underway:

  • "The What If Machine: Bringing the 'Iffy' Future of CSS into the Present" (Lee Meyer)
  • "How To Correctly Use if() In CSS" (Temani Afif)
  • "Future-Proofing Indirect Cyclical Conditions" (Roma Komarov)
  • "The new if() function in CSS has landed in the latest Chrome" (Amit Merchant)

Feedback on early usage belongs in the CSSWG and Chromium issue trackers — real-world use cases are what improve later implementations.