webdevpuneet.com

CSS-Only Parallax Perspective Section — Free HTML CSS JS Snippet

CSS-Only Parallax Perspective Section · Scroll · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<div class="pp-container" id="ppContainer">
  <section class="pp-group">
    <div class="parallax-bg pp-bg-a"></div>
    <div class="pp-foreground">
      <span class="pp-eyebrow">Scene One</span>
      <h2>Pure CSS Depth</h2>
      <p>Scroll inside this box — no scroll-linked JavaScript at all.</p>
    </div>
    <span class="pp-scrollcue">Scroll ↓</span>
  </section>

  <section class="pp-group">
    <div class="parallax-bg pp-bg-b"></div>
    <div class="pp-foreground">
      <span class="pp-eyebrow">Scene Two</span>
      <h2>Perspective &amp; TranslateZ</h2>
      <p>The background layer sits behind the scroll plane and scales to compensate.</p>
    </div>
  </section>

  <section class="pp-group">
    <div class="parallax-bg pp-bg-c"></div>
    <div class="pp-foreground">
      <span class="pp-eyebrow">Scene Three</span>
      <h2>Zero Scroll Listeners</h2>
      <p>The browser's own 3D rendering handles the depth illusion.</p>
    </div>
  </section>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Parallax depth effect achieved with zero scroll event listeners or JavaScript
Uses only perspective, transform-style: preserve-3d, translateZ, and scale
Three independently styled background scenes demonstrating the same technique
Immune to scroll-jank since there is no per-frame JS recalculation at all
Self-contained scrollable demo container for an isolated live preview
Translucent foreground content cards readable over any background gradient
Fully responsive — background scale compensation keeps layers filling their section
Technique portable to any scrollable container, not just full-page scroll

About this UI Snippet

CSS-Only Parallax Perspective Section — Depth Effect With Zero Scroll JavaScript

Screenshot of the CSS-Only Parallax Perspective Section snippet rendered live

Most scroll parallax effects rely on a JavaScript scroll listener recalculating transforms on every frame. This snippet achieves the same layered depth illusion using only CSS — the perspective, transform-style: preserve-3d, and translateZ() combination — so scrolling produces a parallax effect the browser's own 3D rendering engine computes natively, with no scroll event listener at all.

Perspective on the scroll container

The trick starts on .pp-container, the scrollable element itself: perspective: 1px combined with overflow-y: scroll puts the whole scrolling context into a shallow 3D space. Because the perspective value is so small (1px instead of a more typical few hundred), any element pushed backward in Z-space appears to move dramatically less relative to scroll than an element at the natural Z position — that's the entire depth illusion, computed by the browser's rendering engine as a side effect of 3D perspective math, not by JavaScript recalculating positions.

Pushing the background layer back and compensating its scale

Each .parallax-bg layer is given transform: translateZ(-1px) scale(2). Moving an element 1px backward in Z-space at a 1px perspective effectively halves its apparent size, so multiplying its scale by 2 compensates and restores it to filling its container visually — while its actual scroll displacement (governed by the perspective math) is now dramatically slower than the normal-flow foreground content in front of it.

transform-style: preserve-3d ties it together

Each .pp-group section sets transform-style: preserve-3d, which is required for the perspective set on the ancestor .pp-container to actually apply 3D positioning to the .parallax-bg child instead of flattening it into the 2D plane. Without this, translateZ would have no visible effect.

Genuinely zero scroll-linked JavaScript

The JS file in this snippet does no visual work at all — there is no scroll listener, no requestAnimationFrame loop, and no per-frame style recalculation. The entire parallax effect is a byproduct of how the browser paints elements positioned in 3D space relative to a perspective origin as the page scrolls, which is both simpler to reason about and immune to the scroll-jank concerns that JS-driven parallax needs careful rAF throttling to avoid.

Step by step

How to Use

  1. 1
    Load the snippetClick "CSS-Only Parallax Perspective Section" in the sidebar to load its HTML, CSS, and JS into the editor panels. The preview updates instantly.
  2. 2
    Edit the codeModify any panel — HTML, CSS, or JS. The preview refreshes as you type. Use Reset in each panel header to restore the original.
  3. 3
    Preview on devicesClick the Mobile (375px), Tablet (768px), or Desktop buttons in the preview header to check responsiveness.
  4. 4
    Export in your formatClick "HTML" to download a standalone file, "JSX" for a React component, "Tailwind" for a React + Tailwind CSS component, "Tailwind HTML" for a standalone HTML file with Tailwind CDN, "Vue" for a Vue 3 SFC with <template>/<script setup>/<style scoped>, or "Angular" for a standalone Angular .component.ts file. "Copy all" copies the full code to clipboard.
  5. 5
    Save your versionClick "Save as", type a name, and press Enter. Your snippet saves to IndexedDB and appears in the Saved tab.

Real-world uses

Common Use Cases

📣
Marketing storytelling pages
Create depth-rich section breaks with no scroll listeners, using perspective, translateZ and scale to separate layers.
📚
CSS 3D transform tutorials
Provide a canonical CSS 3D transform example showing how transform-style: preserve-3d with a perspective container produces parallax naturally.
⚡
Performance-sensitive sites
Offer a parallax option for teams that cannot afford scroll-jank, since no per-frame JavaScript recalculation takes place.
🧱
CMS and static site templates
Add a parallax section to templates without any script dependencies, with three differently styled background scenes showing the technique.

Got questions?

Frequently Asked Questions

The scrollable container has a shallow CSS perspective (1px). Background layers are pushed backward in 3D space with translateZ(-1px) and their scale compensated to fill the viewport again. Because of how CSS 3D perspective projection works, an element positioned further back in Z-space visually displaces less per pixel of scroll than normal-flow content in front of it — the browser computes this automatically as part of rendering, with no JavaScript involved.

Moving an element backward in a 1px perspective space roughly halves how large it appears, since it is now twice as "far" from the viewer in perspective terms. Scaling it up by 2 compensates for that shrinkage so it still visually fills its container, while its slowed scroll displacement (the actual parallax effect) is unaffected by the scale.

Yes — replace the gradient in a .parallax-bg rule with a background-image, keep background-size: cover and background-position: center, and the same translateZ/scale technique will apply the parallax effect to it identically.