Why Use Keyframe Animations?
Ever wanted your website to have smooth animations without using JavaScript? Say hello to CSS Keyframes! With just a few lines of CSS, you can make elements move, fade, bounce, spin, and more!
In this guide, you’ll learn:
- What CSS Keyframes are
- How to create basic animations
- Key animation properties
- Real-world examples to make your site pop!
Let’s get animated!
What are CSS Keyframe Animations?
A keyframe animation is a way to define movement or style changes over time. Instead of instantly changing a property, keyframes let you animate it smoothly!
Basic Syntax:
@keyframes animation-name {
from {
/* Starting styles */
}
to {
/* Ending styles */
}
}
.element {
animation: animation-name duration timing-function delay iteration-count direction;
}
animation-name
→ Name of the keyframe animation.duration
→ How long the animation lasts (e.g.,2s
).timing-function
→ Speed curve (ease
,linear
, etc.).delay
→ Wait time before the animation starts.iteration-count
→ How many times the animation runs (infinite
for looping!).direction
→ Controls the animation flow (normal
,reverse
,alternate
).
Creating a Simple Animation
Let’s make a box move from left to right!
@keyframes slide-right {
from {
transform: translateX(0);
}
to {
transform: translateX(200px);
}
}
.box {
width: 100px;
height: 100px;
background-color: blue;
animation: slide-right 2s ease-in-out infinite alternate;
}
What happens?
- The box slides 200px to the right in
2s
. ease-in-out
makes it smooth.infinite
loops the animation forever.alternate
makes it move back and forth!
Using Multiple Keyframes for Complex Animations
Let’s create a bouncing ball effect!
@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-50px);
}
100% {
transform: translateY(0);
}
}
.ball {
width: 50px;
height: 50px;
background-color: red;
border-radius: 50%;
animation: bounce 0.5s ease-in-out infinite;
}
What happens?
- The ball moves up and down like it’s bouncing.
0% → 50% → 100%
defines different stages of movement.ease-in-out
makes it feel natural.
Controlling Animation with Key Properties
Property | Description |
---|---|
animation-duration |
Controls how long the animation lasts (e.g., 2s ). |
animation-timing-function |
Defines the speed curve (ease , linear , etc.). |
animation-delay |
Sets a delay before the animation starts. |
animation-iteration-count |
Number of times the animation runs (infinite for loops!). |
animation-direction |
normal , reverse , or alternate motion. |
animation-fill-mode |
Controls how styles apply before/after animation. |
Combining Multiple Animations
Want to spin and change color at the same time? Let’s do it!
@keyframes spin {
0% { transform: rotate(0deg); background-color: red; }
100% { transform: rotate(360deg); background-color: blue; }
}
.element {
width: 100px;
height: 100px;
animation: spin 2s linear infinite;
}
Now the box rotates and changes color continuously!
Conclusion: Your Website is Now ALIVE!
Key Takeaways:
- Use
@keyframes
to define animations over time. - Apply animations with
animation
properties. - Control speed, direction, delays, and iterations.
- Combine animations for awesome effects!
Now go make your website dance, bounce, and spin!
0 Comments