Tevpro insights
CSS if(): Conditional Values and Browser Fallbacks
Try CSS if() in a working compact-card demo, inspect computed padding, and understand its selector-based fallback.
Key takeaways
- CSS if() selects a value within one CSS declaration using a condition, such as a custom-property style query.
- In the demo, comfortable padding is 1.5rem and compact padding is .75rem; an ordinary selector provides the same behavior without if().
- Support remains limited, so test the computed result as well as feature detection before depending on the new function.
CSS if() selects a value inside one CSS declaration. A compact card is a useful case: the component stays the same, but its padding changes based on a custom property.
1. Establish a baseline and an ordinary selector fallback
.card {
--density: comfortable;
padding: 1.5rem;
}
.card[data-density="compact"] { padding: .75rem; }
This is enough for every browser: the data attribute switches between comfortable and compact spacing. With a 16px root font, the values are 24px and 12px. That is also the behavior we want from if().
2. Replace the value in supporting browsers
@supports (padding: if(style(--density: compact): .75rem; else: 1.5rem)) {
.card,
.card[data-density="compact"] {
padding: if(style(--density: compact): .75rem; else: 1.5rem);
}
}
The style() condition checks --density on the card. The repeated compact selector matters: without it, the more specific fallback selector could win the cascade, making a supporting browser appear to use if() when it is not. The @supports block leaves the simpler rules intact elsewhere.
3. Change the input, not the padding
const card = document.querySelector('.card');
card.dataset.density = 'compact';
card.style.setProperty('--density', 'compact');
console.log(getComputedStyle(card).paddingTop); // 12px at a 16px root
The script changes the custom property for if() and the data attribute for the fallback. It never sets padding directly. Use a normal selector or media query when several properties or the whole layout must change; if() is most useful for a focused value choice.
Try the card below. Toggle its density and edit styles.css to compare the ordinary selector fallback with the CSS if() enhancement. The status line reports the computed padding, even when if() is unavailable.
Try conditional CSS padding
Toggle density and inspect the padding. Change the fallback or if() values in styles.css and see the difference.
For browser compatibility and the full specification, see the primary references in Sources below.
Sources
Why work with us
Why Tevpro?
Whether you’re a startup with a bold product idea or an established company seeking a stronger delivery partner, Tevpro delivers results. Our expert consultants specialize in building secure, scalable applications that simplify operations and drive real ROI.
FAQ
Frequently asked questions
It returns a value for the containing CSS property based on a condition. In the example, it returns one of two padding lengths.


