Fill mode
Also called animation-fill-mode or fill-mode forwards
A CSS setting that decides which styles an element shows before a delayed animation starts and after the animation ends.
import { useLayoutEffect, useRef } from 'react';
const features = [
{ title: 'Live preview', text: 'See every change the moment it lands.' },
{ title: 'One-click publish', text: 'Put your app online with a link.' },
{ title: 'Built-in database', text: 'Save data without running a server.' }
];
export function FeatureGrid() {
const listRef = useRef<HTMLUListElement>(null);
// A layout effect starts the animations before the first paint.
useLayoutEffect(() => {
if (matchMedia('(prefers-reduced-motion: reduce)').matches) return;
const cards = Array.from(listRef.current?.children ?? []);
const animations = cards.map((card, index) =>
card.animate(
[
{ opacity: 0, transform: 'translateY(16px)' },
{ opacity: 1, transform: 'translateY(0)' }
],
// 'both': show the first keyframe during the delay (backwards)
// and keep the last one after the animation ends (forwards).
{ duration: 500, delay: index * 100, easing: 'ease-out', fill: 'both' }
)
);
return () => animations.forEach((animation) => animation.cancel());
}, []);
return (
<ul ref={listRef} className='grid gap-4 sm:grid-cols-3'>
{features.map((feature) => (
<li key={feature.title} className='rounded-xl border bg-background p-5'>
<h3 className='font-semibold'>{feature.title}</h3>
<p className='mt-1 text-sm text-muted-foreground'>{feature.text}</p>
</li>
))}
</ul>
);
}Fade each feature card in and slide it up from 16px below over 500ms with ease-out, delaying each card 100ms more than the one before. Use animation-fill-mode: both so the cards stay hidden during their delay and stay in place after the animation ends.
Paste into Layerwise or any AI coding tool.
Fill mode explained
By default, a CSS animation only affects an element while it is running. During an animation-delay the element shows its normal styles, and when the animation ends it snaps back to them. animation-fill-mode changes that.
forwards keeps the styles of the last keyframe after the animation ends. backwards applies the first keyframe during the delay, so a delayed fade-in stays hidden instead of showing and then vanishing. both does both, and it is the usual choice for delayed entrance animations.
With forwards, the finished animation keeps control of the properties it animated, so changing them later in normal CSS appears to do nothing. For a lasting state change, set the final style on the element and animate toward it. The Web Animations API calls the same setting fill.
Related terms
All termsDelay
The wait between the moment an animation is triggered and the moment it starts moving, used to sequence motion or ignore brief triggers.
Keyframes
Snapshots that define how an element looks at set points in an animation. The browser fills in the motion between them.
Fade in
An entrance animation where an element goes from fully transparent to fully visible, so it appears gently instead of popping in.
Stagger
Animating a group of items one after another with a small delay between each, so they arrive as a cascade.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.