Learn How the CSS @keyframes and animation Properties Work:
To animate an element, you need to know about the animation properties and the @keyframes rule.
The animation properties control how the animation should behave and the @keyframes rule controls what happens during that animation.
There are eight animation properties in total. This challenge will keep it simple and cover the two most important ones first:
animation-name sets the name of the animation, which is later used by @keyframes to tell CSS which rules go with which animations.
animation-duration sets the length of time for the animation.
@keyframes is how to specify exactly what happens within the animation over the duration.
This is done by giving CSS properties for specific "frames" during the animation, with percentages ranging from 0% to 100%.
If you compare this to a movie, the CSS properties for 0% is how the element displays in the opening scene.
The CSS properties for 100% is how the element appears at the end, right before the credits roll.
Then CSS applies the magic to transition the element over the given duration to act out the scene. Here's an example to illustrate the usage of @keyframes and the animation properties:
#anim {
animation-name: colorful;
animation-duration: 3s;
}
@keyframes colorful {
0% {
background-color: blue;
}
100% {
background-color: yellow;
}
}
Use CSS Animation to Change the Hover State of a Button:
<style>
img {
width: 30px;
}
img:hover {
animation-name: width;
animation-duration: 500ms;
}
@keyframes width {
100% {
width: 40px;
}
}
</style>
<img src="https://cdn.freecodecamp.org/curriculum/applied-visual-design/google-logo.png" alt="Google's Logo" />
No comments:
Post a Comment