Sub topic 5
Triggering transitions with :hover, :focus-visible and class changes
This chapter assumes you can write a transition declaration (property, duration, timing function, delay), and that you know basic selectors, the cascade, and how to toggle a class with JavaScript.
What actually starts a transition
A transition does not listen for events. The browser starts one when the computed value of a property changes and the element has a transition declared for that property. The cause of the change doesn't matter. A pseudo-class, a class swap, an inline style set from JavaScript, or a media query can all trigger one.
Your job is to arrange two things:
- A rule for the resting state that declares the
transition. - A second rule, active only in the triggered state, that changes the value.
Pseudo-class triggers
.button {
background-color: #2563eb;
transform: translateY(0);
transition: background-color 200ms ease-out, transform 200ms ease-out;
}
.button:hover,
.button:focus-visible {
background-color: #1d4ed8;
transform: translateY(-2px);
}The transition sits on .button, not on the hover rule. Because of that, it applies in both directions. The button animates when the pointer arrives and again when it leaves.
:focus-visible matches when the browser decides focus should be visibly indicated. That is usually keyboard focus, and usually not a mouse click on a button. Pairing it with :hover gives keyboard users the same feedback that pointer users get.
Class changes
Pseudo-classes are controlled by the browser. For states your own code controls, such as an open menu, change a class.
<button class="toggle" type="button" aria-expanded="false">Menu</button>
<nav class="panel">Panel content</nav>.panel {
opacity: 0.4;
transform: translateX(-16px);
transition: opacity 250ms ease-out, transform 250ms ease-out;
}
.panel.is-open {
opacity: 1;
transform: none;
}const toggle = document.querySelector('.toggle');
const panel = document.querySelector('.panel');
toggle.addEventListener('click', () => {
const open = panel.classList.toggle('is-open');
toggle.setAttribute('aria-expanded', String(open));
});JavaScript only flips the class. CSS owns the motion, so you can change the duration or easing without touching the script.
If the user clicks again in the middle of a transition, the browser reverses from the current value. It does not jump to the end first.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Element animates in but snaps back on exit | `transition` is declared only in the `:hover` or class rule | Move it to the base rule, or give each state its own timing on purpose |
| Nothing animates to or from `height: auto` | `auto` has no numeric value to interpolate | Animate `transform` or `opacity`, or use a fixed length |
| Element appears instantly after `display: none` | No before-style exists to transition from | Fade `opacity` instead, or use `@starting-style` (covered later) |
| Hover effect stays "stuck" on a phone | Touch devices keep `:hover` after a tap | Wrap hover rules in `@media (hover: hover)` |
| A newly inserted element skips its transition | The class was added in the same frame as the insert | Add the class after a frame, or use `@starting-style` |
Exercise
A card has .card:hover { transform: scale(1.05); transition: transform 300ms; }. It grows smoothly on hover but snaps back instantly when the pointer leaves. Why, and what is the fix?
Show answer
The transition is declared only in the :hover rule, so it applies only while the element is hovered. When the pointer leaves, the hover rule stops matching and the transition declaration goes away with it, so the change back is instant. Move transition: transform 300ms; into the base .card rule so it applies in both directions.