CSS Animation - Used to create a wide variety of animations
CSS animation allows you to create animations using CSS code, rather than JavaScript. This can be used to create a wide variety of animations, such as hover effects, scrolling animations, and more.
Here is an example of a simple CSS animation that makes an element fade in and out:
/* The animation code */
@keyframes fadeInOut {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
/* The element to be animated */
.fade-element {
animation: fadeInOut 2s ease-in-out infinite;
}
In this example, the @keyframes
rule defines the animation called “fadeInOut”. The animation has three keyframes: one at 0% where the element is invisible, one at 50% where the element is fully visible, and one at 100% where the element is invisible again. The animation
property is used to apply the animation to the element with class “fade-element”, and it will run for 2 seconds with ease-in-out effect and repeat infinitely.
CSS animations are supported in most modern browsers, but there may be slight differences in how they are implemented. You can use browser-specific prefixes, such as -webkit-
for Safari and Chrome, to ensure that your animations work in all browsers.
Here are a few more things you can do with CSS animations:
- Animating multiple properties: You can animate multiple properties of an element at the same time. For example, you can animate both the position and the color of an element.
/* The animation code */
@keyframes moveAndChange {
0% {
transform: translate(0, 0);
color: black;
}
100% {
transform: translate(100px, 100px);
color: red;
}
}
- Using animation-delay to delay the start of an animation: You can use the
animation-delay
property to delay the start of an animation.
/* The element to be animated */
.delayed-element {
animation: moveAndChange 2s;
animation-delay: 2s;
}
- Using animation-iteration-count to control the number of times an animation plays: You can use the ‘
animation-iteration-count
‘ property to control the number of times an animation plays. For example, you can set it to 2 to make the animation play twice.
/* The element to be animated */
.repeating-element {
animation: moveAndChange 2s infinite;
animation-iteration-count: 2;
}
- Using animation-direction to control the direction of an animation: You can use the ‘
animation-direction
‘ property to control the direction of an animation. For example, you can set it to “reverse” to make the animation play in reverse.
/* The element to be animated */
.reverse-element {
animation: moveAndChange 2s;
animation-direction: reverse;
}
- Using @keyframes to control the animation: You can use the
@keyframes
rule to control the animation. For example, you can use it to create more complex animations with multiple stages, or to create animations that repeat in a loop.
/* The animation code */
@keyframes moveAndChange {
0% {
transform: translate(0, 0);
color: black;
}
50% {
transform: translate(100px, 100px);
color: blue;
}
100% {
transform: translate(200px, 200px);
color: red;
}
}
These are just a few examples of what you can do with CSS animations. You can experiment with different animation properties and keyframe values to create your own unique animations.