Chapter 04 of 11Part 2 · CSS

CSS transitions: hover, focus and entry

The transition shorthand, hover and focus states for every input, and how to animate elements in from display: none.

8 minBy Zuzo StudioUpdated

9 min

In this chapter you will

  • write the transition shorthand and give entering and leaving different timings
  • build hover, focus and press states that work for mouse, keyboard and touch
  • animate an element in from display: none with @starting-style

Before you startThis builds on Staggered animation and sequencing.

Check your understanding

3 questions

Q1What does transition: opacity 300ms 100ms ease-out do?

Show the answer

Fades over 300ms, starting after a 100ms delay In the shorthand the first time value is always the duration and the second is always the delay.

Q2A menu is hidden with display: none and given transition: opacity 250ms. When it is shown, it appears instantly. Why?

Show the answer

Transitions do not run on an element's first style update after display: none, so it needs @starting-style An element that was not displayed has no previous value to transition from. @starting-style provides one, and transition-behavior: allow-discrete lets display take part.

Q3Why wrap hover transforms in @media (hover: hover)?

Show the answer

So the hover effect does not get stuck on after a tap on touch screens On touch devices a tap can trigger :hover and leave it applied. The media query limits hover motion to devices with a real pointer.

Key takeaways

  1. A transition animates a property between its old and new values when the computed style changes.
  2. The first time value in the shorthand is the duration and the second is the delay, and the default duration of 0s means nothing animates.
  3. Name the properties you transition instead of using all, and prefer transform and opacity for anything large.
  4. Wrap hover motion in @media (hover: hover) and make :focus-visible at least as clear as hover, with no delay.
  5. @starting-style and transition-behavior: allow-discrete let elements animate in and out of display: none.
Up next · Chapter 05 · 10 min CSS keyframe animation: @keyframes and the animation property How @keyframes and the animation property work, from fill mode to loops, and when to choose them over a transition.

02FAQ

Questions on this chapter

Something else on your mind? Email the studio and a designer, not a salesperson, replies within a day.

What is the difference between a CSS transition and a CSS animation?

A transition animates between two states when a property changes, such as on hover or when a class is added. A keyframe animation runs on its own with as many steps as you define, and can loop, which suits things like spinners and scroll cues.

Can you transition display: none in CSS?

Yes, in current browsers. Add display to the transition list with transition-behavior: allow-discrete, and set the starting values inside @starting-style. MDN marks both as Baseline 2024, and older browsers simply show and hide the element without animating.