Skip to content

CSS Tutorial CSS Transitions

CSS Transitions

Lesson 134 of 182 2 min read By Deepak Paswan Updated Oct 4, 2026

Normally when a style changes, such as a button’s color on hover, the change is instant. A transition tells the browser to move smoothly from the old value to the new one.

index.html
HTML
<style>
  .btn {
    display: inline-block; padding: 12px 24px; background: #1a5276; color: #fff; border-radius: 6px;
    transition: background-color 0.3s;
  }
  .btn:hover { background-color: #d94a00; }
</style>
<span class="btn">Hover over me</span>

Remove the transition line and press Run. The color change becomes instant.

How it is built

A transition always needs two things:

  1. Two states: a normal rule and a changed one, usually with :hover, :focus or a class added by JavaScript.
  2. A transition declaration on the normal state: which property, and how long.
style.css
CSS
.btn {
  transition: background-color 0.3s;   /* property, then duration */
}

Put the transition on the element’s normal rule, not only on :hover. If it is only on :hover, it animates when you enter but snaps back when you leave.

Many properties at once

Separate several with commas:

style.css
CSS
.card { transition: transform 0.2s, box-shadow 0.2s; }

What can be transitioned?

Properties with values that can be calculated between: colors, sizes, opacity, positions, transforms. Properties like display and font-family jump instantly, because there is nothing “between” block and none.

lightbulb
Keep it quick
150 to 300 milliseconds feels responsive. Longer than half a second feels slow for small interface elements.

The next lesson digs into the individual properties: Transition Property and Duration.