Sub topic 5
Nesting, specificity and source order
This chapter assumes you know CSS selectors and the cascade, how transition works, and the basic @starting-style rule from earlier chapters. Here you will learn how starting styles take part in the cascade, and why a starting style sometimes seems to be ignored.
A starting style is an ordinary declaration
@starting-style does not have its own priority system. Declarations inside it compete in the normal cascade, using the same rules as any other declaration: origin and importance, then specificity, then source order.
One detail matters most. The rules inside @starting-style add no specificity of their own. The at-rule itself has no weight. Only the selector inside it counts. A nested @starting-style uses the specificity of its parent rule's selector.
When the browser computes the before-change style for a first render, it takes the normal computed style and then applies the starting-style declarations on top. Those declarations must win the cascade among the rules that match the element. If a normal rule beats them, the starting value is replaced and no transition runs.
Nested and top-level forms
The two forms below are equivalent.
/* Nested form */
.card {
opacity: 1;
transition: opacity 0.4s ease;
@starting-style {
opacity: 0;
}
}
/* Top-level form */
.card {
opacity: 1;
transition: opacity 0.4s ease;
}
@starting-style {
.card {
opacity: 0;
}
}The nested form keeps the starting values next to the values they animate from. The top-level form lets you group many starting styles in one place. In both forms, the selector's specificity is .card, which is (0,1,0).
Specificity conflicts
A common mistake is to write a starting style with a lower-specificity selector than the normal rule.
.list .card {
opacity: 1;
transition: opacity 0.4s;
}
@starting-style {
.card {
opacity: 0;
}
}The normal rule has specificity (0,2,0). The starting rule has (0,1,0). The normal rule wins, so the starting value of 0 is overridden by 1. The element is already at its final value in the before-change style, and nothing animates.
The fix is to match the selector. Write the starting style with the same or higher specificity.
@starting-style {
.list .card {
opacity: 0;
}
}Nesting avoids this problem, because the starting style inherits the selector of its parent rule.
Source order
When two selectors have equal specificity, the later one wins. This applies to starting styles too. A later normal rule can override an earlier starting style.
@starting-style {
.card {
opacity: 0;
}
}
.card {
opacity: 1;
}Here the normal rule comes after the starting style and has equal specificity. It wins, so no transition runs. Put the @starting-style block after the normal rule, or nest it inside the rule, to keep the order safe.
.card {
opacity: 1;
}
@starting-style {
.card {
opacity: 0;
}
}Comparing the failure modes
| Cause | Symptom | Fix |
|---|---|---|
| Starting selector has lower specificity | Element appears at its final style with no transition | Match or raise the selector's specificity, or nest the block |
| Normal rule comes later with equal specificity | Same as above | Move `@starting-style` after the normal rule, or nest it |
| Starting style lacks the transitioned property in `transition` | Value jumps | Add the property to `transition-property` |
| Starting style sets a property the normal rule never sets | Transition starts from the starting value to the initial value | Declare the final value explicitly in the normal rule |
How the cascade resolves the before-change style
Checking the result
In DevTools, select the element and look at the Styles pane. If a normal rule shows the starting declaration crossed out, the cascade is overriding it. Compare the two selectors and their order.
Exercise
A .list .card rule sets opacity: 1 and the transition. A separate @starting-style { .card { opacity: 0; } } follows it, yet the card appears without fading. Why does it fail, and what is the simplest fix?
Show answer
The normal rule has specificity (0,2,0) and the starting rule has (0,1,0), so the normal value of 1 wins and the before-change style equals the final style. The simplest fix is to nest @starting-style { opacity: 0; } inside the .list .card rule, so it uses the same selector.
Exercise
Why can placing a @starting-style { .card { opacity: 0; } } block before .card { opacity: 1; } stop the entry transition?
Show answer
Both selectors have equal specificity, so the later rule wins. The normal rule comes last, so it overrides the starting value, and the before-change style already equals the final style.