How do I build a simple parallax effect with CSS only?
Parallax means two layers move at different speeds while you scroll, which gives a feeling of depth. We build it with a background layer and a text layer, no library, and it stops by itself when the user asks for less motion.
Steps
- Set up two layers: a background (
hero-bg) and the text above it. The section getsoverflow:hiddenso the extra background never shows. - Make the background taller than the section (
inset:-20% 0) so it has room to move without showing an edge. - Tie the background to scrolling with
animation-timeline: view(): it drifts from-8%to8%while the section passes through the viewport. The text stays fixed inside the section, so the background looks slower. - Wrap the motion in
@supports (animation-timeline: view())and inprefers-reduced-motion: no-preference. An older browser, or a user who asked for less motion, sees a calm static background. - Animate
transformonly, and check the text contrast against the background (white on this dark blue is easily high enough).
Sources
- MDN, animation-timeline (Baseline status and view()): https://developer.mozilla.org/en-US/docs/Web/CSS/animation-timeline
- MDN, CSS scroll-driven animations: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_scroll-driven_animations
- MDN, prefers-reduced-motion: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion
- web.dev, stick to compositor-only properties (transform and opacity): https://web.dev/articles/stick-to-compositor-only-properties-and-manage-layer-count
The code
HTML
<section class="hero" dir="ltr" lang="en">
<div class="hero-bg" aria-hidden="true"></div>
<h2>The background moves slower than the text</h2>
</section>
<style>
.hero{position:relative;min-height:70vh;overflow:hidden;display:grid;place-items:center;background:#0d0e11}
.hero-bg{position:absolute;inset:-20% 0;background:linear-gradient(160deg,#27407a,#0d0e11 70%)}
.hero h2{position:relative;margin:0;color:#fff;font:700 clamp(1.6rem,5vw,3rem)/1.3 Cairo,system-ui,sans-serif}
@keyframes drift{from{transform:translateY(-8%)}to{transform:translateY(8%)}}
/* Only browsers that support scroll timelines, and only without reduce-motion */
@supports (animation-timeline: view()) {
@media (prefers-reduced-motion: no-preference) {
.hero-bg{animation:drift linear both;animation-timeline:view()}
}
}
</style>Note: MDN lists animation-timeline as "Baseline 2026 Newly available", meaning it works across the latest browsers since October 2026 and may not work on older devices. That is why the code uses @supports.
Range: use animation-range if you want the motion to cover only part of the pass through the section.
can't find the motion you need? Request it here
Related categories: Canvas & Particles · Video & Ad Motions