TExcelTAD
Home › CSS › CSS Transitions

CSS Transitions

When a property changes, for example on :hover, the new value normally applies instantly. A transition animates the change smoothly over a period of time. It's the easiest way to make an interface feel polished: one line of CSS, and no JavaScript.

Transition properties

Property Purpose Example
transition-property Which properties to animate background-color, transform, all
transition-duration How long 0.3s, 250ms
transition-timing-function Speed curve ease, linear, ease-in-out
transition-delay Wait before starting 0.1s
transition Shorthand: property duration timing delay transition: transform .3s ease-out;

Put the transition on the element's normal state, not only on :hover. That way it animates both in and out.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 20px; padding: 30px; }
  .btn { padding: 14px 22px; border: none; border-radius: 10px; font-size: 16px; color: white; background: #3949ab; cursor: pointer; }
  .instant:hover { background: #e91e63; }
  .smooth { transition: background-color .4s, transform .4s; }
  .smooth:hover { background: #e91e63; transform: scale(1.1); }
</style>
</head>
<body>
  <button class="btn instant">No transition</button>
  <button class="btn smooth">With transition</button>
</body>
</html>

Multiple properties

Separate several transitions with commas, each with its own timing. Avoid transition: all in production code. It also animates properties you never meant to animate, and it can hurt performance.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 30px; }
  .box {
    width: 120px; height: 80px; background: #26a69a; border-radius: 8px; color: white; display: grid; place-items: center;
    transition: width 1s ease, background-color .5s linear 1s, border-radius .5s ease 1.5s;
  }
  .box:hover { width: 300px; background: #ff7043; border-radius: 40px; }
</style>
</head>
<body>
  <div class="box">hover me</div>
  <p>Width first, then colour (after a 1s delay), then roundness (after 1.5s).</p>
</body>
</html>

Timing functions

The timing function shapes the speed over time. All of these bars take the same total time. Hover over the track to start them:

Value Feel
ease (default) Quick start, gentle finish
linear Constant speed, which can look mechanical
ease-in Starts slow, ends fast (good for exits)
ease-out Starts fast, ends slow (good for entrances)
ease-in-out Slow at both ends
cubic-bezier(x1, y1, x2, y2) A custom curve; y values outside 0–1 overshoot
steps(n) Jumps in n discrete steps
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 8px; }
  .track { border: 2px dashed #b0bec5; padding: 6px; border-radius: 8px; }
  .bar { width: 90px; margin: 4px 0; padding: 4px 6px; font-size: 12px; color: white; border-radius: 4px; background: #5c6bc0; transition: transform 2s; }
  .track:hover .bar { transform: translateX(220px); }
  .linear { transition-timing-function: linear; }
  .ease { transition-timing-function: ease; }
  .in { transition-timing-function: ease-in; }
  .out { transition-timing-function: ease-out; }
  .bounce { transition-timing-function: cubic-bezier(.5, -0.6, .5, 1.6); background: #e91e63; }
  .steps { transition-timing-function: steps(5); background: #00897b; }
</style>
</head>
<body>
  <div class="track">
    <div class="bar linear">linear</div>
    <div class="bar ease">ease</div>
    <div class="bar in">ease-in</div>
    <div class="bar out">ease-out</div>
    <div class="bar bounce">cubic-bezier</div>
    <div class="bar steps">steps(5)</div>
  </div>
</body>
</html>

Tip: UI transitions usually feel best between 150ms and 400ms. Anything longer starts to feel sluggish when users interact often.

What can be transitioned?

Only animatable properties with intermediate values can be transitioned: colours, lengths, opacity, transforms, shadows and so on. display traditionally cannot, because there is no value halfway between none and block. Instead, fade with opacity and visibility:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 20px; }
  .menu { position: relative; display: inline-block; }
  .trigger { padding: 10px 16px; background: #37474f; color: white; border-radius: 8px; }
  .panel { position: absolute; top: 48px; left: 0; width: 180px; padding: 10px; background: white; border-radius: 8px;
           box-shadow: 0 8px 20px rgba(0,0,0,.2);
           opacity: 0; visibility: hidden; transform: translateY(-8px);
           transition: opacity .25s, transform .25s, visibility .25s; }
  .menu:hover .panel { opacity: 1; visibility: visible; transform: translateY(0); }
  .panel div { padding: 6px; }
</style>
</head>
<body>
  <div class="menu">
    <div class="trigger">Hover for menu ▾</div>
    <div class="panel"><div>Profile</div><div>Settings</div><div>Sign out</div></div>
  </div>
</body>
</html>

Performance

transform and opacity can be animated on the GPU without recalculating layout, so they stay smooth even on slow phones. Animating width, height, top or margin makes the browser redo the layout on every frame. Where possible, use transform: translate() and scale() instead.

Test yourself

Where should you put the transition declaration so it animates both on hover and when the mouse leaves?

  1. Only in the :hover rule
  2. On the element's normal (non-hover) rule
  3. In a @keyframes block
  4. On the body

In transition: opacity .3s ease-in .1s;, what is .1s?

  1. Duration
  2. Delay
  3. Timing function
  4. Iteration count

Which properties are cheapest to animate?

  1. width and height
  2. top and left
  3. transform and opacity
  4. margin and padding