日本語

Fade in list items added with @starting-style in CSS

​​CSS​Accessibility,HTML,JavaScript

When you add an <li> with JavaScript, the item appears instantly. If you write the "value before display" in CSS @starting-style (for example, opacity: 0), the added item transitions from those values into view. This works for adding an event to a to-do list, showing a new chat message, or adding one entry to a notification list. On the JavaScript side, you only need to add the element.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I read whether a transition started right after adding an item, and the opacity value, using getAnimations() and getComputedStyle(). I did not test Firefox, Safari, real smartphones, or using this inside UI libraries such as React.

A video of pressing "Add item" three times and then "Delete last item" once. Each press shows a count in the top bar, such as "Operation 1 of 4" (the label is in Japanese). A new item rises slightly from below and becomes more opaque, while the opacity value in the field below (shown in pink) goes from 0 to 1. On the fourth press, the last item fades out and disappears, and a "removed from DOM" message appears (the label is in Japanese).
Adding and removing items. To get evenly spaced frames, I stopped transitions and advanced time while recording, then played the result at half speed. The top bar, the cursor circle, and the opacity display below were added for the recording.

Writing the pre-display value on li with @starting-style

The test page had a button that adds one <li> to the end of a <ul> each time it is pressed.

html
<button id="add" type="button">項目を追加</button>
<ul id="list"><li>最初からある項目</li></ul>
<script>
  let n = 0;
  const list = document.getElementById('list');
  function addItem() {
    n += 1;
    const li = document.createElement('li');
    li.textContent = '追加した項目 ' + n;
    list.append(li);
    return li;
  }
  document.getElementById('add').addEventListener('click', addItem);
</script>

In CSS, you add a transition to li and nest @starting-style inside it to write the value before display.

css
li {
  transition: opacity 0.4s ease-out, translate 0.4s ease-out;

  @starting-style {
    opacity: 0;
    translate: 0 8px;
  }
}

The values right after adding an item, and later, were as follows. When I removed only the @starting-style part, no transition started at all, and the item was displayed at opacity: 1 from the start.

CSS Transitions started right after adding 0ms 100ms 200ms 300ms 500ms
With @starting-style opacity, translate 0.00 0.43 0.73 0.93 1.00
Without @starting-style None 1.00 1.00 1.00 1.00 1.00

(The numbers are opacity values. With @starting-style, translate also decreased from 8px toward 0.)

The MDN page for @starting-style says that, to avoid unexpected behavior, CSS transitions do not run when the initial style of an element is updated. An element just added to the DOM has no "value before the change," so no transition starts. @starting-style provides that "value before the change" instead.

When I added three items together in the same process, all three transitions started, and the opacity after 100ms was 0.38 for each.

Standalone @starting-style must come after the original rule

@starting-style can also be written as a standalone block instead of being nested. In that case, pay attention to the order.

css
li {
  opacity: 1;
  transition: opacity 0.4s ease-out, translate 0.4s ease-out;
}

/* Write this after the li rule */
@starting-style {
  li {
    opacity: 0;
    translate: 0 8px;
  }
}

With this order, transitions started just as they did with nesting. When I moved the @starting-style block before the li rule (the one that sets opacity: 1; translate: 0 0;), no transition started, and the item was at opacity: 1 from the moment it was added.

MDN says that @starting-style and the original rule have the same specificity, so @starting-style should be written after the original rule. The CSS Transitions Level 2 specification also says that rules inside @starting-style do not always win over outer rules. If you nest the rule, you do not have to worry about this order.

Items present when the page loads also animate, so apply the rule only to added items

If you apply @starting-style to all li elements, items written in the HTML from the start also fade in when the page opens. In my test, the "initial item" right after the page finished load had opacity and translate transitions running, and its opacity was 0. The specification also says that @starting-style is used for elements that were not displayed when the style was last computed, or that were not in the DOM. Elements right after the page opens fall under this as well.

If you want to animate only the added items, add a class in JavaScript when you add them, and write @starting-style only under that class.

