Sub topic 4
Fading in elements added with JavaScript
This chapter assumes you know CSS transitions (transition-property, duration, timing function), the cascade, and basic DOM scripting with createElement and append. The previous chapters explained that an element needs a before-change style to transition, and that @starting-style supplies one. Here you apply that to elements that a script inserts into the page.
The old problem with inserted elements
Before @starting-style, a freshly inserted element never animated. The browser had no earlier style for it, so it drew the final values straight away. The usual workaround was to insert the element, force a style recalculation (for example by reading offsetHeight), and then add a class in a requestAnimationFrame callback. That works but is fragile timing code.
The CSS: declare the starting point
Put the final look in the main rule and the starting look in a nested @starting-style block:
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
@starting-style {
opacity: 0;
transform: translateY(1rem);
}
}When the browser computes the first style for a .card, it uses the @starting-style values as the before-change style. The main rule then supplies the after-change style, so the transition runs from transparent and shifted down to opaque and in place.
The JavaScript: just insert the element
No class toggling or timing tricks are needed:
<button id="add">Add card</button>
<div id="list"></div>
<script>
document.getElementById('add').addEventListener('click', () => {
const card = document.createElement('div');
card.className = 'card';
card.textContent = 'New card';
document.getElementById('list').append(card);
});
</script>Each click appends a card. The card's first style update happens after insertion, and the starting style applies at that moment. Each new card fades and slides in on its own.
Where the starting style applies
@starting-style is used only on the first style update of an element. That covers two cases:
- The element has just been inserted into the DOM.
- The element changed from
display: noneto a displayed value.
After that first update, the block is ignored. If you later change opacity on the card, the transition runs between ordinary computed values, not the starting values.
Seeing the effect work
Use a slower duration while you experiment, such as 2s. Open DevTools, click the button, and inspect the new card. You can see the transition run in the Animations panel, and the card's computed opacity moves from 0 to 1 over time.
Limits to remember
- Only entry is animated. Calling
card.remove()deletes the element at once, so nothing plays. Animating an exit needs thedisplaytechniques covered in later chapters. - Inserting is not enough if the element is hidden. If a parent has
display: none, the card is not rendered, so its first style update waits until the parent is shown. - Support is recent. Older browsers ignore the block and show the card without animation. That is a safe fallback, because the main rule still gives the correct final look.
Exercise
You append a .card with JavaScript. Its CSS has transition: opacity 0.4s and a nested @starting-style { opacity: 0 }. Later, a script sets card.style.opacity = '0.5'. Does the starting style affect that second change? Why or why not?
Show answer
No. The starting style is used only as the before-change style on the element's first style update. The later change to 0.5 transitions from the card's current computed opacity (1) to 0.5 over 0.4s, because opacity is listed in transition.
Exercise
A colleague's cards fade in, but the translateY(1rem) slide does not animate. The CSS has the starting style for both opacity and transform. What should you check first?
Show answer
Check the transition declaration. It must include transform (or all). If only opacity is listed, the transform change is applied instantly.