CSS Animations
Bring your websites to life with smooth, modern, and engaging animations.
What are CSS Animations?
CSS Animations allow elements to gradually change from one style to another without using JavaScript.
- Create smooth UI transitions
- Improve user experience
- Highlight important elements
- Reduce dependency on JavaScript
Real-Life Example: Like signboards that light up gradually instead of blinking suddenly.
CSS Transitions
Transitions animate changes between two states (for example: hover effects).
transition-propertytransition-durationtransition-timing-functiontransition-delay
Transition Example
Button Hover Animation
button {
background: #2563eb;
color: #fff;
padding: 10px 20px;
transition: background 0.3s ease, transform 0.3s ease;
}
button:hover {
background: #1e40af;
transform: scale(1.1);
}
CSS Keyframes
@keyframes define how an animation progresses over time.
- 0% → starting state
- 100% → ending state
- Can include multiple steps
Keyframes Animation Example
Fade In Animation
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card {
animation: fadeIn 1s ease-in-out;
}
Animation Properties
animation-nameanimation-durationanimation-timing-functionanimation-delayanimation-iteration-countanimation-direction
Infinite Animation Example
Loading Spinner
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.loader {
width: 40px;
height: 40px;
border: 4px solid #ddd;
border-top: 4px solid #2563eb;
border-radius: 50%;
animation: rotate 1s linear infinite;
}
Best Practices for CSS Animations
- Keep animations subtle
- Avoid excessive motion
- Prefer
transformandopacity - Respect user preferences (
prefers-reduced-motion)
Pro Tip: Good animations guide users — bad animations distract them.