CSS Animations & Transitions - Complete Tutorial
CSS Animations & Transitions - Complete Tutorial
🔥 What is Animation?
Animation = Making elements move/change over time
css
.box {
width: 100px;
background: red;
/* This is START state */
}
css
.box:hover {
width: 200px;
background: blue;
/* This is END state */
}
🔄 Interpolation
Interpolation = Browser automatically calculates intermediate values
Example:
css
Simple Example:
css
.button {
background: blue;
transition: background 0.3s ease;
}
.button:hover {
background: red;
}
Transition Properties:
Property What it does Example
🎬 CSS Animations
Why Use Animations?
Multiple steps (not just A to B)
Can repeat
More control
Complex effects
Basic Structure:
css
/* 1. Define animation */
@keyframes myAnimation {
0% { /* start */ }
50% { /* middle */ }
100% { /* end */ }
}
/* 2. Apply to element */
.element {
animation: myAnimation 2s ease infinite;
}
Keyframe Syntax:
css
@keyframes animationName {
from { /* same as 0% */ }
to { /* same as 100% */ }
}
/* OR */
@keyframes animationName {
0% { opacity: 0; }
25% { opacity: 0.5; }
50% { opacity: 1; }
75% { opacity: 0.5; }
100% { opacity: 0; }
}
css
@keyframes bounce {
0% { transform: translateY(0); }
25% { transform: translateY(-20px); }
50% { transform: translateY(0); }
75% { transform: translateY(-10px); }
100% { transform: translateY(0); }
}
⚙️ Animation Properties
Complete Animation Syntax:
css
Individual Properties:
Property Values Example
Animation Direction:
normal - Forward only
Fill Mode:
forwards - Stay at end state
🔄 Multiple Animations
Separate Animations:
css
.element {
animation:
slideIn 1s ease,
fadeIn 2s linear,
rotate 3s infinite;
}
Multiple Keyframes:
css
@keyframes slideAndFade {
0% {
opacity: 0;
transform: translateX(-100px);
}
50% {
opacity: 0.5;
transform: translateX(0);
}
100% {
opacity: 1;
transform: translateX(100px);
}
}
css
.element {
transition-property: width;
transition-duration: 2s;
transition-timing-function: ease;
transition-delay: 0.5s;
}
Shorthand (Combined):
css
.element {
transition: width 2s ease 0.5s;
}
.box {
transition: all 0.5s ease;
}
Answer: Animates ALL properties for 0.5 seconds with ease timing
css
@keyframes wrong {
start { opacity: 0; }
finish { opacity: 1; }
}
Answer:
css
@keyframes correct {
0% { opacity: 0; }
100% { opacity: 1; }
}
animation-iteration-count: infinite
a) @animation
b) @keyframes ✓
c) @transition
d) @animate
a) ease
b) linear
c) smooth ✓
d) ease-in-out
a) animation-duration: infinite
b) animation-iteration-count: infinite ✓
c) animation-repeat: infinite
d) animation-loop: infinite
css
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.fade-element {
animation: fadeIn 1s ease;
}
Slide In from Left:
css
@keyframes slideInLeft {
0% { transform: translateX(-100%); }
100% { transform: translateX(0); }
}
Pulsing Button:
css
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
.pulse-button {
animation: pulse 1s ease infinite;
}
Hover Transition:
css
.hover-box {
background: blue;
transform: scale(1);
transition: all 0.3s ease;
}
.hover-box:hover {
background: red;
transform: scale(1.2);
}
⚡ Last-Minute Tips
Remember:
1. Transitions need a trigger (hover, click)
Performance Tips:
Animate transform and opacity (GPU accelerated)
Browser Support:
Use vendor prefixes for older browsers:
css
🎯 Final Checklist
Before exam, make sure you know:
Good Luck! 🚀