Web animation・First steps

Change smoothly with transition

Turn a color or size that "snaps" over into one that "softly" shifts. Learn transition, the doorway into CSS animation.

Last time you saw an example where a button softly grew when you moved the cursor over it. That “softly” is transition.

With nothing added, it “snaps” over

First, let’s see what happens with no transition.

Both the color and the corner rounding switch over with a jolt the instant the cursor lands.

Add transition and it changes “softly”

Just add one line, transition, to that same CSS.

It’s the same change, yet the impression is completely different.

How to write transition

<div class="box">Hover over me</div>
.box {
  transition: background-color 0.4s ease;
}

Separated by spaces, each part means this:

PartRole
background-colorWhat to change (the property name)
0.4sHow much time to take
easeThe rhythm of the change (easing — more below)

When you want to smooth several properties at once, separate them with commas (like background-color 0.4s ease, border-radius 0.4s ease in the example above). When you want all of them smoothed together, you can also use all in place of a property name.

<div class="box">Hover over me</div>
.box {
  transition: all 0.4s ease;
}

Easing: the rhythm of the change

Change the ease part and you change how the motion picks up speed.

ValueHow the motion feels
easeStarts slow, ends slow (the default — a natural feel)
linearA constant speed from start to finish
ease-inStarts slow, ends with a rush
ease-outStarts with a rush, ends slow

How long should the time be?

The s in 0.4s means seconds. 0.2s is 0.2 seconds. This one number changes the impression a lot.

TimeImpression
Around 0.1sToo fast — the change is hard to notice
0.2–0.3sBrisk and comfortable (the go-to for buttons and such)
0.4–0.6sRelaxed and refined (good for cards and larger elements)
1s or moreEasily feels like you’re being kept waiting

When in doubt, start from 0.2–0.3 seconds and adjust as you try it out for real. The length that feels “good” also changes with the size of the part you’re moving.

Delay the start with transition-delay

Write two time values and the second becomes “the wait before it starts” (the delay).

<div class="box">Hover over me</div>
.box {
  transition: background-color 0.4s 0.2s ease;
}

In this example the motion is “the cursor lands, it waits 0.2 seconds, then the color changes over 0.4 seconds.” It shines when you want to stagger the start of several elements a little and have them change in order.

Some properties can’t be transitioned

transition can only smooth values where an in-between state can be calculated. For a color, “a shade between purple and orange”; for a size, “180px between 160px and 200px” — these can be made, so they can change smoothly.

On the other hand, values with no in-between, like display: none and display: block, snap over even with transition added.

Can be smoothedCan’t (snaps over)
color, background-color (color)display (show/hide)
width, height, font-size (size)font-family (kind of font)
opacity (transparency), transform (transformation)

Summary of this lesson

  1. Add transition and a value’s change becomes smooth
  2. The way to write it is property-name time easing
  3. Use all for all properties, and commas to specify several
  4. Put transition on the element before the change

FAQ

Why does the transition not work and it snaps over instead?
The rule is to write transition on the "before" selector (the normal, resting side). Check that you haven't written it only on the hover side, and that the property name you're changing matches.
Why doesn't switching display between none and block animate?
display is a property that can't have an in-between state, so it isn't a target for transition. When you want something to softly fade out or in, change opacity (transparency) instead.
How long is just right for the time?
0.2–0.3 seconds is a comfortable rule of thumb. Go over 1 second and it starts to feel like you're being kept waiting.