Home / Learn / How do I build a simple parallax effect with CSS only?

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

  1. Set up two layers: a background (hero-bg) and the text above it. The section gets overflow:hidden so the extra background never shows.
  2. Make the background taller than the section (inset:-20% 0) so it has room to move without showing an edge.
  3. Tie the background to scrolling with animation-timeline: view(): it drifts from -8% to 8% while the section passes through the viewport. The text stays fixed inside the section, so the background looks slower.
  4. Wrap the motion in @supports (animation-timeline: view()) and in prefers-reduced-motion: no-preference. An older browser, or a user who asked for less motion, sees a calm static background.
  5. Animate transform only, and check the text contrast against the background (white on this dark blue is easily high enough).

Sources

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