css
li {
  transition: opacity 0.4s ease-out, translate 0.4s ease-out;
}

li.added {
  @starting-style {
    opacity: 0;
    translate: 0 8px;
  }
}
js
const li = document.createElement('li');
li.textContent = '追加した項目';
li.classList.add('added');
list.append(li);

With this approach, the "initial item" right after the page opened had no transition, and its opacity was 1. An item added with the added class started its transition from opacity: 0, as before.

Do not add transitions for people who reduce motion

For people who have set their OS to reduce motion, it is kinder to show items without animation. Put only the transition inside prefers-reduced-motion: no-preference.

css
li {
  @starting-style {
    opacity: 0;
    translate: 0 8px;
  }
}

@media (prefers-reduced-motion: no-preference) {
  li {
    transition: opacity 0.4s ease-out, translate 0.4s ease-out;
  }
}

When I opened the page in a browser with the reduced-motion setting (reducedMotion: 'reduce' in Playwright), the added item had no transition and was at opacity: 1 from the start. The @starting-style values are used only as the starting values of a transition, so without a transition the item never becomes transparent. Without this setting, transitions started just as they did when @starting-style was nested inside li.

When removing, wait for the transition to finish before calling remove()

@starting-style only takes effect when an element is displayed. When I called li.remove(), the item was removed from the DOM immediately, and no transition started. To animate removal, first add a class that sets the "after removal" look, and call remove() after the transition finishes.

css
li.leaving {
  opacity: 0;
  translate: 0 8px;
}

One way to know when a transition ends is the transitionend event. However, a problem occurred when I combined this with the reduced-motion setting from the previous section.

js
function removeItem(li) {
  li.classList.add('leaving');
  li.addEventListener('transitionend', () => li.remove(), { once: true });
}

With this code, the item was removed from the DOM after 401ms in the normal case. However, in the browser with the reduced-motion setting, no transition started, so transitionend never fired, and the item was still in the DOM after 2000ms. Its opacity was 0, so it was invisible, but it still occupied space in the list.

You can handle both cases by using getAnimations() to get the transitions running on the element and waiting for each one's finished promise.

js
function removeItem(li) {
  li.classList.add('leaving');
  Promise.all(li.getAnimations().map((a) => a.finished)).then(() => li.remove());
}
Approach Without reduced-motion setting With reduced-motion setting
Wait for transitionend Removed after 401ms Still present after 2000ms
Wait for finished from getAnimations() Removed after 406ms Removed after 1ms

When there is no transition, getAnimations() returns an empty array, so Promise.all() finishes immediately and the item is removed right away.

I also checked removing an item while it was still appearing. When I called removeItem() 100ms after adding (when opacity was 0.32), the appearing transition stopped, switched to the removal transition, and the item was removed from the DOM after 117ms. The finished promise never threw an error. The MDN example adds a class and removes the element after waiting 1000ms with setTimeout(). With the finished approach, even if you remove an item while it is still appearing, the item is removed when the removal transition ends.

Point

Use @starting-style for the appearing animation, and use a class and a transition for the disappearing animation. When removing, wait for the finished promises from getAnimations() before calling remove(), so that no transparent items are left on screen for people who reduce motion.

Moving an item by reordering makes it fade in again

When I moved an item that had already finished appearing to the top with list.prepend(li), a transition started on it again, and its opacity went back to 0. MDN's description of moveBefore() says that moving an element with prepend() or insertBefore() resets its state each time it is moved.

If you want to avoid flicker when reordering, move the item with moveBefore().

js
list.moveBefore(li, list.firstElementChild);

For an item moved to the top with moveBefore(), no transition started, and its opacity stayed at 1. The same MDN page says that moveBefore() moves an element without removing it, so its animation and transition state is kept after the move. However, Safari does not support moveBefore() (see the table in the next section).

Supported browsers

Desktop browser support versions in @mdn/browser-compat-data (8.1.8) are as follows.

Feature Chrome and Edge Firefox Safari
@starting-style 117 129 17.5
translate property 104 72 14.1
moveBefore() 133 144 Not supported

​Posted by 86