Reading history

CSS Transitions and Animations (@keyframes) with Examples

5 min read 0 views

Learn CSS transitions and keyframe animations: duration, timing functions, transforms, iteration and fill modes, performance and prefers-reduced-motion.

Small movements make an interface feel responsive: a button that darkens and lifts on hover, a card that grows a shadow, a spinner while data loads. CSS gives you two tools for this without any JavaScript. Transitions smoothly animate a property when its value changes, and keyframe animations run a sequence of steps by themselves, once or forever. This lesson shows both and how to keep motion accessible.

In this lesson you will learn

  • The transition property: property, duration, timing function and delay
  • Animating with transform (translate, scale, rotate)
  • Defining @keyframes and the animation property
  • Iteration counts, fill modes and delays
  • Respecting prefers-reduced-motion

Transitions

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Transitions</title>
  <style>
    body { font-family: sans-serif; margin: 10px; display: flex; gap: 12px; flex-wrap: wrap; }
    .btn { background: #1565c0; color: white; border: none; padding: 10px 18px; border-radius: 6px;
           font-size: 15px; cursor: pointer;
           transition: background-color 0.3s ease, transform 0.2s ease; }
    .btn:hover { background: #0d47a1; transform: translateY(-3px); }
    .card { width: 150px; padding: 12px; background: #fff3e0; border-radius: 8px;
            box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: box-shadow .3s, transform .3s; }
    .card:hover { box-shadow: 0 8px 18px rgba(0,0,0,.25); transform: scale(1.05); }
    .grow { width: 60px; height: 30px; background: #2e7d32; transition: width 1s ease-in-out; }
    .grow:hover { width: 200px; }
  </style>
</head>
<body>
  <button class="btn">Hover me</button>
  <div class="card">Hover this card</div>
  <div class="grow" title="hover to grow"></div>
</body>
</html>

Result

  • A transition is written on the element’s normal state: transition: background-color 0.3s ease. When :hover changes the background, the browser animates between the two values over 0.3 seconds. Hover over the items in the result to try it.
  • List several properties with commas, each with its own duration.
  • transform: translateY(-3px) lifts the button and scale(1.05) enlarges the card. Transforms do not affect the layout around the element, so neighbours do not jump.
  • The timing function controls the speed curve: ease (the default), linear, ease-in, ease-out, ease-in-out or a custom cubic-bezier().

Keyframe Animations

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Keyframe animations</title>
  <style>
    body { font-family: sans-serif; margin: 10px; display: flex; gap: 24px; align-items: center; }
    @keyframes spin { to { transform: rotate(360deg); } }
    .spinner { width: 36px; height: 36px; border: 4px solid #cfd8dc; border-top-color: #1565c0;
               border-radius: 50%; animation: spin 1s linear infinite; }
    @keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.2); } }
    .heart { font-size: 32px; color: #c62828; animation: pulse 1.2s ease-in-out infinite; }
    @keyframes slide { from { transform: translateX(-40px); opacity: 0; } to { transform: none; opacity: 1; } }
    .msg { background: #e8f5e9; padding: 8px 12px; border-radius: 6px;
           animation: slide .8s ease-out 0.3s both; }
    @media (prefers-reduced-motion: reduce) { .spinner, .heart, .msg { animation: none; } }
  </style>
</head>
<body>
  <div class="spinner" aria-label="Loading"></div>
  <div class="heart">&#9829;</div>
  <div class="msg">Saved successfully</div>
</body>
</html>

Result

  • @keyframes spin { to { transform: rotate(360deg); } } defines an animation named spin. Steps can use from/to or percentages such as 0%, 50%, 100%.
  • animation: spin 1s linear infinite applies it: name, duration, timing function and iteration count.
  • The message uses a 0.3s delay and the fill mode both, so it stays invisible during the delay and keeps its final state afterwards.
  • The prefers-reduced-motion media query turns the animations off for people who have asked their operating system to reduce motion, for example because movement makes them dizzy.

Which Properties to Animate

PropertyPerformanceNotes
transformVery smoothMove, scale, rotate without changing layout
opacityVery smoothFades
background-color, color, box-shadowGoodRepaints the element
width, height, top, marginSlowerRecalculates layout on every frame
displayNot animatableUse opacity and visibility instead

Common Mistakes

  1. Putting the transition only on :hover, so the element animates in but snaps back instantly.
  2. Using transition: all, which animates properties you did not intend and can hurt performance.
  3. Animations that are too slow or too many; most interface transitions should take 150 to 300 milliseconds.
  4. Ignoring reduced-motion preferences.

Practice Exercises

Exercise 1. Make a link’s underline colour change smoothly over 0.2 seconds on hover.

Show answer
a { text-decoration-color: transparent; transition: text-decoration-color .2s; }
a:hover { text-decoration-color: currentColor; }

Exercise 2. Write a keyframe animation that fades an element in from 0 to full opacity over half a second, once.

Show answer
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.notice { animation: fade-in .5s ease-out; }

Frequently Asked Questions

When should I use a transition and when an animation?

Use a transition for a change between two states triggered by something, such as hover, focus or a class added by JavaScript. Use keyframes for multi-step motion or anything that runs by itself, such as loaders and attention effects.

Can JavaScript start CSS animations?

Yes. The usual way is to add or remove a class with element.classList.add("open"), which triggers the transition or animation defined in CSS. See the JavaScript tutorials.

Summary

  • transition animates a property between states.
  • @keyframes plus animation runs multi-step motion.
  • Animate transform and opacity for the smoothest results.
  • Keep motion short and respect prefers-reduced-motion.

Next: CSS Pseudo-classes and Pseudo-elements.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *