๐ฌ CSS Transitions & Animations: Making Things Move Like Magic!
Imagine you have a toy car. You push it, and it zooms across the floor. But what if it could start slow, speed up, and then gently stop? Thatโs what CSS transitions and animations do for things on websites!
Think of it like being a movie director ๐ฅ โ you tell your website elements exactly HOW to move, WHEN to move, and for HOW LONG!
๐ Transition Properties: Teaching Your Car to Drive Smoothly
When you push a door, it doesnโt instantly teleport from closed to open. It swings smoothly! Transitions make your website elements move smoothly too.
The 4 Magic Controls
Think of a remote control with 4 buttons:
| Button | What It Does | Example |
|---|---|---|
transition-property |
WHAT moves | color, size, position |
transition-duration |
HOW LONG it takes | 0.3s, 1s, 2s |
transition-timing-function |
HOW it moves | fast-slow, slow-fast |
transition-delay |
WHEN it starts | wait 0.5s first |
Simple Example
.button {
background: blue;
transition-property: background;
transition-duration: 0.3s;
}
.button:hover {
background: red;
}
When you hover, the button smoothly changes from blue to red in 0.3 seconds!
The Shortcut Way
Instead of writing 4 lines, use ONE:
.button {
transition: background 0.3s ease 0s;
}
/* property | duration | timing | delay */
Multiple Properties
Want EVERYTHING to animate? Use all:
.card {
transition: all 0.5s ease;
}
Or list specific ones:
.card {
transition:
background 0.3s ease,
transform 0.5s ease;
}
๐ข Easing Functions: The Speed of Your Roller Coaster
Imagine a roller coaster ๐ข:
easeโ Starts slow, speeds up, slows down at end (most natural!)linearโ Same speed the whole time (like a boring escalator)ease-inโ Starts slow, ends fast (rolling downhill!)ease-outโ Starts fast, ends slow (car braking)ease-in-outโ Slow start AND slow end (very smooth)
graph TD A["๐ Start"] --> B{Which Easing?} B --> C["ease: Natural feel"] B --> D["linear: Constant speed"] B --> E["ease-in: Slow start"] B --> F["ease-out: Slow end"] B --> G["ease-in-out: Both slow"]
Example
/* Button flies in fast, lands softly */
.fly-in {
transition: transform 0.5s ease-out;
}
๐จ Cubic-Bezier: Create Your Own Roller Coaster!
The built-in easings not enough? Make your own!
cubic-bezier(x1, y1, x2, y2) has 4 numbers that control the โcurveโ of your animation.
Think of it like bending a flexible stick ๐ช:
- Numbers between 0 and 1 = normal bending
- Numbers above 1 = BOUNCY!
- Numbers below 0 = Goes backward first!
Popular Custom Easings
/* Bouncy! Overshoots then settles */
.bouncy {
transition: all 0.6s
cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
/* Super snappy start */
.snappy {
transition: all 0.3s
cubic-bezier(0.4, 0, 0.2, 1);
}
Understanding the Numbers
| Position | What It Controls |
|---|---|
| x1, y1 | Start curve shape |
| x2, y2 | End curve shape |
Pro Tip: Use cubic-bezier.com to visually create your curves!
๐ฌ @keyframes: Directing Your Own Movie Scenes!
Transitions go from A to B. But what if you want A โ B โ C โ D?
@keyframes lets you create a whole MOVIE with multiple scenes!
Simple Example: Bouncing Ball
@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-100px);
}
100% {
transform: translateY(0);
}
}
This says:
- Scene 1 (0%): Ball at normal position
- Scene 2 (50%): Ball jumps UP 100px
- Scene 3 (100%): Ball back down
Using Keywords
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
from = 0%, to = 100%
Complex Example: Rainbow Glow
@keyframes rainbow {
0% { background: red; }
25% { background: yellow; }
50% { background: green; }
75% { background: blue; }
100% { background: red; }
}
๐ฎ Animation Properties: The Control Panel
Now you have your movie (@keyframes). Time to PLAY it!
The 8 Animation Controls
| Property | What It Does | Example Values |
|---|---|---|
animation-name |
Which movie to play | bounce, fade-in |
animation-duration |
How long ONE play takes | 1s, 2s |
animation-timing-function |
Speed curve | ease, linear |
animation-delay |
Wait before starting | 0.5s |
animation-iteration-count |
How many times | 3, infinite |
animation-direction |
Forward, backward, both? | normal, reverse, alternate |
animation-fill-mode |
What happens when done? | forwards, backwards |
animation-play-state |
Play or pause? | running, paused |
Example: Forever Bouncing Ball
.ball {
animation-name: bounce;
animation-duration: 1s;
animation-timing-function: ease;
animation-iteration-count: infinite;
}
The Shortcut (Animation Shorthand)
.ball {
animation: bounce 1s ease infinite;
}
/* name | duration | timing | count */
Cool Directions
/* Goes forward then backward */
.swing {
animation: swing 2s ease infinite alternate;
}
/* Plays backward */
.rewind {
animation: grow 1s ease reverse;
}
Fill Mode Magic
/* Stays at final position when done */
.fade-in {
animation: appear 1s ease forwards;
}
/* Starts at first keyframe position */
.delayed {
animation: slide 1s ease 2s backwards;
}
๐ช Multiple Animations: The Grand Circus!
Why have ONE performer when you can have a WHOLE CIRCUS?
Running Multiple Animations
Separate them with commas:
.circus-star {
animation:
spin 2s linear infinite,
glow 1s ease-in-out infinite alternate,
bounce 0.5s ease infinite;
}
This element:
- ๐ Spins continuously
- โจ Glows in and out
- ๐ Bounces up and down
All at the SAME TIME!
Example: Dancing Button
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.1); }
}
@keyframes shimmer {
0% { box-shadow: 0 0 5px gold; }
100% { box-shadow: 0 0 20px gold; }
}
.magic-button {
animation:
pulse 1s ease infinite,
shimmer 1.5s ease infinite alternate;
}
Timing Each Animation Differently
.complex {
animation:
slide 1s ease forwards,
fade 0.5s ease 0.5s forwards,
grow 2s ease 1s forwards;
}
Here:
- slide starts immediately
- fade starts at 0.5s
- grow starts at 1s
graph TD A["0s: slide starts"] --> B["0.5s: fade joins"] B --> C["1s: grow joins"] C --> D["All dancing together!"]
๐ Quick Summary
| Want Toโฆ | Use This |
|---|---|
| Smooth state change | transition |
| Custom speed curve | cubic-bezier() |
| Multi-step animation | @keyframes |
| Control the movie | animation properties |
| Multiple effects | Comma-separate animations |
๐ Your Animation Superpowers
Now you can:
- โ Make buttons glow when touched
- โ Create bouncing balls
- โ Build rainbow color effects
- โ Design loading spinners
- โ Animate anything you imagine!
Remember: Start simple, experiment, and have FUN! Every great animator started by making a box move across the screen. ๐ฌโจ
