If you would like to support CSSPortal, please consider making a small donation.
☕ Buy a Coffee
CSS animations are a powerful way to add interactivity and visual interest to your websites. With CSS animations, you can create effects such as fading elements in and out, moving elements around the page, changing the appearance of elements over time, and even simulate complex physics-based effects.
To create a CSS animation, you need to do two things:
animation property.Animation keyframes are defined using the @keyframes at-rule. The @keyframes at-rule takes the name of your animation as its argument. Inside the @keyframes at-rule, you define one or more keyframes.
Each keyframe is defined by a percentage value and a set of CSS properties. The percentage value specifies when the keyframe should occur in the animation. The CSS properties specify the values that the element should have at that point in the animation.
For example, the following code defines a simple animation that bounces an element up and down:
@keyframes bounce {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-10px);
}
100% {
transform: translateY(0);
}
}
This animation has three keyframes:
transform: translateY() property is set to 0, which keeps the element in its original position.transform: translateY() property is set to -10px, which moves the element 10 pixels down.transform: translateY() property is set back to 0, which moves the element back to its original position.Once you have defined your animation keyframes, you can apply the animation to an HTML element using the animation property. The animation property takes the name of your animation as its value.
For example, the following code applies the bounce animation to the .box element:
.box:hover {
animation: bounce 1s linear;
}
This code tells the browser to animate the .box element using the bounce animation over a period of 1 second. The linear timing function specifies that the animation should play at a constant speed. You can see the result of this animation below.
In addition to the animation property, there are a number of other CSS properties that you can use to control your animations. These properties include:
animation-duration: Specifies the duration of the animation in seconds.animation-timing-function: Specifies the speed curve of the animation.animation-delay: Specifies a delay before the animation starts.animation-iteration-count: Specifies how many times the animation should play.animation-direction: Specifies whether the animation should play forwards, backwards, or alternate directions.animation-fill-mode: Specifies what happens to the element before and after the animation.Once you have a basic understanding of CSS animations, you can start to create more complex and creative animations. For example, you can use CSS animations to create:
Here are some additional tips for creating CSS animations:
animation property to apply the animation to HTML elements.This article is just the beginning of a series of articles we will be publishing about CSS animations, hopefully this first one will give you a general understanding of how animations work in CSS.