CSS scroll fade-in: how @keyframes make it work

Contents
- Why a Scroll Fade-In Plays Without Any Video
- How to Check It: Building It Yourself with CSS Keyframes
- Where People Get Stuck
- The Takeaway
- Under the Hood: How Does the Browser Handle a Fade-In?
- Scroll Animation Trigger Comparison (Matrix)
- 5 Critical Mistakes People Make (and How to Prevent Them)
- A 5-Step, Stutter-Free Scroll Reveal Workflow
- 7 Tips for Polishing Your Animations
- Final Decision Tree: Which Kind of Scroll Animation Should You Choose?
- Key Terms Glossary and One-Line Cheat Sheet
- Frequently Asked Questions
You've probably seen websites where text or cards fade in as you scroll. The motion looks as smooth as a video, but there isn't a single video file playing behind it. The effect is just a few lines of CSS called @keyframes telling the browser, "change the opacity from 0 to 1."
Summary ① The fade-in effect you see on scroll isn't a video or GIF — it's an animation built with CSS @keyframes ② With @keyframes, you define only the start (from) and end (to) states, and the browser calculates and fills in the values in between ③ Details like duration, delay, and repeat are controlled by listing them in order in a single animation property
Why a Scroll Fade-In Plays Without Any Video
The first time you see this effect, it's easy to assume there's a playback file hidden somewhere. But when you inspect the element with your browser's developer tools, in most cases you'll find a plain div or p tag — not a video or img tag.
What moves the element is the CSS @keyframes rule. Just as the name suggests, you define only the "key frames," and the browser's rendering engine automatically calculates and draws every frame in between, dozens of times per second.
For a fade-in, for example, you just set opacity: 0 at the start (from) and opacity: 1 at the end (to). If the animation lasts 0.8 seconds, the browser smoothly interpolates between the two values over that time and plays it.
How to Check It: Building It Yourself with CSS Keyframes
Creating a fade-in takes just two steps. First, give the @keyframes rule a name and set opacity: 0 in the start state (from) and opacity: 1 in the end state (to). Then, in the animation property, list that name, the duration, the easing curve (timing-function), and the iteration count in order to attach it to the element.
One value people often miss here is animation-fill-mode. If you don't set it to both, you can get a flicker where the element briefly shows its original style before the animation starts, or after it ends. With both, the from state holds before the animation starts and the to state holds after it ends.
To play it only when scrolling, you need one more step. CSS alone can't tell whether an element has entered the screen, so you also use JavaScript such as IntersectionObserver to detect when it enters the viewport and add an animation class.
Try it with our tool. Click through the fade, slide, zoom, bounce, pulse, shake, and rotate presets, then dial in duration, delay, and easing curve with the sliders. You can copy the finished CSS, including the @keyframes and animation properties, as-is.
Free · no sign-up · generate CSS code right in your browser

