CSS Variables & Animations
Custom properties, transitions for smooth state changes, and keyframe animations
Free tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.
UpgradeKnowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
CSS custom properties (variables) and animations are the tools that separate a polished UI from a static mockup.
CSS Custom Properties (Variables):
css /* Declare on :root to make globally available */ :root { --color-primary: #6366f1; --color-text: #1e293b; --radius: 8px; --spacing-md: 16px; } /* Use with var() */ button { background: var(--color-primary); border-radius: var(--radius); padding: var(--spacing-md); /* Fallback if variable isn't defined */ color: var(--color-text, black); }
Variables are scoped — you can redefine them on any element and children inherit the new value: ``css .dark-theme { --color-primary: #818cf8; /* lighter for dark backgrounds */ --color-text: #f1f5f9; }
Transitions — smooth state changes:
css .button { background: var(--color-primary); transition: background 200ms ease, transform 150ms ease; /* property duration timing-function */ } .button:hover { background: #4f46e5; /* darker on hover */ transform: translateY(-2px); /* slight lift */ } /* Transition shorthand: all properties */ .card { transition: all 300ms ease; } /* Prefer listing specific properties — "all" transitions can be slow */
Common timing functions:
| Value | Behavior | |---|---| | ease | Slow start, fast middle, slow end (default) | | linear | Same speed throughout | | ease-in | Starts slow, ends fast | | ease-out | Starts fast, ends slow (best for exits) | | ease-in-out | Slow at both ends (best for entrances) | | cubic-bezier() | Custom easing curve |
Keyframe Animations:
css /* Define the animation */ @keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } } @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } /* Apply it */ .modal { animation: fadeIn 300ms ease-out both; /* name duration timing fill-mode */ } .spinner { animation: spin 1s linear infinite; }
`transform` — hardware-accelerated, no layout reflow:
css transform: translateX(20px); /* move horizontally */ transform: translateY(-50%); /* move vertically */ transform: scale(1.05); /* zoom */ transform: rotate(45deg); /* rotate */ /* Chain multiple transforms */ transform: translateY(-4px) scale(1.02);
Examples
Design token system with dark mode
One set of variable redefinitions in dark mode updates every component automatically — no per-component dark-mode selectors needed
/* Design tokens in :root */
:root {
--bg: #ffffff;
--bg-surface: #f8fafc;
--text-primary: #0f172a;
--text-secondary: #64748b;
--accent: #6366f1;
--accent-hover: #4f46e5;
--radius-md: 8px;
--shadow: 0 1px 3px rgba(0,0,0,0.1);
--transition: 200ms ease;
}
/* Dark mode: just redefine the variables */
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f172a;
--bg-surface: #1e293b;
--text-primary: #f1f5f9;
--text-secondary: #94a3b8;
--accent: #818cf8;
}
}
/* Components use tokens — automatically adapt to dark mode */
.card {
background: var(--bg-surface);
color: var(--text-primary);
border-radius: var(--radius-md);
box-shadow: var(--shadow);
transition: box-shadow var(--transition);
}
.card:hover {
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}Loading spinner and page entrance animation
`animation-delay` on `:nth-child` creates a stagger effect; always disable animations for `prefers-reduced-motion` users
/* Spinning loader */
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner {
width: 24px;
height: 24px;
border: 3px solid #e2e8f0;
border-top-color: #6366f1;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
/* Staggered card entrance */
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(16px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card {
animation: slideUp 400ms ease-out both;
}
/* Stagger each card using animation-delay */
.card:nth-child(1) { animation-delay: 0ms; }
.card:nth-child(2) { animation-delay: 100ms; }
.card:nth-child(3) { animation-delay: 200ms; }
/* Respect reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
.card, .spinner { animation: none; }
}How well did you understand this?