CSS animations allow you to animate HTML elements without using JavaScript or Flash. It enhances user experience by creating smooth transitions between different states of an element. With CSS animations, you can change various CSS properties over a specified duration, from basic color changes to more complex transforms.
CSS animations are created by defining keyframes and specifying which CSS properties will be animated over time.
@keyframes
Rule: Defines the animation. It holds what styles the element will have at certain points in the animation cycle.@keyframes
Rule@keyframes animation-name {
0% { /* Starting styles */ }
100% { /* Ending styles */ }
}
0%
, 50%
, 100%
)..element {
animation-name: animation-name;
animation-duration: 2s; /* Length of time to complete one cycle */
animation-timing-function: ease; /* Speed curve of the animation */
animation-delay: 1s; /* Time before the animation starts */
animation-iteration-count: infinite; /* Number of times the animation should repeat */
animation-direction: alternate; /* Whether the animation should play in reverse every other cycle */
}
In this example, we’ll animate the background color of a box from blue to red.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
width: 200px;
height: 200px;
background-color: blue;
animation: changeColor 3s infinite;
}
@keyframes changeColor {
0% {
background-color: blue;
}
100% {
background-color: red;
}
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
.box
, and the @keyframes
rule changeColor
defines that the background color will change from blue to red over 3 seconds.animation-duration
animation-duration: 5s;
animation-timing-function
linear
: Constant speed throughout.ease
: Starts slow, speeds up, then slows down.ease-in
: Starts slow, speeds up.ease-out
: Starts fast, slows down.ease-in-out
: Slow start, fast middle, slow end.animation-timing-function: ease-in;
animation-delay
animation-delay: 2s;
animation-iteration-count
3
).infinite
: Loop the animation forever.animation-iteration-count: infinite;
animation-direction
normal
: The animation plays as specified.reverse
: The animation plays in reverse.alternate
: Animation alternates between forward and reverse on every cycle.animation-direction: alternate;
animation-fill-mode
none
: Default value. The element will not retain any animation styles after the animation ends.forwards
: The element will retain the style values from the last keyframe (after animation completes).backwards
: The element gets the styles defined at the start before the animation begins.both
: Combines forwards
and backwards
.animation-fill-mode: forwards;
animation-play-state
animation-play-state: paused;
This example demonstrates an animation that moves a box across the screen while rotating it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
position: relative;
animation: moveAndRotate 4s infinite alternate;
}
@keyframes moveAndRotate {
0% {
transform: translateX(0) rotate(0deg);
}
100% {
transform: translateX(300px) rotate(360deg);
}
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
.box
will move 300px to the right while rotating 360 degrees.translateX
moves the box horizontally, and rotate
rotates it.CSS allows multiple animations on the same element using a comma-separated list.
.element {
animation: slide 3s linear infinite, fade 5s ease-in-out infinite;
}
@keyframes slide {
0% { transform: translateX(0); }
100% { transform: translateX(300px); }
}
@keyframes fade {
0% { opacity: 1; }
100% { opacity: 0; }
}
slide
and fade
) are applied simultaneously.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.spinner {
width: 50px;
height: 50px;
border: 5px solid lightgray;
border-top: 5px solid blue;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="spinner"></div>
</body>
</html>
spin
keyframe rotates the .spinner
element from 0 to 360 degrees infinitely.CSS animations are a powerful tool for creating visually engaging websites. They allow for dynamic transitions and interactive designs without relying on JavaScript. Understanding how to combine keyframes, animation properties, and transforms gives you great flexibility in crafting animations that enhance user experiences.
next -> Css Variables