CSS Variables & Animations — Essential Vocabulary for Modern UI
CSS Variables & Animations — Essential Vocabulary for Modern UI
🎯 After reading this lesson
Once you've finished this lesson, you'll be able to confidently do the following three things.
- ▸✅ Implement dark mode using CSS Custom Properties (--var)
- ▸✅ Understand the difference between transition and @keyframes
- ▸✅ Handle accessibility with prefers-reduced-motion
Keep the learning objectives as a checklist, and close the lesson once you can answer all of them.
CSS Variables (Custom Properties) — `--name`
Basic Usage
:root is the global scope. Reference it anywhere with var(--name).
Component Scope
Variables can be redefined per element. Child elements can override variables defined by their parent.
Dark Mode — Just change one variable
No need to touch dozens of colors individually — theme switching made simple. Tailwind's dark: variant automates exactly this.
prefers-color-scheme — Follow System Settings
Automatically detects the user's OS setting. Use when a manual toggle isn't needed.
Integration with JS
Tailwind + CSS Variables
Tailwind v4 is entirely CSS variable-based. bg-primary automatically becomes bg: var(--color-primary).
transition — Smooth State Changes
The Simplest Form
Property / duration / timing function — these three are the core of transition.
Multiple Properties at Once
Card lifts slightly on hover + deeper shadow — the standard pattern in modern web design.
Timing Functions
- ▸linear — constant speed
- ▸ease (default) — accelerate then decelerate
- ▸ease-in — starts slow
- ▸ease-out — starts fast, ends gently (UI standard)
- ▸cubic-bezier(...) — custom
UI transitions almost always use ease-out. The naturalness of "quick start + smooth landing."
The Pitfall of transition: all
Explicitly listing properties is better:
Which Properties Are Animatable
Only those that can be interpolated numerically. display, visibility, and background-image are not animatable. opacity, transform, color, width, and height are animatable.
Properties with good GPU acceleration: transform and opacity (performance winners). Changing width/height triggers layout recalculation, making it slower.
transform — translate / scale / rotate
Faster than changing margin/padding — composited directly by the GPU.
@keyframes — Complex Animations
Basic Structure
Define 0% → 100% (from → to). CSS handles the interpolation automatically.
When You Need Intermediate Steps
Loading Spinner — Real-world Example
This is the spinner used on every SaaS site.
Full animation Shorthand
Shorthand. The expanded form uses 6 individual properties:
prefers-reduced-motion — Accessibility Essential
Disabling motion for users with vertigo or vestibular disorders. Directly impacts your accessibility score.
🤖 Try asking AI like this
- ▸"Make this button float up slightly and deepen the shadow on hover."
- ▸"Create a CSS loading spinner. 360-degree rotation, 0.8 seconds, infinite loop."
- ▸"Add prefers-reduced-motion support."