agyanadda@gmail.com

CSS Transitions

Advanced CSS 1 min read Updated Dec 1, 2026 Topic 35 of 39

A transition animates a property smoothly when its value changes (for example on :hover).

.btn { transition: background-color 0.3s ease, transform 0.3s ease; }
.btn:hover { background-color: navy; transform: scale(1.05); }

Parts

  • transition-property — what to animate (all, opacity, transform)
  • transition-duration — how long (0.3s, 300ms)
  • transition-timing-function — ease, linear, ease-in, ease-out, ease-in-out
  • transition-delay — wait before starting

Put the transition on the normal state, not only on :hover, so it animates both ways.

Example

Example — Hover transitions HTML
<style>
  .btn { background: #1f386b; color: white; border: none; padding: 12px 24px; border-radius: 6px; font-size: 16px; cursor: pointer; transition: background-color .3s ease, transform .3s ease; }
  .btn:hover { background: #e8590c; transform: translateY(-3px); }
  .grow { width: 100px; height: 40px; background: seagreen; margin-top: 16px; transition: width 1s ease-in-out; }
  .grow:hover { width: 300px; }
</style>

<button class="btn">Hover me</button>
<div class="grow"></div>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which property sets how long a transition takes?

Exercise 2 True / False

A transition needs a change of value (like :hover) to run.

Exercise 3 Fill in the Blank

transition: opacity 0.5s ___; starts slowly and ends slowly (ease-in-out family).

Frequently Asked Questions

Only properties with in-between values (colours, sizes, opacity, transforms). display cannot be transitioned.