The vanilla CSS(without any external JavaScript and CSS libraries) allows animation of HTML elements using CSS properties. This is very powerful and quite simple to learn and use.
As a beginner to CSS animation, I have learned some cool things and made a photo gallery 🖼️. This article is all about sharing that knowledge with you with a step-by-step approach. Hope you find it useful.
If you are using CSS animation already, read this article for fun. Feel free to comment to let us know some of the tips and tricks you use.
Photo Gallery
A photo gallery is a collection of photos. How about creating a gallery of some rocking stars from the entertainment industry? To put some animations behind, let us hang their photos from the wall and put a wave effect to make it feel awesome. The animation will stop if you try hovering on the photos.
At the current state of it, the gallery looks like this. Just check if you know all the characters there 😎.
TL;DR
This article is mostly aimed at beginners like me. Hence expect it to be a bit lengthy and tutorial type with a step-by-step approach. If you want to jump to the code and the working demo sooner, here are the links:
The CSS animation property helps animating many of the CSS properties. We can animate height, width, color, font, background-color, etc where there are a few properties such as, background-image can not be animated.
CSS animation property has a bunch of sub-properties that define the animation characteristics.
@keyframesanimation-name{/* animation actions to come here */}.element{animation:animation-nameanimation-durationanimation-timing-functionanimation-delayanimation-directionanimation-iteration-countanimation-fill-modeanimation-play-state;}
Note, the above declaration is a short-hand declaration. We will be able to use these properties separately as well,
@keyframesmove{/* animation actions to come here */}.element{animation-name:move;animation-duration:0.5s;animation-timing-function:linear;animation-delay:2s;animation-direction:alternate;animation-iteration-count:infinite;animation-fill-mode:none;animation-play-state:paused}
animation-name: The name of the @keyframes defined for the transition. A @keyframes must be defined for each of the animation need.
> A keyframe in animation and filmmaking is a drawing that defines the starting and ending points of any smooth transition.
animation-duration: The time duration that takes for an animation to complete one complete cycle.
animation-timing-function: This property specifies the speed(acceleration) curve of the animation. There are existing functions such as, linear, ease, ease-in, ease-in-out, ease-out. We can also write a custom function using the cubic-bezier(n,n,n,n) function.
animation-delay: It specifies the time delay to start the animation.
animation-iteration-count: It specifies the number of times the animation should be played. We can use the value infinite to play it in a loop.
animation-direction: It specifies if the animation should be played in a particular direction such as, forward(normal), reverse, alternate , alternate-reverse.
animation-fill-mode: With this property, we can specify a style for the element when the animation is not playing.
animation-play-state: This property is to specify if an animation is running or paused.
A Simple Example - Animate with Colors
Let us get started with a simple example to apply the animation properties to a div element. In this example, we just want to change the background color and text color of the div element with an animation-duration of 5 seconds.
First, create a div element.
<divclass="simple">
I'm so animated, but I animate once!
</div>
Create a keyframe to define the animation action. In this case, we just want to change the background and text colors. The @keyframes rule specifies the start and end of the animation with two properties, "from" and "to" (which represents 0% (start) and 100% (complete)).
Note the animation-name is simple in this example. The next thing is to apply the animation on the div element with this name and other sub-properties.
Here goes the example running in the Codepen for you to explore, fork, and edit.
Keyframes with the percentage properties
Animation actions are defined with the @keyframes rule. We have seen the usage of the from and to attributes. Now we will learn how to specify the actions with percentages such as 0%, 25%, 50%, 75%, 100%, etc.
In this example, we will change the background color and text color of a div element on the percentage breaks. Here is the @keyframes rule,
So far, we have explored the animation-name and animation-duration properties. Let us now explore three more properties, animation-delay, animation-iteration-count and animation-direction. This time. we will use these properties on the background-color, color, left, and top properties of a div element.
As usual, let us first create the div element
<divclass="move">I'm moved!</div>
Define the @keyframes rule to change the colors and positions.
Last, use the animation properties. Notice the animation-* properties here. We have used the name as the same as we defined with the @keyframes rule. An animation duration of 5 seconds used here. The animation starts with a delay of 2 seconds and runs infinitely with an alternate direction.
Here goes the Codepen link for you to explore further. How about you try a negative value for the animation-delay? How about trying out another animation-direction?
See how I have modified the same example to run the animation 3 times instead of an infinite count.
Use the Shorthand notation
So far, we have used the animation properties separately. However, we can use a shorthand notation to use them in the single animation property.
The same example like the above can be defined like,
Please notice the animation property here with all the property values together. You can explore it further with this Codepen example.
Let's race with the Timing function
In this example, we will learn about animation-timing-function property. The animation-timing-function property specifies the acceleration curve of the animation. The property values are,
linear: With linear an animation runs with the same speed from start to end. This is the default value.
ease: With ease, an animation starts slowly, then fast, then ends slowly.
ease-in: This enables an animation with a slow start.
ease-out: This specifies an animation with a slow end.
ease-in-out: With this, an animation with a slow start and end.
cubic-bezier(n,n,n,n): Define your own value for a custom function.
Let us use all of these. To do that, we will first create a few div elements to apply each of the functions explained above.
Next is to define the @keyframes rules. Here we will have three rules. One to move the div horizontally, one vertically, and another to change the shape of them.
Here is the Codepen example to see how the bunny and ginny divs moving around and changing shape and color.
Finally, the Photo Gallery
Alright, let us do a quick recap of things we learned so far.
We have learned about the @keyframes rule.
We have defined animation properties like name, duration, delay, time-function, direction, and iteration-count.
We have learned about the shorthand way of declaring the animation property.
Great! in this final example, we will use all of them along with another useful property called, animation-play-state.
First thing first, let us use the HTML5's figure element to define four images with captions. These four images are the photo frames used in our gallery.
Next, we want to add animations to the photo frames such that, they wave(or swing) in the left and right directions. So, time to create our @keyframes rule.
At this point, we should see the photo frames are swinging left and right. Next, we want to stop the animation when someone hovers on a photo frame. We will use the animation-play-state property value as paused for this.
.wave:hover{animation-play-state:paused;}
That's all for all the functionality we needed. But wait, we also have to make it look elegant. Use the following CSS rules to make the photo frames look like hanging from a wall using a screw head.
You can see it working in this Codepen example. Feel free to explore and change.
Performance
Animating a CSS property can lead to performance concerns. We should be careful about using the animation properties. Here is a great article that explains the concerns in detail. Please have a look.
Browser Support
Here is the table shows the browser support for the animation properties.
If you had started this article as a beginner to CSS animation and you are here after reading it through, how do you feel? Isn't it fun? Give it a try by doing some hands-on with any fantasy project. You are going to love it.
All the source code used in this article are in the GitHub repository. Please give it a star if you liked the work. Feel free to follow it as I keep updating the source code with examples.
Learn CSS animation with fun. It is simple, easy to use, and fun to learn.
learn-css-animation
As a beginner to CSS animations, I have learned some cool things and made a photo gallery 🖼️. This project repo is all about sharing that knowledge with you with a step-by-step approach. Hope you find it useful.
✋ Feel free to follow this project as I keep updating the source code. If it was useful to you, show your support with a star ⭐
Please feel free to clone the repo and try running it locally. This project is also deployed with Netlify and you can see it running from here,
https://css-animation-fun.netlify.app/.
Build Status:
Many Thanks to all the Stargazers who has supported this project with stars(⭐)