Where People Get Stuck
| Symptom | Cause |
|---|---|
| The animation doesn't show at all | The name in the animation property doesn't match the @keyframes name, or it's misspelled |
| The element flickers briefly before the animation starts | animation-fill-mode isn't set to both, so the original style shows before the start |
| It plays as soon as the page loads, regardless of scrolling | CSS alone can't read the scroll position — you need to add a class with IntersectionObserver |
| It never stops repeating | animation-iteration-count is set to infinite, so it loops forever |
As the table shows, most problems come from leaving out a single value, like a name mismatch or a missing fill-mode. Starting from finished code generated from a preset cuts down on these mistakes.
The Takeaway
The fade-in you see on scroll isn't a video or GIF. It's a CSS animation where the browser automatically interpolates between the start and end states you define with @keyframes. Set the duration, easing curve, and iteration count in the animation property and set fill-mode to both, and the element holds the state you want with no flicker.
Instead of typing values into code one at a time and refreshing, you can match presets and sliders with a live preview and find the feel you want faster.
Free · no sign-up · generate CSS code right in your browser
Under the Hood: How Does the Browser Handle a Fade-In?
Behind a smooth fade-in animation lies the browser rendering engine's precise mathematical interpolation.
Keyframe Interpolation Pipeline
The browser builds a timeline from the animation's start point, t=0, to its end point, t=duration. When opacity: 0 changes to opacity: 1, the rendering engine doesn't touch the element's layout geometry at all — it calculates only the alpha channel independently on each frame (60Hz = 16.6ms or 120Hz = 8.3ms). This process is GPU-accelerated on the compositor thread, so the animation keeps going even if the main thread is blocked.
The Decisive Role of animation-fill-mode
If you leave this property out of an animation with a delay, your layout can fall apart.
- Default (
none): Keyframes apply only while the animation is playing. During the delay the original CSS state shows, and when it ends the element returns to that original state, causing severe flash or flicker. backwards: Before the animation starts, it forces the 0% keyframe styles (for example, opacity 0) onto the element duringanimation-delay.forwards: After the animation ends, it keeps the 100% keyframe styles permanently.both: Combines the behavior ofbackwardsandforwards, making it an essential setting for delayed scroll reveal effects.
IntersectionObserver + CSS Stagger Pattern
To avoid overloading scroll events, the modern web uses the observer pattern.
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
// Stop observing once it has appeared to free up memory
observer.unobserve(entry.target);
}
});
}, { threshold: 0.15 });
Scroll Animation Trigger Comparison (Matrix)
Here's a comparison table to help you pick the best rendering strategy for your goal.
| Approach | JavaScript overhead | Bundle size | Browser support | Hardware acceleration | Best use case |
|---|---|---|---|---|---|
Pure CSS Scroll-Driven (animation-timeline: view()) | None at all (0%) | 0KB | Chrome 115+ (limited in Safari/Firefox) | Best (GPU) | Targeting modern browsers, fully static sites with no JS |
| IntersectionObserver + CSS @keyframes | Very low | < 1KB | All browsers (no polyfill needed) | Best (GPU) | Typical blogs, marketing landing pages (most recommended) |
| Scroll Event Listener + JS Styles | Very high (jank risk) | 0KB | Down to legacy browsers | Low (main thread) | An anti-pattern to avoid at all costs |
| GSAP ScrollTrigger | Medium (well optimized) | About 60KB | All browsers | High | Complex pinning, timeline sequence animations |
| AOS Library | Low to medium | About 14KB | All browsers | High | When you want quick and easy scroll reveals |
As the table shows, combining IntersectionObserver with CSS @keyframes is the best practice right now — it gives you both browser compatibility and performance.
5 Critical Mistakes People Make (and How to Prevent Them)
These are the classic mistakes junior developers keep making without realizing it when they build scroll animations.
- Forgetting
animation-fill-mode: both: Leave this out on a card list with staggered delays, and the moment the page loads every card flashes at 100% opacity before disappearing. It's an ugly flicker. - Trying to transition
visibility: hiddenordisplay: none: Browsers can't interpolate between values of these two properties. That's why elements just pop into view. You need to animateopacityinstead. - Triggering the whole animation at page load: The animations on elements far down the page finish before the user ever scrolls them into the viewport, so by the time you scroll down, there's nothing left to see.
- Overusing heavy CSS scroll event listeners: If you use
window.addEventListener('scroll')without throttling or thepassive: trueflag, the frame rate on mobile devices tanks. - Skipping the accessibility media query: Omit
@media (prefers-reduced-motion: reduce)and you'll make users with vestibular disorders feel sick. Remove the movement and keep only a simple opacity change.
A 5-Step, Stutter-Free Scroll Reveal Workflow
- Step 1 (Build the initial HTML skeleton): Mark up the card elements you want to animate in a hidden state with
opacity: 0and a littletransform: translateY(20px). - Step 2 (Write the @keyframes): Declare a
fadeInUpkeyframe that goes fromfrom { opacity: 0; transform: translateY(20px); }toto { opacity: 1; transform: translateY(0); }. - Step 3 (Set the easing curve and mode): Hook up the
animationproperty and always pair it withanimation-fill-mode: bothand a naturalcubic-beziereasing function. - Step 4 (Attach a lightweight observer): Set up an
IntersectionObserverwith athresholdaround 0.1 ∼ 0.2 so thevisibleclass only toggles when the element enters the viewport. - Step 5 (Stagger with CSS variables): Add
style="--i: 1"to each list item's inline style, then useanimation-delay: calc(var(--i) * 0.1s);in CSS to create that cascading, waterfall-like delay.
7 Tips for Polishing Your Animations
These core tips come from years of hands-on UI motion design experience.
- Keep the displacement small: Hold your
translateYdistance to around 15 ∼ 30px. Too much movement looks tacky and pulls focus. Subtle motion feels premium. - Give it inertia with cubic-bezier: Instead of a default like
ease-out, usecubic-bezier(0.16, 1, 0.3, 1)(the editorial easing). It produces a stop that feels heavy yet smooth, like real-world physics. - Stagger with CSS custom properties (var): Don't hand-set every delay in JavaScript. Just define
style="--index: 1"in your markup and let CSS do the math — your code gets much cleaner. - Unobserve to free memory: A fade-in effect usually only needs to play once. Right after an element appears, call
observer.unobserve(entry.target)so the garbage collector can reclaim the memory. - Adopt progressive enhancement: For the latest Chrome, declare JavaScript-free CSS
animation-timeline: view()inside an@supportsquery to push performance to the limit. - Always provide a pure-opacity fallback: For accessibility, apply
transform: nonefor users who dislike motion, leaving only a plain opacity swap so you don't make them dizzy. - Use a CSS-only generator tool: Stop wasting time hunting down typos. Complex
@keyframesand vendor prefixes are best copied in one go from a dedicated web tool.
Final Decision Tree: Which Kind of Scroll Animation Should You Choose?
A decision guide to help you pick the best strategy for your project in about a second.
- Question 1: Are your target browsers all on modern Chrome (Chrome 115+)?
- YES ➔ Modern CSS Scroll-Driven Animations (use
animation-timeline: view(), zero JS) - NO ➔ Go to Question 2
- YES ➔ Modern CSS Scroll-Driven Animations (use
- Question 2: Do elements need to pin in place on scroll, or is the sequence very complex?
- YES ➔ GSAP + ScrollTrigger library (reliable, powerful control)
- NO ➔ IntersectionObserver + CSS @keyframes (the lightest, most versatile and highly recommended approach)
Avoid over-engineering by loading a heavy JS animation library into a lightweight marketing page. Get the most out of native browser technology instead.
Key Terms Glossary and One-Line Cheat Sheet
These are the essential terms you need to know when doing CSS animation work.
- Keyframes (
@keyframes): A CSS rule that defines the important points of change in an animation (the start state and end state) and lets the browser fill in everything in between. - Fade In: The classic visual effect where
opacitygoes from 0 to 1 and the element gradually appears on screen. animation-fill-mode: A browser instruction that decides which state (style) an element keeps before the animation plays or after it ends.IntersectionObserver: A high-performance browser API that asynchronously detects whether a specific HTML element has entered or left the user's viewport.- Scroll-Driven Animations: A recent CSS spec that ties an animation timeline to the user's scroll position instead of to time.
- Staggering: A polished timing technique where a list of items doesn't appear all at once but shows up one after another with a slight delay between them.
- prefers-reduced-motion: A feature that improves accessibility by using a media query to detect whether the user has turned on "reduce motion" in their OS settings.
- Hardware acceleration: An optimization technique that moves animation calculations off the main thread and onto the GPU compositor layer to prevent dropped frames (stutter).
Understanding the exact terms is the strongest tool you have for searching for problems and collaborating smoothly with colleagues.
Frequently Asked Questions
Q1. Why does a delayed animation flicker terribly without animation-fill-mode: both?
A1. By default, a CSS keyframe effect only applies to an element while it's actually playing. If you set animation-delay: 1s, then during that one second of waiting, the keyframe's opacity: 0 rule isn't applied, and the element renders with its original CSS (usually the visible state). So you get a flicker: it's already on screen, then it disappears, then it fades in. Only when you declare both or backwards does the from state get applied ahead of time during the delay.
Q2. How does IntersectionObserver differ in performance from plain window.addEventListener('scroll')?
A2. A scroll event listener fires synchronously dozens to hundreds of times per second whenever the user turns the scroll wheel, forcing heavy operations like getBoundingClientRect() onto the main thread. That causes forced synchronous layout (reflow). IntersectionObserver, on the other hand, determines intersections asynchronously inside the browser engine and hands you a callback at an optimized moment, so there's no main-thread bottleneck at all.
Q3. How does the modern CSS animation-timeline: view() work without JavaScript?
A3. This property completely changes the timeline's reference from time to scroll progress. It maps the moment an element appears at the bottom of the viewport to 0% and the moment it disappears past the top to 100%, then renders the keyframes forward and backward according to scroll position. Everything is handled at the browser's compositor thread level with no JS involvement, which gives you extremely smooth motion — but it still isn't officially supported in Safari and Firefox.
This article was adapted from our Korean original. Read the Korean original