Aral Code

Effects & Animation Lesson 31 of 33

CSS Animations with @keyframes

Create animations that play on their own with @keyframes — loading spinners, pulses, fades and a jeepney driving across the screen — and respect reduced-motion settings.

Transitions animate between two states when something changes, like hovering. Animations can have many steps, can start on their own when the page loads, and can loop forever. Loading spinners, bouncing arrows and pulsing "LIVE" badges are all CSS animations.

Step 1: define the keyframes

@keyframes describes what happens during the animation. You give it a name, and list what the element looks like at different points, from 0% (start) to 100% (end):

@keyframes fade-in {
  from { opacity: 0; }   /* same as 0% */
  to   { opacity: 1; }   /* same as 100% */
}

Step 2: apply it to an element

.message {
  animation-name: fade-in;
  animation-duration: 1s;
}
Example A fade-in on page load
<style>
  @keyframes fade-in {
    from { opacity: 0; transform: translateY(20px); }
    to   { opacity: 1; transform: translateY(0); }
  }
  .message {
    animation: fade-in 0.8s ease-out;
    background: #e3f1ec;
    padding: 16px;
    border-radius: 12px;
    font: 18px Arial, sans-serif;
  }
</style>
<div class="message">🎉 Congratulations! You passed the quiz.</div>

Press Run or Reset in the editor to replay it.

Animation properties

PropertyWhat it doesExample
animation-nameWhich @keyframes to usespin
animation-durationHow long one cycle takes2s
animation-timing-functionSpeed curvelinear, ease-in-out
animation-delayWait before starting0.5s
animation-iteration-countHow many times3, infinite
animation-directionPlay backward or back-and-forthalternate
animation-fill-modeKeep the end state after finishingforwards

The animation shorthand combines them: animation: spin 1s linear infinite;

A loading spinner

Example Spinner
<style>
  @keyframes spin {
    to { transform: rotate(360deg); }
  }
  .spinner {
    width: 48px;
    height: 48px;
    border: 5px solid #e3f1ec;
    border-top-color: #0f5c4d;
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
  }
</style>
<div class="spinner"></div>
<p style="font-family: Arial, sans-serif;">Loading your grades…</p>

A circle with one colored border side, rotating forever. Notice linear: a spinner should turn at a constant speed.

Multiple steps

Use percentages for animations with more than two steps:

Example A pulsing LIVE badge
<style>
  @keyframes pulse {
    0%   { box-shadow: 0 0 0 0 rgba(206, 17, 38, 0.6); }
    70%  { box-shadow: 0 0 0 14px rgba(206, 17, 38, 0); }
    100% { box-shadow: 0 0 0 0 rgba(206, 17, 38, 0); }
  }
  .live {
    display: inline-block;
    background: #CE1126;
    color: white;
    font: bold 14px Arial, sans-serif;
    padding: 6px 12px;
    border-radius: 999px;
    animation: pulse 1.6s infinite;
  }
</style>
<span class="live">● LIVE</span> <span style="font-family: Arial, sans-serif;">Intramurals Basketball Finals</span>

alternate: back and forth

Example A bouncing scroll arrow
<style>
  @keyframes bounce {
    from { transform: translateY(0); }
    to   { transform: translateY(12px); }
  }
  .arrow {
    font-size: 36px;
    color: #0f5c4d;
    display: inline-block;
    animation: bounce 0.6s ease-in-out infinite alternate;
  }
</style>
<p style="font-family: Arial, sans-serif;">Scroll down for the lessons</p>
<span class="arrow">↓</span>

Project: a jeepney driving across

Example Jeepney animation
<style>
  .road {
    position: relative;
    height: 150px;
    overflow: hidden;
    background: linear-gradient(#bde3f5 0 70%, #6b6b6b 70% 100%);
    border-radius: 12px;
  }
  .jeep {
    position: absolute;
    bottom: 12px;
    width: 140px;
    animation: drive 5s linear infinite;
  }
  @keyframes drive {
    from { left: -150px; }
    to   { left: 100%; }
  }
</style>
<div class="road">
  <img class="jeep" src="/img/jeepney.svg" alt="A jeepney driving across the road">
</div>

Try changing 5s to 2s for a jeepney in a hurry, or add animation-direction: alternate; so it drives back and forth. (A smoother version would animate transform: translateX() instead of left, as you learned in the last lesson.)

Respect reduced motion

Some people feel dizzy or sick from movement on screen. Phones and computers have a "reduce motion" setting, and CSS can detect it. Always add this at the end of your CSS when you use animations:

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

This is one of the few good uses of !important: it makes sure the user's accessibility setting wins over every animation on the page.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does @keyframes do?
2. Which value makes an animation repeat forever?
3. What does animation-direction: alternate; do?
4. Why add a prefers-reduced-motion media query?

By Sir Reyel · Last updated 2026-12-04