
Inline Component Logic with the CSS if Conditional Function
Historically, expressing conditional logic in CSS required duplicating entire rule blocks across @media queries, @supports blocks, or verbose attribute selectors. Even after container style queries arrived, toggling two or three related design tokens inside a component still forced authors to scatter property overrides across nested @container style(...) rules, obscuring which values belonged to a single component API. That fragmentation made design-system variants harder to audit and encouraged utility-class bloat when styling state-driven components.
Introduced in CSS Values and Units Level 5 and shipping in Chromium 137+ and modern stylesheets by mid-2026, the native if() function brings concise ternary-style branching directly inside individual CSS property declarations. An if() expression accepts semicolon-separated condition-value pairs evaluated in order—combining style(), media(), and supports() tests with a final else: fallback—so a button, badge, or layout shell can resolve colors, spacing, and typography inline from a single custom property or viewport query. Paired with standard baseline declarations for progressive enhancement, if() keeps component styling declarative, localized, and easy to maintain.
Example
/* Inline property-level branching using style() and media() conditions */
.alert-banner {
--tone: info;
padding: if(
media(width >= 48rem): 1rem 1.5rem;
else: 0.75rem 1rem
);
background: if(
style(--tone: danger): #fef2f2;
style(--tone: success): #ecfdf5;
else: #eff6ff
);
color: if(
style(--tone: danger): #991b1b;
style(--tone: success): #065f46;
else: #1e40af
);
border-left: 4px solid currentColor;
}
Implementation
<!-- Variant and size controlled purely through custom properties or attributes -->
<div class="action-bar" role="group" aria-label="Document actions">
<button type="button" class="btn">Save Draft</button>
<button type="button" class="btn" data-tone="primary" data-size="lg">Publish</button>
<button type="button" class="btn" data-tone="danger">Delete</button>
</div>
/* ==========================================================================
Accessible Button Component with Native CSS if() and Progressive Fallback
========================================================================== */
.btn {
--tone: neutral;
--size: md;
/* Map semantic HTML attributes cleanly onto component tokens */
&[data-tone="primary"] { --tone: primary; }
&[data-tone="danger"] { --tone: danger; }
&[data-size="lg"] { --size: lg; }
/* Baseline fallback declarations for non-if() engines */
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 2.5rem;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
border: 1px solid #cbd5e1;
background: #f8fafc;
color: #0f172a;
font: 600 0.9375rem/1.2 system-ui, sans-serif;
cursor: pointer;
transition: filter 0.15s ease, transform 0.1s ease;
}
/* Progressive enhancement with CSS Values 5 inline if() */
@supports (color: if(style(--tone: primary): #ffffff; else: #000000)) {
.btn {
min-height: if(
style(--size: lg): 3rem;
media(pointer: coarse): 2.75rem;
else: 2.5rem
);
padding: if(
style(--size: lg): 0.75rem 1.5rem;
else: 0.5rem 1rem
);
font-size: if(
style(--size: lg): 1rem;
else: 0.9375rem
);
background: if(
style(--tone: primary): #1d4ed8;
style(--tone: danger): #b91c1c;
else: #f8fafc
);
color: if(
style(--tone: primary): #ffffff;
style(--tone: danger): #ffffff;
else: #0f172a
);
border-color: if(
style(--tone: primary): #1e40af;
style(--tone: danger): #991b1b;
else: #cbd5e1
);
}
}
.btn:hover {
filter: brightness(0.95);
}
.btn:active {
transform: translateY(1px);
}
.btn:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.btn {
transition: none;
}
.btn:active {
transform: none;
}
}
Main Point: The native CSS if function consolidates style, media, and feature branching into single property declarations, eliminating duplicated rule blocks while preserving progressive fallbacks.
Credits
Photo by Monica Valls on Unsplash