Favorites

if() CSS Function

if()
CSS FunctionThis function is supported in some modern browsers, but not all.
Updated 21st November 2026

The if() function chooses a property value based on a condition, such as a style query, media query or supports test, written inline in the declaration.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The if() CSS function lets a property value depend on a condition, written inline inside the declaration. It works like a chain of "if ... else if ... else" statements: the function checks each condition in order and uses the value paired with the first one that is true. If none match and there is no else branch, the declaration becomes invalid at computed-value time, just like an unresolved var().

Each branch is a condition, a colon, a value and a semicolon. Three kinds of condition are available. style() tests the value of a custom property on the element, for example style(--size: large). media() tests a media feature, such as media(width >= 700px). supports() tests whether the browser supports a declaration. The keyword else always matches and is used as the final fallback. Conditions can be combined with and, or and not.

/* Component variants driven by a custom property */
.button {
  background: if(
    style(--variant: danger): #dc2626;
    style(--variant: success): #16a34a;
    else: #2563eb
  );
}

/* Responsive value without a separate media query block */
.card {
  padding: if(media(width >= 700px): 2rem; else: 1rem);
}

/* Use a modern color if supported */
.title {
  color: if(supports(color: oklch(0 0 0)): oklch(55% 0.2 260); else: #3b5bdb);
}

if() is a recent addition to CSS and is not yet supported in Firefox, so include a plain declaration before it as a fallback.

Interactive Demo

--theme: light
--theme: dark
--theme: brand
Demo Code
HTML Code
<div class="demo-element">
	<div style="--theme: light">--theme: light</div>
	<div style="--theme: dark">--theme: dark</div>
	<div style="--theme: brand">--theme: brand</div>
</div>
CSS Code
.demo-element {
	width: 260px;
}
.demo-element > div {
	padding: 12px;
	margin-bottom: 6px;
	border: 2px solid #4361ee;
	font-size: 14px;
	color: #888;
	background: if(style(--theme: dark): #1b1f3b; else: #e7ecff);
}

Syntax

if() = if( [ <if-branch> ; ]* <if-branch> ;? )

<if-branch> = <if-condition> : <declaration-value>?
<if-condition> = <boolean-expr[ <if-test> ]> | else
<if-test> = supports( ... ) | media( ... ) | style( ... )

Values

  • <if-condition>A test to evaluate: style() for custom property values, media() for media queries, supports() for feature detection, or else, which always matches. Tests can be combined with and, or and not.
  • <declaration-value>The value used when its condition is the first one that is true. It can be empty.

Example

<div class="wrap">
<h1>CSS if() examples</h1>
<div class="grid">
<div class="card">
<span class="btn">Default</span>
<div class="label">no --variant: else branch</div>
</div>
<div class="card">
<span class="btn" style="--variant: danger">Delete</span>
<div class="label">style(--variant: danger)</div>
</div>
<div class="card">
<span class="btn" style="--variant: success">Save</span>
<div class="label">style(--variant: success)</div>
</div>
<div class="card">
<span class="btn" style="--variant: warning">Caution</span>
<div class="label">style(--variant: warning)</div>
</div>
</div>
<p class="note">In browsers without if(), every button shows the blue fallback.</p>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* if() examples */
.btn {
display: inline-block;
padding: 10px 18px;
border-radius: 8px;
color: #fff;
font-weight: 600;
/* fallback for browsers without if() */
background: #2563eb;
background: if(
style(--variant: danger): #dc2626;
style(--variant: success): #16a34a;
style(--variant: warning): #d97706;
else: #2563eb
);
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS if() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported in some modern browsers, but not all.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.