SyntaxStudy
Sign Up
CSS State-Based Variables
CSS Intermediate 3 min read

State-Based Variables

State Variables

Change CSS variables to reflect UI state — open/closed sidebars, active nav items — instead of toggling many classes.

Example
.sidebar { --w: 240px; width: var(--w); transition: width 0.3s; }
.sidebar.collapsed { --w: 60px; }
.nav-item[aria-current="page"] { --bg: var(--primary-light); background: var(--bg); }
Pro Tip

One variable change can update multiple properties at once across the component.