Sub topic 3
Giving an element a starting point with @starting-style
A newly rendered element has no earlier style to transition from, so it appears instantly. The @starting-style at-rule lets you declare that earlier style yourself. This chapter covers the two ways to write it, the rules the browser follows when it uses it, and the mistakes that stop it from working.
This chapter assumes you know the following:
- CSS selectors and the cascade (specificity and source order).
- How
transition-property,transition-durationand the timing function work. - Basic HTML and JavaScript for adding elements and toggling classes.
The problem: no before-change style
A transition runs when the browser compares two computed styles for the same element. The first is the before-change style, the element's computed values at the previous style update. The second is the after-change style, the values it has now. If a property differs and a transition is declared for it, the browser interpolates between the two.
A newly rendered element has no previous style update. The same is true of an element that was display: none and is now shown, because elements with display: none have no computed style to compare against. In both cases there is no before-change style, so nothing transitions. This code does not animate on page load:
.card {
opacity: 1;
transition: opacity 0.4s ease;
}The card is created with opacity: 1 and simply appears.
What @starting-style does
@starting-style supplies the missing before-change values. On an element's first style update, the browser uses the declarations inside @starting-style as the before-change style. It then transitions to the values from the normal rules.
You can write it nested inside a style rule:
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
@starting-style {
opacity: 0;
transform: translateY(1rem);
}
}The declarations inside the nested block apply to the same element the parent rule selects. The block needs no selector of its own.
You can also write it as a top-level block that contains ordinary rules with selectors:
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
}
@starting-style {
.card {
opacity: 0;
transform: translateY(1rem);
}
}Both forms do the same thing. The nested form keeps the starting values next to the final values, which makes them easy to compare. The top-level form works in plain CSS that does not use nesting, and it is useful when the selector is long.
How the browser resolves it
Two cases count as "no previous style": the element's first style update, and the first update after it changes from display: none to a displayed value. The second case matters for show and hide patterns, which a later chapter covers.
Rules to remember
- It applies only once per appearance. After the first style update,
@starting-styleis ignored. Later changes, such as a hover or a class toggle on a visible element, use the previous computed style as normal. - List only the properties you want to animate from. A property that appears in the starting style but has the same value in the main rule produces no visible change. A property that is missing from the starting style starts at its final value and does not transition.
- Declare the transition in the main rule. The
transitionproperty is read from the after-change style. Putting it in the starting style does nothing useful. - The property must be animatable.
opacity,transform,background-colorand similar properties interpolate. Discrete properties such asdisplayneed extra work, which a later chapter covers. - Starting styles join the cascade. They follow the same specificity and source-order rules as other declarations, and the next section shows how this goes wrong.
Cascade order: where it goes wrong
The block @starting-style adds no specificity of its own. A rule inside it competes with your normal rules. When the browser builds the starting style, it applies the normal rules first, then the matching starting-style rules, and picks winners in the usual way.
This top-level version fails:
@starting-style {
.card {
opacity: 0;
}
}
.card {
opacity: 1;
transition: opacity 0.4s ease;
}Both selectors have the same specificity, and the normal .card rule comes later. It wins, so the starting opacity is also 1 and nothing animates. The fix is to move the @starting-style block after the main rule, or to raise the specificity of the selector inside it.
The nested form avoids the problem in most cases. The nested block sits inside the parent rule, after the parent's own declarations, and it has the same specificity as the parent selector. It therefore wins over the parent's declarations for the starting values. A different, more specific rule elsewhere can still override your starting values, so check for that if the animation does not run.
A complete example
This page fades and slides each card in. The first card is in the HTML, so it animates on page load. The button adds more cards with JavaScript, and each of those animates as it is inserted.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>@starting-style demo</title>
<style>
.card {
padding: 1rem;
margin: 0.5rem 0;
background: #eef;
border-radius: 0.5rem;
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
@starting-style {
opacity: 0;
transform: translateY(1rem);
}
}
</style>
</head>
<body>
<button id="add">Add card</button>
<div id="list">
<div class="card">First card</div>
</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>
</body>
</html>No JavaScript timing code is needed. Before this feature, developers inserted the element, forced a reflow or waited for requestAnimationFrame, and only then added a class that carried the final values. @starting-style replaces that workaround. A later chapter looks at DOM insertion in more detail.
Nested versus top-level
| Aspect | Nested inside a rule | Top-level block |
|---|---|---|
| Needs CSS nesting support | Yes | No |
| Selector inside the block | None, uses the parent's | Written out in full |
| Cascade position | After the parent's declarations | At its place in the stylesheet |
| Typical use | Starting values next to final values | Pseudo-elements, popovers and dialogs, shared rules |
| Common mistake | Overridden by a more specific rule elsewhere | Placed before the main rule |
Common errors
- Nothing animates on load. Check that the main rule has a
transitionfor the same property, and that the starting value differs from the final value. - It animates only once. That is expected.
@starting-styleruns only when the element first renders or leavesdisplay: none. A visible element that changes class uses ordinary transitions. - The starting block is ignored. Look for the source-order problem described above, or for a more specific selector that sets the same property.
- Only some properties animate. A property missing from the starting block has no before-change value, so it does not transition. Add it to the block with a different starting value.
- The element jumps instead of animating. The transition in the main rule may not cover the property.
transition: opacitydoes not covertransform.
Browser support and fallback
@starting-style is supported in current versions of Chrome, Edge, Safari and Firefox, but check a compatibility table for the versions you target. A browser that does not support it ignores the whole block. The element then appears instantly with its final values, which is a safe fallback because the content is still visible and correct. If the nested form is unsupported, the browser may drop the surrounding rule as well, so check that your target browsers support CSS nesting.
Exercise
This stylesheet is meant to fade a .note in when it is inserted, but it does not animate. Name the cause and give one fix.
@starting-style {
.note { opacity: 0; }
}
.note {
opacity: 1;
transition: opacity 0.3s;
}
Show answer
The two rules have equal specificity, and the main .note rule comes later in the source. It overrides the starting value, so the before-change opacity is also 1. Fix it by moving the @starting-style block after the .note rule, by nesting the block inside .note, or by giving the selector inside the block higher specificity.
Exercise
A visible panel has the nested @starting-style { opacity: 0 } and a transition on opacity. A script adds a class that changes its opacity from 1 to 0.5. Does the starting style take part in that change?
Show answer
No. The panel already has a previous computed style, so that style is the before-change style. The starting style applies only on the element's first style update or when it changes from display: none. The opacity change uses the normal transition from 1 to 0.5.