Sub topic 1
Recap: how transitions use the before-change style
Entry and exit animations with @starting-style build on one idea from ordinary CSS transitions: the before-change style. This chapter reviews that model and shows exactly where it fails for newly rendered elements. The later chapters fix that failure.
Prerequisites
This chapter assumes you already know:
- CSS selectors, the cascade, and how a later or more specific rule wins.
- The basic transition properties:
transition-property,transition-duration,transition-timing-functionandtransition-delay, plus thetransitionshorthand. - What
display: nonedoes: the element generates no box and is not rendered. - Basic JavaScript for toggling a class and inserting an element.
You do not need to know @starting-style, dialog or popover yet.
What a transition actually does
A transition is a rule that says: when the computed value of this property changes, don't jump to the new value. Interpolate (blend) from the old value to the new one over a duration.
A computed value is the value after the cascade has been resolved, such as opacity: 0.5 or transform: translateY(1rem). The browser calculates computed values for every element in a step called a style update (also called a style recalculation). A transition can only start during a style update, and only when that update finds a difference.
The CSS Transitions specification names the two sets of values it compares:
- The before-change style is the element's computed style as of the previous style update. If a transition is already running, its current in-progress value is used.
- The after-change style is the computed style produced by the current style update, with no transition effects applied yet.
For each property, the browser compares the two. If the values differ and the other conditions hold, it starts a transition from the before-change value to the after-change value.
Look at the first question in the chart. Everything in this tutorial exists because that answer can be "No".
A worked example
Here is a complete page with a button that fades when hovered.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hover transition</title>
<style>
.btn {
padding: 0.6rem 1.2rem;
border: 0;
border-radius: 0.4rem;
background: #334;
color: white;
opacity: 1;
transition: opacity 0.4s ease;
}
.btn:hover {
opacity: 0.4;
}
</style>
</head>
<body>
<button class="btn">Hover me</button>
</body>
</html>When the pointer enters the button, the next style update gives the button a new computed opacity of 0.4. The before-change style has opacity: 1. The two differ, opacity is in transition-property, and the duration is 0.4s, so the browser interpolates from 1 to 0.4. When the pointer leaves, the same logic runs in reverse and the opacity returns to 1.
Which rule supplies the transition settings
The transition-* values that count are the ones in the after-change style. This is a common source of lopsided animations.
Move the transition declaration into the hover rule:
.btn {
opacity: 1;
}
.btn:hover {
opacity: 0.4;
transition: opacity 0.4s ease;
}Now the fade-out on hover animates, because the after-change style has a 0.4s transition. The fade-back does not animate. When the pointer leaves, the after-change style comes from .btn alone, which declares no transition, so the value snaps back to 1.
Declaring transition on the base rule makes it apply in both directions. Declare it on a state rule only when you want a different timing for entering that state.
You can use this on purpose. A menu might open slowly and close fast:
.menu {
opacity: 0;
transition: opacity 0.1s ease-in;
}
.menu.open {
opacity: 1;
transition: opacity 0.4s ease-out;
}Conditions that must all hold
A transition starts only when all of these are true. The table lists the common ways each one fails.
| Condition | How it fails |
|---|---|
| The element had a before-change style | The element was just inserted into the document, or it was `display: none` in the previous update |
| The computed value changed | The class toggle did not change the property you transition, or a more specific rule overrides it |
| The property is in `transition-property` | You listed `opacity` but animated `transform`, or you misspelled the property name |
| The values can be interpolated | `height: 0` to `height: auto`, or `display: none` to `display: block` (without `allow-discrete`) |
| Duration plus delay is greater than zero | The default `transition-duration` is `0s`, so a transition with no duration never runs |
The first row is the subject of the next chapter. The fourth row, discrete properties like display, comes later, along with the transition-behavior: allow-discrete keyword that changes it.
Style updates are batched
The browser does not run a style update after every line of JavaScript. It batches changes and runs the update when it needs to render the next frame. Intermediate changes made in the same task are never seen as a before-change style.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Batched changes</title>
<style>
#box {
width: 100px;
height: 100px;
background: teal;
opacity: 1;
transition: opacity 1s;
}
</style>
</head>
<body>
<button id="go">Run</button>
<div id="box"></div>
<script>
const box = document.getElementById('box');
document.getElementById('go').addEventListener('click', () => {
box.style.opacity = '0';
box.style.opacity = '1';
});
</script>
</body>
</html>Clicking the button sets the opacity to 0 and then back to 1 in the same task. No style update happens between the two assignments. The next update compares the old computed 1 with the new computed 1, finds no change, and starts nothing. The box does not flicker.
Some JavaScript calls force the browser to run a style update early. Reading getComputedStyle(box).opacity or box.offsetWidth are examples. Before @starting-style existed, developers used this to animate newly inserted elements: add the element, read a layout property to flush styles, then add the class that triggers the change. It works, but it is easy to forget and awkward to read. It is also the problem @starting-style replaces.
Interrupted and reversed transitions
The before-change style uses the current value of a running transition, not the value the transition was heading toward. Suppose a 0.4s fade from 1 to 0.4 is halfway through when the pointer leaves. The before-change opacity is about 0.7, the after-change opacity is 1, and the browser starts a new transition from 0.7.
The specification also shortens a transition that reverses an unfinished one. The reversal takes only as long as the portion already travelled, so the element does not spend the full duration returning a short distance. You do not need to code anything for this, but it explains why quick hover in and out looks smooth.
What a first render looks like
Consider this rule on its own:
.card {
opacity: 1;
transition: opacity 0.4s ease;
}If a .card element is added to the page, you might expect it to fade up from nothing. It does not. A newly rendered element has no previous style update, so it has no before-change style. The browser has nothing to interpolate from, and the card appears at opacity: 1 immediately.
The same thing happens when an element goes from display: none to a visible display value. An element with display: none has no computed style that the transition machinery tracks, so when it becomes visible, it counts as newly rendered. This is why fading an element in from hidden by toggling display fails even when you write a correct transition.
The next chapters call these two cases the first style update, and they cover how @starting-style supplies the missing before-change values.
Common errors to check first
When a transition does nothing, check these in order:
- Open DevTools and confirm the computed
transition-durationon the element is greater than0s. - Confirm the property you change appears in
transition-property, or that you usedall. - Check that the value actually changes between the two states. A more specific selector can keep the old value.
- Check that both values are interpolable.
autoandnoneusually are not. - Ask whether the element was rendered before the change. If it was just inserted or just shown, no transition will run, and this is the case
@starting-stylehandles.
Summary of the model
- A transition compares the before-change style with the after-change style during a style update.
- The
transition-*values come from the after-change style. - Changes made between two style updates collapse into one comparison.
- An element with no before-change style, because it is newly rendered or was
display: none, does not transition.
Exercise
A .toast element has opacity: 0; transition: opacity 0.5s;. JavaScript creates it, appends it to the page, and in the same function adds the class show, which sets opacity: 1. The toast appears instantly at full opacity. Explain why, using the terms before-change style and style update.
Show answer
The element is appended and given show before any style update runs, so its first style update already computes opacity: 1. A newly rendered element has no before-change style, so there is no earlier value to interpolate from, and the 0 value was never computed. The transition therefore never starts. Forcing a style update between insertion and class change would work, and @starting-style provides the same starting value without that trick.
Exercise
A button has transition: background-color 0.3s declared only in its :hover rule. Describe what the user sees when hovering and when moving away, and give the fix to animate both directions.
Show answer
Entering hover animates over 0.3s, because the after-change style (the hover rule) has the transition. Leaving hover snaps back immediately, because the after-change style is the base rule, which has no transition. Move the transition declaration into the base rule, so it applies in both directions.