CSS Transforms

Translate

Move
/* slides 50px to the right */
.moveBox {
  transition: transform .6s ease;
}

.card:hover .moveBox {
  transform: translateX(50px);
}

Rotate

Rotate
/* spins around its centre */
.spinBox {
  transition: transform .6s ease;
}

.card:hover .spinBox {
  transform: rotate(45deg);
}

Scale

Scale
/* grows the box to 140% */
.growBox {
  transition: transform .6s ease;
}

.card:hover .growBox {
  transform: scale(1.4);
}

Skew

Skew
/* tilts the box sideways */
.tiltBox {
  transition: transform .6s ease;
}

.card:hover .tiltBox {
  transform: skew(20deg);
}

Animation Properties

Duration

@keyframes move {
  from {
    transform: translateX(-70px);
  }
  to {
    transform: translateX(70px);
  }
}

.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-iteration-count: infinite;
}

Delay

.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-delay: 1s;
  animation-iteration-count: infinite;
}

Iteration Count

/* runs 3 times, then stops */
.ball {
  animation-name: move;
  animation-duration: 1s;
  animation-iteration-count: 3;
}

Animation Properties

Direction

/* forwards, then backwards */
.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

Fill Mode

/* keeps the last frame's style */
.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-fill-mode: forwards;
}

Play State

/* hover the ball to pause it */
.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-iteration-count: infinite;
}

.ball:hover {
  animation-play-state: paused;
}

Animation Timing Functions

Ease

/* slow, fast, then slow */
.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-timing-function: ease;
  animation-iteration-count: infinite;
}

Cubic Bezier

/* pulls back, then overshoots */
.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-timing-function:
    cubic-bezier(.68, -.55, .27, 1.55);
  animation-iteration-count: infinite;
}

Steps

/* jumps in 5 steps, no gliding */
.ball {
  animation-name: move;
  animation-duration: 2s;
  animation-timing-function: steps(5);
  animation-iteration-count: infinite;
}

Why & Where We Use CSS Animations

CSS transitions and animations are mostly used to improve UX, provide instant visual feedback, and make interfaces feel alive without heavy JavaScript.

Hover & Click Feedback

Buttons, cards, links

Loaders & Spinners

Skeleton screens, loading icons (reduces perceived wait time)

Attention Cues

Pulsing notification badges, bouncing scroll arrows

Entrance Effects

Smooth fade-ins for modals, menus

When to Use JavaScript Instead

While CSS handles simple state changes cleanly, JavaScript—using the Web Animations API or GSAP (GreenSock, an industry-standard JS animation library)—is better suited for complex, interactive motion.

User-State Driven Sequences

Animations synced to scroll progress, drag gestures, or game logic

Complex Timelines & Physics

Multi-step choreographies or natural spring mechanics

Playback Control

When you need to scrub a timeline, reverse dynamically, or interrupt an animation based on logic

In the Wild

In the wild:
CSS

Standard UI buttons, dropdowns, simple hover states

In the wild:
JS (GSAP)

Complex scroll-triggered product storytelling (e.g., Apple)

Two Golden Rules:
Performance & Accessibility

Rule 1: Animate transform & opacity only

Animating layout properties like width, height, top, left, or margin forces the CPU to recalculate layout and repaint pixels on every frame, causing lag. In contrast, transform and opacity run directly on the GPU, keeping animations smooth at 60fps.

Rule 2: Respect prefers-reduced-motion

When someone turns on "Reduce motion" in their OS settings, the browser lets your CSS know, but nothing changes on its own.

You decide what happens inside a @media (prefers-reduced-motion: reduce) block: switch heavy movement off, or swap it for something calmer, like a fade.

Hover this card to see a calmer version below.

Now to learn how to animate something in CSS!