Fade elements in on scroll with CSS transitions and react-intersection-observer

A common effect is to fade content in as the user scrolls down to it. The browser’s Intersection Observer API tells you when an element enters the viewport, and a CSS transition does the animation. The react-intersection-observer package wraps the observer in a hook, so the React side is a few lines. The examples were checked against react-intersection-observer 11.
Using useInView
Section titled “Using useInView”Install the package:
npm install react-intersection-observeruseInView returns a ref to attach to the element you want to watch, an inView boolean, and the raw IntersectionObserverEntry. You can destructure it as an object or an array:
import { useInView } from 'react-intersection-observer';
function Example() { const { ref, inView } = useInView(); // or: const [ref, inView] = useInView();
return <div ref={ref}>{inView ? 'Visible' : 'Not visible'}</div>;}The hook takes a single options object. The ones you’ll use most:
threshold: how much of the element (0 to 1) must be visible beforeinViewbecomes true. Defaults to0.rootMargin: grows or shrinks the area that counts as “in view”, using CSS margin syntax such as'0px 0px -100px 0px'.triggerOnce: stop observing after the element has been seen once.
A fade-in on scroll
Section titled “A fade-in on scroll”The idea is to always render the element, start it transparent, and add an is-visible class once it’s in view. The transition on opacity (and a small transform) does the rest.
import { useInView } from 'react-intersection-observer';import './styles.css';
export function FadeIn({ children }) { const { ref, inView } = useInView({ threshold: 0.2, triggerOnce: true });
return ( <div ref={ref} className={`fade-in${inView ? ' is-visible' : ''}`}> {children} </div> );}.fade-in { opacity: 0; transform: translateY(20px); transition: opacity 0.6s ease-out, transform 0.6s ease-out;}
.fade-in.is-visible { opacity: 1; transform: none;}
/* Respect users who have asked the OS for less motion. */@media (prefers-reduced-motion: reduce) { .fade-in { transform: none; transition: none; }}Use it by wrapping any content:
<FadeIn> <h2>Hello world!</h2></FadeIn>Two details matter here. First, the content is always rendered; only the class changes. If you conditionally render children based on inView ({inView ? <p>…</p> : null}), the wrapper has no height until the content appears, so the page jumps and nothing is there to fade. Second, triggerOnce: true keeps the element visible after the first reveal. Without it, the class is removed when the element scrolls out, so it fades out and back in every time.
How the transition shorthand works
Section titled “How the transition shorthand works”transition: property duration timing-function delay;property: the CSS property to animate, such asopacity,transformorbackground-color. Separate several with commas, as in the example above.duration: how long the change takes, such as1sor300ms.timing-function: the easing curve, such asease,linear,ease-in-outorcubic-bezier(0.1, 0.7, 1.0, 0.1).delay: how long to wait before starting, such as0sor500ms.
A transition runs whenever the property’s value changes, whether that is caused by a class being added from JavaScript or by a pseudo-class like :hover:
.box { opacity: 1; transition: opacity 1s ease-in-out;}
.box:hover { opacity: 0.5;}Animating opacity and transform is cheaper than animating properties like height or top, because browsers can usually run them without recalculating layout.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)