CSS
HTML
JavaScript
jQuery
PHP
SQL
WordPress
CSS Tutorials

Overview

CSS animations allow for elements to gradually change from one style to another or create movement on the page using keyframes to store the animation data.

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

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.

CSS
HTML
JavaScript
jQuery
PHP
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

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.