CSS just got an “if” statement (available now in Chrome)

Introduction and Sponsor Message 00:00

  • CSS has introduced a native if statement, joining JavaScript in supporting conditional logic.
  • This new feature dramatically increases the composability and complexity possible directly in CSS, reducing the need to toggle classes or use JavaScript for conditional styling.
  • A brief sponsorship segment highlights Frontend Masters as a quality source for programming education taught by industry experts.

Overview of the CSS if Function 02:43

  • The CSS if function is available in Chrome 137 and lets developers write logical styles based on custom property values and style queries.
  • A common use case is switching styles based on themes like light or dark mode, but it also allows for more complex conditionals, such as reflecting dynamic UI states.
  • Example: Using data attributes (like data-status) on elements, custom properties can be set and used in style queries.

Practical Implementation Example 03:08

  • Demo shows cards with different statuses (pending, complete, inactive) styled by reading data-status attributes and converting them into custom properties via the new attr() function.
  • The if function in CSS enables setting border color, background color, and grid column based on the card’s status—all without toggling classes.
  • This conditional styling acts more like a switch statement, selecting the right value among several options.
  • Assignments like grid column can now visually reorder DOM elements with CSS logic alone.

Accessibility and Reading Flow 06:06

  • Visually reordering DOM elements with CSS once caused screen reader issues due to mismatch between visual and source order.
  • Chrome now introduces a reading-flow property, allowing screen readers to follow the visual column order instead of the DOM order, improving accessibility for dynamic layouts.

Complex Demo: Button States and Responsiveness 07:46

  • Another demo showcases a button that conditionally changes its appearance both on hover, focus, and active state, as well as on window resize.
  • Global custom properties such as hover, focus-visible, and active are set and used in if statements for conditional styling.
  • The logic allows background colors to be assigned based on the specific interaction or responsive condition.
  • Compared to the old approach—using multiple class selectors and relying on selector order—this method centralizes logic, making outcomes more predictable and maintainable.

Benefits of the CSS if Function 10:18

  • The if function removes complications from selector order or multiple class names, enabling intuitive, single-location logic for conditionals.
  • Inline styles now work more naturally with conditionals, allowing complex behaviors without the need for classes or JavaScript.
  • Media queries and support queries can also be integrated into if statements, enabling adaptation to device characteristics or feature support without extra wrappers or class declarations.
  • Previously complex tasks like switching colors for pointer types or color schemes are simplified with the new if function.

Advanced Conditional Styling and Practical Advantages 13:11

  • Multiple, independent conditions can be handled elegantly in CSS, such as prioritizing an “overdue” state over “completed” status with simple reordering in the if statement.
  • Conditional logic can mix different data attributes or user/device states for nuanced component behavior.
  • This approach avoids duplicating class definitions and JavaScript logic for UI variants, making styling systems easier to manage and scale.

Conclusion and Future Outlook 16:52

  • The new CSS if function potentially reduces reliance on frontend frameworks for state-based UI styling by making browser primitives more flexible and combinable.
  • Inline styles and dynamic property checks become more powerful and convenient.
  • Presenter is enthusiastic about the future of CSS with these advancements, though notes continued use of existing frameworks.
  • Viewers are invited to share their opinions on the utility of if statements in CSS.