CSS Transitions, Transforms, & Custom Properties
Up to this point, our CSS rules have defined static layouts—elements that sit immovably on screen. But modern digital products—from Apple and Stripe to Thinkora—feel premium and responsive because of motion design.
In this chapter, you will master:
- CSS Custom Properties (Variables) for maintainable, switchable design systems
- 2D Transforms (
translate,scale,rotate) - Smooth Transitions for state hover and focus changes
@keyframesAnimations for autonomous, continuous motion
1. CSS Custom Properties (Variables)
Before CSS variables, if you wanted to change your brand color from blue (#2563eb) to purple (#7c3aed), you had to find and replace that hex code across hundreds of CSS lines.
CSS variables allow you to store values in reusable design tokens:
/* 1. Define global variables in the :root pseudo-class */:root { --primary-color: #2563eb; --accent-color: #f59e0b; --surface-bg: #ffffff; --text-main: #0f172a; --radius-lg: 16px; --glass-border: rgba(255, 255, 255, 0.15);}/* 2. Consume variables using the var() function */.btn-primary { background-color: var(--primary-color); color: #ffffff; border-radius: var(--radius-lg); padding: 12px 24px;}/* 3. Variables can provide fallback defaults: var(--name, fallback) */.card { background-color: var(--card-bg, #f8fafc);}Instant Dark Mode Switching with Variables
By re-assigning variable values inside a media query or a theme class, your entire page updates without modifying individual component rulesets:
@media (prefers-color-scheme: dark) { :root { --surface-bg: #0f172a; --text-main: #f8fafc; }}2. 2D Transforms: Moving Beyond the Box Model
The transform property alters an element's coordinate geometry without disrupting surrounding layout flow:
| Transform Function | Example | Visual Effect |
|---|---|---|
translate(x, y) | transform: translateY(-8px); | Shifts element up by 8px without pushing neighboring elements |
scale(factor) | transform: scale(1.05); | Grows element by 5% in size |
rotate(deg) | transform: rotate(45deg); | Rotates element clockwise |
skew(x, y) | transform: skewX(10deg); | Shears element along horizontal axis |
Chaining Multiple Transforms
You can combine multiple transformations in a single declaration:
.card:hover { /* Shifts up by 6px and expands slightly */ transform: translateY(-6px) scale(1.02);}3. Smooth Transitions: Animating State Changes
By default, when you hover over an element, CSS changes occur instantaneously with an abrupt visual snap. The transition property smooths the interpolation between the start and end values over time:
Instant Snap: [Normal State] ──(Snap!)──> [Hover State]
Smooth Transition:[Normal State] ──(0.3s ease)──> [Hover State]
Transition Sub-Properties
transition-property: Which properties to animate (e.g.transform,opacity,background-color).transition-duration: How long the animation takes (e.g.0.3sor300ms).transition-timing-function: The acceleration curve (ease,linear,ease-in,ease-out,cubic-bezier).transition-delay: Waiting time before motion begins.
Best Practice: The Shorthand Syntax
/* transition: property duration timing-function delay */.interactive-card { background-color: #ffffff; transform: translateY(0); box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05); /* Always declare transition on the BASE rule, not inside :hover! */ transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.3s ease, background-color 0.2s ease;}.interactive-card:hover { transform: translateY(-8px); box-shadow: 0 20px 25px rgba(0, 0, 0, 0.15);}[!TIP] Performance Rule: Always prefer animating
transformandopacity. These two properties are processed directly by the device's GPU (Graphics Processing Unit), avoiding expensive browser layout recalculations and guaranteeing silky 60fps / 120fps animations.
4. Keyframe Animations: Autonomous Motion
While transitions require a user trigger (like :hover or :focus), Keyframe Animations run automatically and can loop indefinitely.
1. Defining the Animation with @keyframes
@keyframes pulseGlow { 0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(37, 99, 235, 0.7); } 70% { transform: scale(1.05); box-shadow: 0 0 0 16px rgba(37, 99, 235, 0); } 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(37, 99, 235, 0); }}2. Applying the Animation
.pulse-badge { display: inline-block; padding: 6px 14px; background-color: #2563eb; color: white; border-radius: 9999px; /* Apply keyframes: name duration timing-function iteration-count */ animation: pulseGlow 2s infinite ease-in-out;}Common Animation Properties
animation-iteration-count:1,3, orinfinite.animation-direction:normal,reverse, oralternate(bounces back and forth smoothly).animation-fill-mode:forwards(retains final keyframe style after completing).
5. Live Interactive Animation & Variable Sandbox
Experiment with live CSS variables, transforms, and keyframe animations in the interactive sandbox below:
CSS Motion & Animation Studio
Live SandboxInspect the pulse animation, hover transforms, and adjust CSS variables live.
6. Flashcards & Core Concepts
Why should you declare CSS transitions on the base selector rather than on the :hover state?
Which two CSS properties are best suited for high-performance 60fps animations?
How do you define a root-level CSS variable and consume it?