The from and to Keywords
You'll learn: How to use shorthand keywords to define simple animation start and end states without percentage values.
What Are from and to?
When writing @keyframes for CSS animations, you often need to specify a starting point and an ending point. Instead of writing 0% and 100%, CSS provides two convenient keywords: from and to. They're simply aliases that make your code more readable when you only care about the beginning and end states.
Think of it like giving directions: instead of saying "At kilometer 0, turn left; at kilometer 100, arrive at destination," you can say "From here, turn left; to there, you'll arrive." It's the same information, just more natural.
How They Work
The from keyword is identical to 0% (the animation's start), and to is identical to 100% (the animation's end). The browser fills in all the in-between values automatically based on your animation-timing-function.
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
This is functionally identical to:
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
When to Use Them
Use from and to when your animation only needs two states—a clear beginning and end. If you need intermediate steps (like 50% or 75%), stick with percentage notation. You can't mix them effectively in the same @keyframes rule.
Key Takeaway: The from and to keywords are readable shortcuts for 0% and 100% in simple two-state animations, making your keyframes cleaner when you don't need intermediate steps.