Day 51 - CSS native mixins and ternary function
Emiliano Pisu
?? Sensei & Co-Host @ DevDojo IT ?? Speaker ?? Accessibility WCAG Expert ? Turning Designs into interactive things ??♂? Your friendly neighborhood UI/UX Engineer
?? An Awesome CSS Link a Day – Day 51 ??
W3C drafted Custom Functions @function (CSS Mixins) and if() (ternary function)
Today, we explore one of the most exciting additions coming to CSS: the if() function! Traditionally, CSS has been a declarative language with no built-in conditional logic. But with if(), styles can now react dynamically, without preprocessors or JavaScript. NOTE: Actually available in Chrome Canary with the Experimental Web Platform Features flag enabled.
Introduction
For years, developers have relied on media queries, CSS variables, and JavaScript to implement conditional styles. Now, the if() function allows logic directly inside CSS, enabling scenarios such as:
All natively, without extra dependencies.
?? Theory: Custom Functions @function and if() function
The Custom Functions will be a real game-changer, in the example:
@function --negate(--value) {
result: calc(-1 * var(--value));
}
:root {
padding: --negate(1px); /* = -1px */
}
The if() function follows a simple conditional logic, and acts more like a switch:
/*
color: if(
CONDITION_1: VALUE_1;
CONDITION_2: VALUE_2;
...
CONDITION_N: VALUE_N;
else: FALLBACK_VALUE
);
*/
color: if(
style(--scheme: dark): var(--dark-color);
style(--scheme: dim): var(--dim-color);
else: var(--light)
);
This introduces true conditional styling in CSS, enabling:
When combined with @function, CSS gains even greater logical control over styles.
?? Today’s Link
For a deep dive into if(), @function, and the future of custom CSS functions, check out these articles:
?? ??????????????????
? Why This Rocks
??? ???????? ?????? ????????????????????????
Let’s discuss the future of logic-driven CSS! ??