CSS Animation Example: Caption Slide-Up
The "Caption Slide-Up" is a classic interaction used in image galleries and article grids to keep a clean aesthetic until the user interacts for more context.
Ocean Voyage
Explore unseen territories.
To implement this, we pair position: absolute with overflow: hidden on the parent card. We position the caption securely at the bottom using bottom: 0, but then immediately shove it out of frame downwards by applying transform: translateY(100%). Since 100% in a translate function resolves relative to the element's own height, it tucks itself perfectly below the visual edge. On hover, we simply reset it to translateY(0) via a transition.
Here is the code to create the Caption Slide-Up animation.
Accessibility Considerations
Because sliding overlays constitute broad motion patterns, some users might find it uncomfortable. 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 by removing the transition duration for users who have this setting enabled on their device.
Without the transition, hovering instantly reveals the caption text seamlessly. Also, note the use of :focus-within to ensure keyword navigation (using the "Tab" key) triggers the hover effect properly.
Here's what we used in our example:
@media (prefers-reduced-motion: reduce) {
.caption {
/* Disables the slide effect, popping it into place instantly */
transition: none;
}
}