Sub topic 3
Giving an element a starting point with @starting-style
This chapter assumes you know how CSS transitions work (transition-property, duration, timing function) and how the cascade picks between competing rules. The previous chapter explained that an element has no earlier style to change from when it first renders, so nothing animates. Here you supply that earlier style.
What @starting-style does
@starting-style is an at-rule that declares the values an element should start from the first time it is styled. The browser uses them as the before-change style for that first update. It then transitions from those values to the element's normal computed values.
You can write it as a top-level block that contains ordinary rules:
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
}
@starting-style {
.card {
opacity: 0;
transform: translateY(1rem);
}
}You can also nest it inside the rule it belongs to:
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
@starting-style {
opacity: 0;
transform: translateY(1rem);
}
}Both versions do the same thing. When a .card is first rendered, it starts at opacity: 0 and 1rem below its final position. It then fades and slides into the values in the main rule. The nested form keeps the entry state next to the final state, so it is usually easier to maintain.
How the browser applies it
The starting values apply only on an element's first style update. That means when it is first rendered, or when it switches from display: none to a displayed value. After that update the starting style is ignored, and later changes use the normal before-change style.
Two details follow from this:
- The
transitionproperty must be set on the element's normal style, not only inside@starting-style. Transitions are driven by the after-change style, so atransitiondeclared only in the starting block will not be used. - Only properties that differ between the starting style and the main style animate. If a property appears in the starting block but has the same value in the main rule, nothing visible happens for it.
Properties you do not repeat
You only list the properties you want to animate from. Everything else uses its normal value. A starting style that sets only opacity: 0 leaves transform and the rest unchanged.
.toast {
opacity: 1;
transition: opacity 0.25s;
@starting-style {
opacity: 0;
}
}The starting style is not a replacement for the main rule. It only overrides it for that one first update.
A common mistake
A frequent error is writing the starting values in the main rule and the final values in @starting-style. That reverses the effect. The main rule describes where the element ends up. The starting block describes where it begins.
/* Wrong: the element animates away from its resting state */
.badge {
opacity: 0;
transition: opacity 0.3s;
@starting-style {
opacity: 1;
}
}In this version the badge would begin visible and fade to invisible, which is the opposite of an entry animation. Swap the values so that the main rule holds opacity: 1 and the starting block holds opacity: 0.
Checking your work
You can test a starting style without any JavaScript. Reload the page, and any element with a starting style that is visible on load should animate in. If it appears instantly, check that the main rule has a transition for the property you are animating, and that the starting values differ from the final ones.
Exercise
A .banner rule sets opacity: 1, but transition: opacity 0.5s appears only inside its nested @starting-style block, which sets opacity: 0. The banner appears instantly on page load. Why, and how do you fix it?
Show answer
Transitions are driven by the element's normal (after-change) style, so a transition declared only inside @starting-style is not used. Move transition: opacity 0.5s into the main .banner rule and keep opacity: 0 in the starting block.