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

PublishedToolCSS Animation Generator

Contents
  1. Why a Scroll Fade-In Plays Without Any Video
  2. How to Check It: Building It Yourself with CSS Keyframes
  3. Where People Get Stuck
  4. The Takeaway
  5. Under the Hood: How Does the Browser Handle a Fade-In?
  6. Scroll Animation Trigger Comparison (Matrix)
  7. 5 Critical Mistakes People Make (and How to Prevent Them)
  8. A 5-Step, Stutter-Free Scroll Reveal Workflow
  9. 7 Tips for Polishing Your Animations
  10. Final Decision Tree: Which Kind of Scroll Animation Should You Choose?
  11. Key Terms Glossary and One-Line Cheat Sheet
  12. 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.

CSS Animation Generator

Free · no sign-up · generate CSS code right in your browser

Where People Get Stuck

SymptomCause
The animation doesn't show at allThe name in the animation property doesn't match the @keyframes name, or it's misspelled
The element flickers briefly before the animation startsanimation-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 scrollingCSS alone can't read the scroll position — you need to add a class with IntersectionObserver
It never stops repeatinganimation-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.

CSS Animation Generator

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 during animation-delay.
  • forwards: After the animation ends, it keeps the 100% keyframe styles permanently.
  • both: Combines the behavior of backwards and forwards, 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.

ApproachJavaScript overheadBundle sizeBrowser supportHardware accelerationBest use case
Pure CSS Scroll-Driven (animation-timeline: view())None at all (0%)0KBChrome 115+ (limited in Safari/Firefox)Best (GPU)Targeting modern browsers, fully static sites with no JS
IntersectionObserver + CSS @keyframesVery low< 1KBAll browsers (no polyfill needed)Best (GPU)Typical blogs, marketing landing pages (most recommended)
Scroll Event Listener + JS StylesVery high (jank risk)0KBDown to legacy browsersLow (main thread)An anti-pattern to avoid at all costs
GSAP ScrollTriggerMedium (well optimized)About 60KBAll browsersHighComplex pinning, timeline sequence animations
AOS LibraryLow to mediumAbout 14KBAll browsersHighWhen 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.

  1. 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.
  2. Trying to transition visibility: hidden or display: none: Browsers can't interpolate between values of these two properties. That's why elements just pop into view. You need to animate opacity instead.
  3. 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.
  4. Overusing heavy CSS scroll event listeners: If you use window.addEventListener('scroll') without throttling or the passive: true flag, the frame rate on mobile devices tanks.
  5. 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

  1. Step 1 (Build the initial HTML skeleton): Mark up the card elements you want to animate in a hidden state with opacity: 0 and a little transform: translateY(20px).
  2. Step 2 (Write the @keyframes): Declare a fadeInUp keyframe that goes from from { opacity: 0; transform: translateY(20px); } to to { opacity: 1; transform: translateY(0); }.
  3. Step 3 (Set the easing curve and mode): Hook up the animation property and always pair it with animation-fill-mode: both and a natural cubic-bezier easing function.
  4. Step 4 (Attach a lightweight observer): Set up an IntersectionObserver with a threshold around 0.1 ∼ 0.2 so the visible class only toggles when the element enters the viewport.
  5. Step 5 (Stagger with CSS variables): Add style="--i: 1" to each list item's inline style, then use animation-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.

  1. Keep the displacement small: Hold your translateY distance to around 15 ∼ 30px. Too much movement looks tacky and pulls focus. Subtle motion feels premium.
  2. Give it inertia with cubic-bezier: Instead of a default like ease-out, use cubic-bezier(0.16, 1, 0.3, 1) (the editorial easing). It produces a stop that feels heavy yet smooth, like real-world physics.
  3. 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.
  4. 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.
  5. Adopt progressive enhancement: For the latest Chrome, declare JavaScript-free CSS animation-timeline: view() inside an @supports query to push performance to the limit.
  6. Always provide a pure-opacity fallback: For accessibility, apply transform: none for users who dislike motion, leaving only a plain opacity swap so you don't make them dizzy.
  7. Use a CSS-only generator tool: Stop wasting time hunting down typos. Complex @keyframes and 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
  • 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 opacity goes 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

Related articles

  1. CSS filter breaks position: fixed: why it happens and fixA CSS filter on an ancestor becomes the containing block for position: fixed children, so your fixed element scrolls with the page. Find it in DevTools.
  2. CSS Glitch Text Effect: clip-path Layers and Keyframe TimingSee how CSS glitch text works: three stacked color layers, ::before and ::after shadows, clip-path inset() math, and keyframe timing you can copy.
  3. CSS Glitch Text Effect: How the Shake and Color Split WorksGlitch text isn't a video or GIF. It's the same text stacked in different colors and shaken with CSS. Here's how the layers and clip-path trick work.
  4. CSS Gradient Color Stops: Why Colors Shift SharplyColor stops fill the space between two colors in a line. See why tight stops change color fast and why big brightness gaps look muddy in between.
  5. CSS gradient text with background-clip: a practical guideSee how background-clip: text and -webkit-text-fill-color make gradient headings, plus fixes for text selection, dark mode, and older browsers.
  6. CSS Grid fr unit: why it beats % and px in responsive layoutsHow the fr unit and repeat() make CSS Grid more reliable than % or px, how the math works, and how to set columns, rows and gap and copy the CSS.

Related tools