Hey there! Ever wondered how websites make pictures move around, fade in and out, or just look fancy? That's all, thanks to something called animation!
Animation is like adding cool tricks to your website, making it more interesting and fun to look at. The good news? You don't need to be a super-coder to use it. Even beginners can get started with animation.
In this article, we'll break down the basics of animation and show you how it works. We'll use clear and simple language, so even if you're brand new to this, you'll be able to understand it.
Ready to learn how to make your website come alive? Let's dive into the world of animation with something called CSS animations!
To start, it's important to understand the difference between CSS Animation and CSS Transition.
Think of CSS transitions like mini-makeovers for your website elements. They're great for those smooth changes you see, like a button brightening up when you hover your mouse over it.
CSS animations are like full-on website action movies! You can create awesome, fancy movements with different parts happening one after another. Imagine a picture popping in, spinning around, and then slowly fading away—that's the kind of coolness CSS animations can do!
Let's see a quick example of a CSS Transition.
Now, when we talk about CSS animation, we use special things called "animation properties" and "keyframes" to make it work.
It will look like this:
As you can see, we're using different properties like "animation" and "keyframes" to make things animate. And because of these special CSS properties, we can make things move around and look lively.
If this doesn't totally make sense right now, no worries. In the next part, we'll start from the very beginning to help you understand it better.
Well, CSS animation allows us to add movement to HTML elements using CSS. But to animate an HTML element, there are several animation sub-properties that you need to understand.
These sub-properties are as follows:
In the example above, we've assigned the animation name "bounce" and defined its behavior using keyframes.
For example, in the previous instance, we took a bounce animation as an illustration and then proceeded to define the keyframes that produced the bouncing effect like this:
Here, the keyframes are defined using percentage values. At 0%, the animation is at its initial stage, and at 100%, it's at the final stage.
Of course, you can include additional percentage values to customize the animation further.
Keep in mind that if you don't specify the duration, the animation won't happen. This is because the default value for “animation-duration” is 0 (zero).
In this case, the animation will start after a 4-second delay.
For instance, if you set “animation-iteration-count: 0”, the animation won't play at all. Similarly, specifying “animation-iteration-count: 1” means the animation will play once, and so on.
For example,
Now, let’s take a simple example.
In this example, we've set various animation properties.
The animation is named “bounce”, with a duration of 1 second. Additionally, there's a delay of 4 seconds before the animation begins, the bouncing action persists endlessly due to the use of an iteration count of infinite, and so on.
Let's revisit the example above.
As you can see, we're writing several lines of code just to add a bounce animation. It might seem a bit unnecessary, don't you think?
That's precisely why a shorthand CSS animation property exists.
For instance, instead of writing all of this:
We can achieve the same effect with a shorter version:
Likewise, you can combine multiple animation values and adjust things like iteration count, delay time, duration, and more.
Here's an example:
In this code, the slide keyframe will run once based on the iteration property, while the bounce keyframe will run indefinitely due to the "infinite" setting.
Here’s the output.
Apart from the earlier properties we learned, here are a few more animation properties you can use to make complicated animations.
The options are as follows:
The available choices are:
3. animation-play-state: This property lets you clearly control the current status of the animation, allowing you to pause or play it.
The options include:
By using these animation tricks, you can make really fancy animations that look cool and keep users interested.
Here’s one more example.
As you can see, we have added two keyframes to a single “div” with the class name as “cube".
Here’s the output:
Up to this point, we've learned quite a bit about CSS animation properties.
However, it's important to note that you can only use CSS animation properties after a specific version in web browsers like Google Chrome, Microsoft Edge, Opera Mini, and others.
These versions are as follows:
And thanks to CSS animations, many websites are adding CSS animations to elevate their websites.
For instance, you can take a look at some of the best websites that have added animations to enhance the user experience, such as Nike-React, KPRVerse, Ventriloc, Heights, and more.
These websites undoubtedly use a variety of animation packages to make their websites highly interactive, like Framer Motion and others.
For swift integration of animations into your application, Locofy.ai offers pre-built effects such as slide-in, fade-in, bounce, and more. Additionally, Locofy.ai enables element tagging as Lottie, allowing for the seamless incorporation and animation of custom Lottie files.
If you want to learn more about how to convert your Figma design to React, React Native, HTML/CSS, Nextjs, Vue, and more, check out our docs. Or explore our solution pages for an in-depth look at how Locofy helps you build apps, websites, portfolio pages and responsive prototypes effortlessly using our low-code platform with seamless AI code generation.
Join the discord server for more updates, and try our sample projects on Figma to experience Lightning.