Sub topic 1
Recap: how transitions use the before-change style
Prerequisites
This chapter assumes you know CSS selectors and the cascade, and that you have written a basic transition with a property, duration and timing function. You should know what display: none does. You do not need to know @starting-style yet. This chapter prepares the ground for it.
The working model: two styles and a change
A CSS transition runs when the computed value of a property changes from one style update to the next. The browser compares two values:
- The before-change style is the element's computed style from the previous style update.
- The after-change style is the computed style after the new update.
If a property listed in transition-property has different values in the two styles, the browser interpolates between them over transition-duration. If the values are the same, nothing runs.
.button {
background-color: steelblue;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: tomato;
}When the pointer enters the button, the before-change style has steelblue and the after-change style has tomato. The browser blends the two colors over 0.3 seconds. When the pointer leaves, the roles swap and it blends back.
The transition declaration that counts is the one in the after-change style. That is why you can put a different transition on :hover than on the base rule, to get different timing in each direction.
The first decision in the diagram is the one this tutorial is about. If there is no earlier style, the browser has nothing to compare against.
What can be interpolated
The browser can only blend values that have intermediate steps. opacity, transform, color and width all do. Values such as display, which switches between keywords like none and block, have no in-between state. By default these properties flip at the start of the transition and never animate. A later chapter covers transition-behavior: allow-discrete, which changes that.
A worked example: toggling a class
Here is a panel that fades and slides when a class is toggled.
<button id="toggle">Toggle</button>
<div id="panel" class="panel">Hello</div>
<style>
.panel {
opacity: 1;
transform: translateY(0);
transition: opacity 0.3s ease, transform 0.3s ease;
}
.panel.is-away {
opacity: 0;
transform: translateY(1rem);
}
</style>
<script>
document.getElementById('toggle').addEventListener('click', () => {
document.getElementById('panel').classList.toggle('is-away');
});
</script>The panel exists and is rendered before the click. So when the class changes, the browser has a before-change style (opacity: 1) and an after-change style (opacity: 0). Both properties are in the transition list, so both animate. The panel stays in the layout the whole time, only invisible.
Interrupted transitions
If a second change arrives mid-transition, the browser starts a new transition from the current interpolated value toward the new target. It does not jump to the old end value first. Toggling the class twice quickly gives a smooth reversal rather than a flash.
Where this model falls short
The model needs a before-change style, which means the element must already have been rendered with a computed style. That fails in two common cases:
- An element is inserted into the DOM and rendered for the first time.
- An element changes from
display: noneto a visible display value. While it isdisplay: none, it has no computed style that counts as a previous one.
In both cases the first style the element gets is the after-change style, with nothing before it. No transition runs, and the element simply appears. The next chapter looks at this in detail, and the chapters after it show how @starting-style supplies the missing before-change values.
Exercise
A .card has opacity: 1 and transition: opacity 0.4s. JavaScript creates a new card and appends it to the page. Why does the card appear instantly instead of fading in, even though its CSS is fine?
Show answer
The card has never been rendered before, so there is no before-change style to compare with its first computed style. Without two differing styles, no transition is triggered, and the card appears at opacity: 1 immediately.
Exercise
In the toggle example, the user clicks twice within 100 ms. Does the panel jump to its end state before reversing?
Show answer
No. The second change starts a new transition from the panel's current interpolated value toward the new target, so the motion reverses smoothly from where it is.