Sub topic 2
Why newly rendered elements do not animate
This chapter assumes you know CSS selectors and the cascade, and that you can write a basic transition with transition-property, a duration and a timing function. You should also know what display: none does. The chapter explains why an element that is new to the page, or that has just left display: none, shows up with no transition. The next chapter introduces the fix.
The before-change style
A CSS transition needs two values for a property: a start and an end. The browser takes the start from the element's before-change style. This is the computed style from the previous style update. It takes the end from the after-change style, the computed style after the current update.
If a property has a transition defined and its before-change and after-change values differ, the browser animates between them.
.box {
opacity: 1;
transition: opacity 0.4s ease;
}
.box:hover {
opacity: 0.5;
}When you hover, the style update finds the old computed value 1 and the new value 0.5. A transition runs between them.
The first style update has no before-change style
An element that has just been rendered has no previous computed style. The first style update is the first time the browser calculates its values. With no before-change style, there is nothing to start from. The element appears directly in its final state.
This happens in three common cases:
- The page loads and the element renders for the first time.
- JavaScript inserts a new element into the DOM.
- An element changes from
display: noneto a visible value.
The third case surprises people. An element with display: none has no computed style that the browser keeps for transitions. When display changes to block, the element starts rendering again, so the update counts as a first style update.
.panel {
opacity: 1;
transition: opacity 0.3s ease;
}
.panel.is-hidden {
display: none;
opacity: 0;
}Removing is-hidden sets opacity to 1, and the transition is declared. But the panel had no rendered style before the change, so it appears at once.
Why the old workarounds exist
Before @starting-style, developers forced a before-change style by hand. A typical approach was to insert the element, force the browser to calculate its style, then change a class on the next frame.
<button id="add">Add card</button>
<div id="list"></div>
<style>
.card {
opacity: 0;
transition: opacity 0.4s ease;
}
.card.visible {
opacity: 1;
}
</style>
<script>
document.getElementById('add').addEventListener('click', () => {
const card = document.createElement('div');
card.className = 'card';
card.textContent = 'New card';
document.getElementById('list').append(card);
requestAnimationFrame(() => {
requestAnimationFrame(() => card.classList.add('visible'));
});
});
</script>The script adds the card with opacity: 0. Then it waits two animation frames so the browser calculates that style first. Only then does it add visible. The card now has a before-change value of 0 and an after-change value of 1, so the transition runs.
This works, but it has costs:
- The timing depends on the browser. One frame is sometimes not enough, which is why the example uses two.
- The animation logic is split between CSS and JavaScript.
- Every component that needs an entry effect repeats the pattern.
A related trick is reading element.offsetHeight to force a style calculation. It also works, but it forces layout work on the page.
What the missing piece is
The problem is not that the transition is badly written. The browser has no starting value for the element's first render. The fix is to supply that value in CSS, so the element has a before-change style even though it has never been rendered. The @starting-style rule does this, and the next chapter covers it.
Exercise
A .toast element has transition: opacity 0.5s and opacity: 1. JavaScript creates it and appends it to the page. Why does it appear instantly instead of fading in, and what does the browser lack?
Show answer
The first style update after insertion is the first time the browser computes the toast's style. The element has no before-change style, so there is no starting opacity value to transition from. The browser applies opacity: 1 straight away.
Exercise
A .menu toggles between display: none and display: block when a class changes. It has a transition on opacity. Why does opening the menu not fade it in?
Show answer
An element with display: none is not rendered, so it has no computed style to use as a before-change style. When display changes to block, the update counts as a first style update, and the opacity transition has no starting value.