CSS Animation Example: The Stacked Card Fan
The "Stacked Card Fan" injects a delightful micro-interaction to stacked UI elements like a gallery album cover or a deck of virtual flashcards.
This layout uses multiple identically sized cards stationed directly atop one another inside a position: relative container using position: absolute. A randomized initial transform: rotate() creates "messiness", indicating the pile has multiple items. We also use transform-origin: bottom center so the cards fan outwards from their base.
Upon hover, we give each individual card class a distinct transformation command. The top card lifts, while the other two swing outwards by rotating and translating in respective directions. A bouncy cubic-bezier() value for the timing-function completes the playful appearance.
Here is the code to create The Stacked Card Fan animation.
Accessibility Considerations
Rapid expanding layouts can be disorienting or distracting for some users. prefers-reduced-motion is a CSS media feature used to detect if a user has enabled a setting on their device to minimize the amount of non-essential animation or motion. We handle this effectively by removing the transition duration for users who have this setting enabled on their device.
Here's what we used in our example:
@media (prefers-reduced-motion: reduce) {
.fan-card {
/* Disables the animation swinging, snapping it open instantly */
transition: none;
}
}
In addition, making the container keyboard-focusable by adding tabindex="0" combined with a :focus-within or :focus state on our hover ruleset ensures that those who navigate using a keyboard instead of a mouse can still trigger the fan-out behavior.