iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

CSS Animations

Where transition animates between two states, @keyframes defines an animation with multiple frames and timings — looping, multi-step, choreographed.

Define the keyframes

CSS
@keyframes pulse {
  0%   { transform: scale(1);    opacity: 1; }
  50%  { transform: scale(1.05); opacity: 0.8; }
  100% { transform: scale(1);    opacity: 1; }
}

Apply it

CSS
.live-indicator {
  animation: pulse 1.2s ease-in-out infinite;
}

Animation properties

PropertyPurpose
animation-nameThe @keyframes rule to use.
animation-durationLength of one cycle.
animation-timing-functionEasing — linear, ease, steps(N), custom cubic.
animation-delayWait before starting.
animation-iteration-countNumber of loops or infinite.
animation-directionnormal, reverse, alternate.
animation-fill-modeKeep styles before/after animation runs.
animation-play-staterunning or paused.
Accessibility: Wrap non-essential animations in @media(prefers-reduced-motion: no-preference) so motion-sensitive users get a still version.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>

<h1>CSS Animations</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

</body>
</html>
Try it Yourself »

Exercise

Make the pulse animation loop forever.

.dot { animation: pulse 1.2s ease-in-out ; }

Test yourself

Q1. Which at-rule defines an animation sequence?
Q2. Which property loops an animation forever?
Q3. Which value of `animation-direction` bounces back and forth?

Discussion

Loading…