When you specify CSS styles inside the @keyframes rule, the animation will gradually change from the current style to the updated style at specific times.
The animation will always be bound to a specified element or elements.
The following has a “from” frame and “to” frame, which are essential a start and end.
/* the element to animate */
div {
width: 20px;
height: 120px;
background-color: red;
opacity: 1.0;
position: relative;
top: 0;
left: 0;
animation-name: move-div;
animation-delay: 2s;
animation-duration: 3s;
animation-iteration-count: 4;
}
/* the animation */
@keyframes move-div {
from {
left: 0;
opacity: 1.0;
}
to {
left: 300px;
opacity: 0;
}
}
Reload the page, and the following element will wait 2 seconds and then animate over 3 seconds, 4 times.
We can also use percentages to set multiple frames to change the animation multiple times.
In the following, we have three frames. The first is the starting point which animates to a midpoint and then to an endpoint. And because we have the animation-iteration-count set to infinite, it starts back over again and never stops.
/* the element to animate */
div {
width: 20px;
height: 120px;
background-color: red;
opacity: 1.0;
position: relative;
top: 0;
left: 0;
animation-name: ping-pong;
animation-duration: 3s;
animation-iteration-count: infinite;
}
/* the animation */
@keyframes ping-pong {
0% {
left: 0;
background-color: red;
}
50% {
left: 95%;
background-color: blue;
}
100% {
left: 0;
background-color: red;
}
}
The following element will bounce from left to right and back to left again infinite times while changing colors.
Properties
- CSS – animation Property
- CSS – animation-delay Property
- CSS – animation-direction Property
- CSS – animation-duration Property
- CSS – animation-fill-mode Property
- CSS – animation-iteration-count Property
- CSS – animation-name Property
- CSS – animation-play-state Property
- CSS – animation-timing-function Property
- CSS – @keyframes Rule
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.
Why 1SMARTchicken?
See More →
Thanks for your support!
CSS Books
All are affiliate links. Thank you for your support!
CSS NOTES:
- The “inherit”, “initial” and “unset” keywords can be used with any CSS property to set its value
- In CSS there are many ways to express a color value in a property
Feedback
If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.