CSS if()関数ビルダーとは
The CSS if() function lets you write conditional values directly inside a property declaration. Instead of wrapping a value inside @media, @container, or @supports at-rules, you use if() inline: width: if(style(--variant: large): 300px else 150px). It supports three condition types: style() to check a CSS custom property value, media() to test media features like min-width or prefers-color-scheme, and supports() to detect whether the browser supports a specific CSS feature. Chrome 137 supports if(style()) and Chrome 142 adds if(media()) and if(supports()).
クイックアンサー
The CSS if() function lets you write conditional values inline inside a property declaration. Use if(style()) to branch on a CSS custom property, if(media()) to branch on a media feature like viewport width, or if(supports()) to branch on browser feature support. Chrome 137+ supports if(style()); Chrome 142+ adds if(media()) and if(supports()).
制限事項
- The if() function is only available in Chrome 137+ (style()) and Chrome 142+ (media(), supports()). Firefox and Safari do not yet support CSS if() in any stable release as of May 2026.
- if() evaluates a single condition for a single value — it cannot replace @media or @container at-rules for complex rule blocks with multiple declaration changes.
- if(style()) only works on explicitly set or registered custom properties. Custom properties inherited from an ancestor with no explicit value on the target element are treated as not matching.
使い方
- Choose the condition type: style() for checking CSS custom property values, media() for viewport or user-preference media features, or supports() for feature detection.
- Enter the condition expression and the value to use when the condition is true.
- Optionally add an else value that applies when the condition is false.
- Copy the generated if() declaration and place it directly in your CSS property value.
主な用途
- Set a different width based on a CSS custom property value without a @container query wrapping the entire rule.
- Apply a color scheme value conditionally based on prefers-color-scheme inside a single property declaration.
- Provide a fallback value when the browser does not support a specific CSS feature using supports().