KodingKaro
CSS · Intermediate · CSS Transitions and Animations

Transitions for interface states

A transition smooths the change between two CSS states, such as a button’s resting and hovered appearance.

✓ What you will learn in this topic

  • Explain the key idea: Transitions animate known property changes and should remain brief and purposeful.
  • Apply the concept: The link color and button color/position ease between states rather than changing abruptly.
  • Avoid this common mistake:

Concept explanation

A transition needs a property, duration, and optionally a timing function and delay. Put the transition on the base rule so it works both when entering and leaving the state. Avoid transition: all when specific properties are known; broad transitions can animate unintended changes.

Motion should confirm an interaction, not delay it. Keep controls responsive and ensure focus remains visible throughout the transition.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo a {
  color: #1d4ed8;
  transition: color 160ms ease;
}
.demo a:hover {
  color: #0f172a;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo a {
  color: #1d4ed8;
  transition: color 160ms ease;
}
.demo a:hover {
  color: #0f172a;
}

Expected result: The link color and button color/position ease between states rather than changing abruptly.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card button {
  transition: transform 160ms ease, background-color 160ms ease;
}
.card button:hover {
  transform: translateY(-2px);
  background-color: #1e40af;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Animating layout-heavy properties or essential content indefinitely can create jank and discomfort.

Best practice

Prefer transform and opacity for motion, keep durations purposeful, and honor reduced-motion preferences.

Practice exercise

Add a short hover and focus transition to a button, then disable it for users who request reduced motion. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Transitions animate known property changes and should remain brief and purposeful.
  • Prefer transform and opacity for motion, keep durations purposeful, and honor reduced-motion preferences.

Knowledge check

Where should a transition declaration usually be placed?

Reveal the explanation

Putting the transition on the base style allows both state changes to animate.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo a {
  color: #1d4ed8;
  transition: color 160ms ease;
}
.demo a:hover {
  color: #0f172a;
}

Expected result:

The link color and button color/position ease between states rather than changing abruptly.

Hands-on practice task

Add a short hover and focus transition to a button, then disable it for users who request reduced motion.

In this section

Other lessons in CSS Transitions and Animations

Broaden your knowledge

Related learning topics