TetSharing
CSS tutorial · 12 chapters
  1. Get started
  2. Selectors
  3. Cascade and specificity
  4. Colors
  5. Units and sizes
  6. Box model
  7. Text and fonts
  8. Flexbox
  9. Grid layout
  10. Media queries
  11. Custom properties
  12. Transitions

Final CSS quiz
Cheat sheet: css selectors

Chapter 12 of 12 · By Ethan Cole · Updated November 21, 2026

CSS Transitions: Smooth Hover and State Changes

When a property changes, for example because a button is hovered, the new value normally appears instantly. A transition tells the browser to animate from the old value to the new one over a short time. A small, quick transition makes an interface feel responsive; a slow one makes it feel sluggish.

The four parts

.button {
  background: #0e7490;
  transition: background-color 200ms ease-out;
}
.button:hover { background: #155e75; }

The transition shorthand combines:

  1. Property: what to animate (background-color, transform, opacity), or all.
  2. Duration: how long, in ms or s. Interface feedback usually feels right between 100 and 300 milliseconds.
  3. Timing function: the speed curve. ease (the default) starts quickly and slows down; linear is constant; ease-out decelerates into the final value, which suits things entering or reacting.
  4. Delay: an optional wait before starting.

Separate several transitions with commas: transition: opacity 150ms, transform 300ms ease-out;.

The transition goes on the element's base rule, not the :hover rule, so it runs in both directions: on hover and when the pointer leaves. The example reads the transition settings back from the browser and prints them. Hover the button in the preview to see the effect.

HTML · editable

Expected page text

Hover me
property: background-color, transform
duration: 0.2s, 0.15s
timing: ease-out, ease

What can be animated

Properties with in-between values can transition: colors, lengths, opacity, transform. Properties without in-between values, like display or font-family, switch instantly. For smooth performance, prefer animating transform and opacity. The browser can usually handle them without recalculating the layout, whereas animating width or top makes it redo layout on every frame.

Respect reduced motion

Some people get dizzy or distracted by movement and turn on a reduce-motion setting. Honor it:

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0.01ms !important; }
}

Small color fades are generally fine, but sliding panels and zooming cards should be calmed down.

Transitions versus animations

Transitions animate between two states when something changes. For multi-step or looping motion that runs on its own, CSS has @keyframes animations, a separate feature built on the same timing ideas.

Summary

  • transition: property duration timing-function delay on the base rule.
  • Keep interface transitions short, around 100 to 300 ms.
  • Prefer transform and opacity, and respect prefers-reduced-motion.

Quiz: Transitions

1. Where should the transition property go to animate both hover-in and hover-out?

Declared on the base rule, it applies to changes in both directions.

2. Which properties are usually cheapest to animate?

transform and opacity normally avoid recalculating layout on each frame.

3. What does transition: opacity 0.3s ease-in 1s mean?

The first time is the duration and the second is the delay.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.