CSS Animations let you smoothly change element styles over time using keyframes. You can animate movement, color, size, opacity, and more — all without JavaScript.
With CSS animations you define @keyframes (what changes over time) and then attach them to an element using animation properties. The browser smoothly interpolates between the states you define.
@keyframes.animation-name and friends.from/to.@keyframes block.2s, 500ms).1, 3, infinite).normal, reverse, alternate).ease, linear, ease-in).forwards, backwards, both).You typically write CSS animations in two parts: a @keyframes declaration and an element rule that uses animation-* properties.
/* 1️⃣ Define the animation steps using keyframes */
@keyframes animationName {
0% { opacity: 0; transform: translateY(20px); }
50% { opacity: 0.5; transform: translateY(10px); }
100% { opacity: 1; transform: translateY(0); }
}
/* 2️⃣ Attach the animation to an element */
.element {
animation-name: animationName;
animation-duration: 2s;
animation-delay: 0.5s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-timing-function: ease-in-out;
animation-fill-mode: both;
}
animation Property
/* duration | timing-function | delay | iteration-count | direction | fill-mode | name */
.element {
animation: 2s ease-in-out 0.5s infinite alternate both animationName;
}
The box below moves left & right, grows slightly, and changes color using CSS animations.
0%, the box starts on the left, normal size, coral background.50%, it moves right, scales up to 1.2, and turns gold.100%, it returns to the left, normal size, deepskyblue background.alternate direction makes it move back and forth seamlessly.
<!-- Simple animated box using CSS animations -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Animation Example</title>
<style>
@keyframes moveScale {
0% { transform: translateX(0) scale(1); background-color: coral; }
50% { transform: translateX(100px) scale(1.2); background-color: gold; }
100% { transform: translateX(0) scale(1); background-color: deepskyblue; }
}
.box {
width: 100px;
height: 100px;
background-color: coral;
border-radius: 12px;
animation: moveScale 3s ease-in-out 0s infinite alternate both;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
animation-direction: alternate for smooth back-and-forth motion.transform, opacity, and background-color for rich effects.animation-fill-mode: forwards to keep the final state after the animation ends.transform and opacity as they are GPU-friendly.infinite animations on many elements.transform: scale() and animation-iteration-count: infinite.transform: rotate() inside @keyframes.animation-delay values.animation-direction values like reverse and alternate-reverse.ease-in-out with linear and observe the difference in motion.