Day 51 - CSS native mixins and ternary function

Day 51 - CSS native mixins and ternary function

?? 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:

  • Adaptive layouts: "If the viewport is wider than X, adjust the design"
  • Theme-based styles: "If dark mode is enabled, change the colors"
  • Context-aware spacing: "If a value is positive, apply different margins"

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:

  • Responsive design without media queries
  • More flexible, self-contained components
  • New possibilities for color schemes, spacing, and layout logic

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:

?? ??????????????????

  • Code snippets showcasing if() in real-world scenarios
  • Comparison guide: if() vs. media queries—when to use what
  • Practical examples of if() combined with other modern CSS features
  • A preview of upcoming CSS custom functions and their potential

? Why This Rocks

  • Eliminates JavaScript reliance for many conditional styles
  • Reduces media query clutter, making stylesheets more readable
  • Paves the way for even more dynamic CSS logic
  • Brings CSS closer to functional programming paradigms

??? ???????? ?????? ????????????????????????

  • Do you see if() as a replacement for media queries or a complementary tool?
  • How would you leverage if() in your current projects?
  • What other logical functions would you love to see in CSS?
  • Could if() simplify component-based styling in design systems?

Let’s discuss the future of logic-driven CSS! ??

要查看或添加评论,请登录

Emiliano Pisu的更多文章

  • Day 52 - Reimagining Fluid Typography

    Day 52 - Reimagining Fluid Typography

    ?? An Awesome CSS Link a Day – Day 52 ?? Reimagining Fluid Typography: Are We Responding to the Right Inputs?…

    2 条评论
  • Day 50 - The Mightiest Selectors - :has() and :not() in Action

    Day 50 - The Mightiest Selectors - :has() and :not() in Action

    ?? An Awesome CSS Link a Day – Day 50 ?? The Mightiest Selectors - :has() and :not() in Action The CSS :has()…

    1 条评论
  • Day 49 - Smarter Number Inputs with step and pattern

    Day 49 - Smarter Number Inputs with step and pattern

    ?? An Awesome CSS Link a Day – Day 49 ?? Making Number Inputs Smarter with step and pattern Number inputs are a great…

  • Day 48 - Where Ancient HTML meets Modern CSS

    Day 48 - Where Ancient HTML meets Modern CSS

    ?? An Awesome CSS Link a Day – Day 48 ?? A Content List with Bulk Actions Using Ancient HTML and Modern CSS Ever…

  • Day 47 - The mighty powers of @view-transition

    Day 47 - The mighty powers of @view-transition

    ?? An Awesome CSS Link a Day – Day 47 ?? The View Transitions API: amazing Animations with no-effort What if smooth…

  • Day 46 - The Magic of Custom CSS Counters

    Day 46 - The Magic of Custom CSS Counters

    ?? An Awesome CSS Link a Day – Day 46 ?? The Magic of Custom CSS Counters: some thing you might not know about Custom…

  • Day 45 - CSS Masonry Layouts

    Day 45 - CSS Masonry Layouts

    ?? An Awesome CSS Link a Day – Day 45 ?? Unlocking the Power of CSS Masonry Layouts Say goodbye to JavaScript-heavy…

  • Day 44 - Animated Sign-Up Buttons

    Day 44 - Animated Sign-Up Buttons

    ?? An Awesome CSS Link a Day – Day 44 ?? Animated Sign-Up Buttons: Making CTAs Pop! Ever felt like your call-to-action…

  • Day 43 - HTML & CSS only One Time Password Input

    Day 43 - HTML & CSS only One Time Password Input

    ?? An Awesome CSS Link a Day – Day 43 ?? HTML & CSS only One Time Password Input: Elevate Your OTP Inputs with CSS…

  • Day 42 - Multi-Column Layouts

    Day 42 - Multi-Column Layouts

    ?? An Awesome CSS Link a Day – Day 42 ?? Mastering Multi-Column Layouts in CSS Say goodbye to clunky grid hacks and…