Introduction to CSS Animations and Transitions
Using transition, transform, and animation
Keyframes and @keyframes Syntax
Hover, Click, and Scroll-Based Effects
Chaining Animations and Easing Functions
Performance Considerations
Real-World Animation Examples
Conclusion
CSS animations allow elements to gradually change from one style to another. Instead of making abrupt style changes (like color or position), you can animate these properties over time using CSS.
There are two main types:
Both are defined in your stylesheets, no JavaScript required.
transition
The transition property enables smooth interpolation between CSS property changes.
button {
transition: background-color 0.3s ease-in-out;
}
transform
The transform property allows movement, rotation, scaling, and skewing of elements.
button:hover {
transform: scale(1.1);
}
animation
The animation property gives you complete control using keyframes.
div {
animation: fadeIn 2s ease-in;
}
To create more complex animations, define keyframes with @keyframes:
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
You can then assign this animation to any element:
.modal {
animation: fadeIn 0.5s ease-out forwards;
}
Hover Effects
Simple hover transformations:
.card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}
Click Effects
Use the :active pseudo-class for click animations:
.button:active {
transform: scale(0.95);
}
Scroll Effects (with IntersectionObserver + CSS)
While CSS doesn’t support scroll triggers natively, you can combine it with JavaScript to add or remove classes on scroll, which then trigger CSS animations.
Chaining Animations
Use animation-delay or transition timings to sequence multiple effects.
h1 {
animation: slideIn 1s ease-out;
}
p {
animation: fadeIn 1s ease-out;
animation-delay: 0.5s;
}
Easing Functions
Define how the speed of the animation progresses:
box {
transition: transform 0.5s ease-in-out;
}
While animations look great, overusing or misusing them can hurt performance. Keep in mind:
.card {
will-change: transform, opacity;
}
Button Hover
.button {
padding: 12px 24px;
background: #007BFF;
color: white;
border: none;
transition: background 0.3s ease, transform 0.2s;
}
.button:hover {
background: #0056b3;
transform: translateY(-3px);
}
Modal Fade-In
@keyframes modalFade {
from {
opacity: 0;
transform: scale(0.95);
}
to {
opacity: 1;
transform: scale(1);
}
}
.modal {
animation: modalFade 0.4s ease-out;
}
CSS animations are a powerful and lightweight way to enhance your UI. Whether you're building interactive hover states or crafting elegant page transitions, CSS gives you the flexibility and control to do it all, without writing a single line of JavaScript.
By mastering properties like transform, transition, and @keyframes, and keeping performance in mind, you can craft modern interfaces that are responsive, smooth, and user-friendly.
Senior Frontend Engineer
Mark is a passionate software developer and author with expertise in JavaScript and Python. He enjoys simplifying complex programming concepts and sharing practical coding tips.