You can transition z-index
You can animate z-index. That sounds a little odd because there is nothing visual between one stacking level and the next, but it can still be useful when an element changes size or position at the same time.
Here is the smallest version:
@keyframes move {
from {
z-index: 0;
transform: scale(1);
}
to {
z-index: 4;
transform: scale(2.5);
}
}
The scale changes smoothly. z-index, however, is an integer, so the browser moves through whole stacking values rather than blending between them.
That distinction matters. Use the animation when changing the stacking order at a particular point helps the rest of the effect. Do not expect z-index itself to look animated—it is doing useful work behind the scenes.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- Making Links Easy to Recognize
Make links recognisable with underlines, clear focus states, useful text, and behaviour that does not surprise the reader.
- When CSS Layout Order Stops Matching the HTML
Keep Grid and Flexbox layouts aligned with the HTML order so keyboard focus and screen reader reading do not jump around the page.
- Keyboard Focus Styles With :focus-visible
Keep a clear keyboard focus indicator while avoiding the same focus ring after every pointer interaction.