Sub topic 5
Nesting, specificity and source order
This chapter assumes you know how the cascade picks a winner (origin, layers, specificity, source order). It also assumes you have seen CSS nesting syntax. You should know the previous chapters' main point. An element's first style update has no before-change style, so @starting-style supplies one.
Most "my entry animation does nothing" bugs are not about transitions. They are cascade bugs, because the starting values lose a fight with the normal values.
How starting styles join the cascade
@starting-style does not create a separate stylesheet or a separate priority tier. The browser runs the ordinary cascade twice for an element's first style update:
- Normal pass: it uses only the regular rules. This produces the after-change style, the values the element ends up with.
- Starting pass: it uses the regular rules and the rules inside
@starting-style. This produces the before-change style, the values the transition starts from.
In the starting pass, the rules inside @starting-style compete with every other rule using the usual criteria. If a regular declaration beats a starting declaration for the same property, the starting value is discarded. The before-change style then equals the after-change style, so there is nothing to animate and no transition runs.
Two consequences follow:
- Only properties that the cascade lets the starting rule win will animate.
- After the first style update,
@starting-stylerules are ignored. They never affect later state changes such as hover or class toggles.
Two ways to write the same thing
You can write @starting-style as a top-level block that contains ordinary rules, or nest it inside a style rule.
/* Top-level form */
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
}
@starting-style {
.card {
opacity: 0;
transform: translateY(1rem);
}
}/* Nested form */
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
@starting-style {
opacity: 0;
transform: translateY(1rem);
}
}These two forms are equivalent. In the nested form the declarations inside @starting-style apply to the parent selector, as if the selector were &. The nested form keeps the entry values next to the rule they belong to, and it removes the chance of typing a different selector in the second place. That second point matters for the next section.
| Form | Selector inside `@starting-style` | Where it lives |
|---|---|---|
| Top-level | You write it out (`.card`) | After or beside the main rule |
| Nested | Implicit, the parent selector | Inside the main rule |
Specificity: the at-rule adds none
Wrapping a rule in @starting-style adds no specificity. The rule inside keeps the specificity of its own selector. A starting rule therefore needs a selector at least as specific as the regular rule that sets the same property.
This example does not fade:
.list .card { /* specificity (0,2,0) */
opacity: 1;
transition: opacity 0.4s ease;
}
@starting-style {
.card { /* specificity (0,1,0): loses */
opacity: 0;
}
}In the starting pass, .list .card { opacity: 1 } beats .card { opacity: 0 }. The before-change opacity is 1, which matches the final value, so the element simply appears.
To fix it, match the selector, or nest the block so the match is automatic:
.list .card {
opacity: 1;
transition: opacity 0.4s ease;
@starting-style {
opacity: 0;
}
}The popover example from earlier in the tutorial shows the same rule at work. The open state is [popover]:popover-open, which has specificity (0,2,0). The starting rule must use that same selector. If it used only [popover] (0,1,0), the open-state opacity: 1 would win the starting pass and the entry would not animate.
[popover]:popover-open {
opacity: 1;
}
@starting-style {
[popover]:popover-open { /* same (0,2,0) as the rule above */
opacity: 0;
}
}Source order breaks ties
When the specificity is equal, source order decides, as with any other declarations. The later declaration wins. Put the starting values after the regular values they must override.
At the top level, this means writing the @starting-style block after the main rule. In the nested form, it means writing the nested block after the regular declarations. A declaration placed after the nested block can override the starting value:
.card {
transition: opacity 0.4s ease;
@starting-style {
opacity: 0;
}
opacity: 1; /* later, same specificity: wins, so no fade */
}The nested block is fine in the earlier example because it comes last. Keep the habit: regular declarations first, @starting-style last.
Cascade layers
Layers sit above specificity in the cascade, so they can defeat a starting style that has plenty of specificity. A rule in a later layer beats a rule in an earlier layer, whatever their selectors. Unlayered rules beat layered ones.
@layer base, components;
@layer base {
.card {
@starting-style {
opacity: 0; /* earlier layer: loses */
}
}
}
@layer components {
.card {
opacity: 1;
transition: opacity 0.4s ease;
}
}The starting rule lives in base, but the regular opacity: 1 lives in components, which comes later. The regular rule wins the starting pass and the card does not fade. Keep an element's starting values in the same layer as its regular values, or in a later layer.
Nested selector lists and `:is()`
When you nest a rule inside a rule that has a selector list, the parent selector behaves like :is(). The specificity of :is() is that of its most specific argument. That applies to both items in the list:
.card,
#pinned {
opacity: 1;
transition: opacity 0.4s ease;
@starting-style {
opacity: 0;
}
}The nested starting rule has the specificity of #pinned (1,0,0), even when it applies to a plain .card. This is usually harmless, because the starting rule gets high specificity along with the regular rule. It becomes a problem if another rule elsewhere sets opacity for .card.featured (0,2,0) and expects its own starting values to win. The :is() rule beats it. When specificity gets confusing, split the list into separate rules.
A quick order of checks
When a starting style seems ignored, work down this list:
- Does the regular rule set a
transitionfor the same property? Without it, there is nothing to animate. - Does a regular declaration for that property sit in a later layer, or in an unlayered rule when yours is layered?
- Does the regular selector have higher specificity than the selector in
@starting-style? - At equal specificity, does the regular declaration come later in the source?
- Is a nested selector list inflating specificity through
:is()?
A complete page to test with
This page uses the nested form and runs as shown. Open it, click the button, and each new card fades and slides in. To see the specificity bug, change .list .card in the CSS to .card and the nested block's parent stays the same. The cards still animate. Then move the opacity: 1 below the nested block and they stop animating.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Starting style cascade demo</title>
<style>
.list .card {
padding: 1rem;
margin-block: 0.5rem;
background: #eef;
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 class="list" 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>
</body>
</html>Exercise
This rule is meant to fade .toast in when it is inserted, but the toast just appears. Name the cause and give a fix.
#stack .toast { opacity: 1; transition: opacity 0.3s; }
@starting-style { .toast { opacity: 0; } }
Show answer
The regular selector #stack .toast has specificity (1,1,0). The starting selector .toast has (0,1,0). @starting-style adds no specificity, so the regular opacity: 1 wins the starting pass and the before-change opacity equals the final value. Fix it by nesting the block inside #stack .toast { ... }, or by using #stack .toast as the selector inside @starting-style.
Exercise
A card has opacity: 1 in a rule in @layer components, and its @starting-style { opacity: 0 } is in @layer base, declared earlier in @layer base, components;. Does the card fade in? Why, and how do you change it?
Show answer
No. The later layer components beats the earlier layer base, regardless of specificity, so the regular opacity: 1 wins the starting pass and nothing animates. Move the @starting-style block into components (ideally nested in the same rule), or into a layer declared after it.