#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Add Parallax Effects to Background Sections

A parallax background moves more slowly than the content in front of it as the user scrolls, which adds a sense of depth. You can build it with one line of CSS, with a pure-CSS 3D trick, with a few lines of JavaScript, or with the newer CSS scroll-driven animations.

This tutorial shows all four with working code, explains where each one fails (iPhones, performance, accessibility) and how to pick one.

Add Parallax Effects to Background Sections

The starting markup

All examples use a simple page with a parallax "hero" section followed by normal content:

<section class="parallax">
  <div class="parallax-bg"></div>
  <h2>Learn to build for the web</h2>
</section>
<section class="content">...normal text...</section>

Method 1: background-attachment: fixed

The quickest version. The background image stays fixed to the viewport while the section scrolls over it.

.parallax {
  min-height: 70vh;
  display: grid;
  place-items: center;
  background-image: url("/img/mountains.webp");
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
}

Limits: this is a "fixed" effect rather than true slow-motion parallax, and iOS Safari does not support background-attachment: fixed in the usual way, so on iPhones the image simply scrolls normally. It can also cause repaint jank on long pages. Use it when a simple fallback on mobile is acceptable. (With this method you do not need the .parallax-bg div.)

Method 2: pure CSS with 3D perspective

This method makes a scrolling container with perspective and pushes the background layer "back" in 3D space, so it moves more slowly.

.page {
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  perspective: 1px;
}
.parallax {
  position: relative;
  min-height: 70vh;
  display: grid;
  place-items: center;
  transform-style: preserve-3d;
}
.parallax-bg {
  position: absolute;
  inset: 0;
  background: url("/img/mountains.webp") center / cover;
  transform: translateZ(-1px) scale(2);
  z-index: -1;
}

Wrap your sections in <div class="page">. With perspective: 1px, a layer at translateZ(-1px) appears half its size and moves at half speed. The scale(2) brings it back to full size, using the formula scale = 1 + (−translateZ ÷ perspective). Move it to translateZ(-2px) scale(3) for a slower layer.

Limits: the page now scrolls inside .page instead of the window, which can affect sticky headers, scroll-position scripts and some browser features. Test it with your layout.

Method 3: a small JavaScript script

JavaScript gives full control and works on iPhones. The key to smooth performance is to change only transform and to update at most once per frame.

.parallax { position: relative; overflow: hidden; min-height: 70vh;
            display: grid; place-items: center; }
.parallax-bg { position: absolute; left: 0; right: 0; top: -30%; bottom: -30%;
               background: url("/img/mountains.webp") center / cover;
               will-change: transform; z-index: -1; }
const layers = document.querySelectorAll(".parallax-bg");
const speed = 0.2;            // 0 = no effect, higher = stronger
let ticking = false;

function update() {
  layers.forEach(layer => {
    const top = layer.parentElement.getBoundingClientRect().top;
    layer.style.transform = `translate3d(0, ${-top * speed}px, 0)`;
  });
  ticking = false;
}

const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (!reduceMotion) {
  window.addEventListener("scroll", () => {
    if (!ticking) {
      requestAnimationFrame(update);
      ticking = true;
    }
  }, { passive: true });
  update();
}

How it works: as the section scrolls up, its top value goes from positive to negative. The background is moved by a fraction of that distance in the opposite direction, so it appears to move more slowly than the section. The layer extends 30% above and below the section so its edges never show; if you raise speed, increase that overhang. The requestAnimationFrame flag ensures the work happens at most once per screen refresh, and { passive: true } tells the browser the listener will not block scrolling.

Method 4: CSS scroll-driven animations

Newer CSS can link an animation directly to scroll position without JavaScript:

@keyframes drift {
  from { transform: translateY(-15%); }
  to   { transform: translateY(15%); }
}
@supports (animation-timeline: view()) {
  .parallax-bg {
    animation: drift linear both;
    animation-timeline: view();
  }
}

The view() timeline runs the animation as the section passes through the viewport. It runs off the main thread in supporting browsers, so it is very smooth. Support arrived first in Chromium-based browsers and is still spreading, so the @supports rule keeps other browsers on a static background. Check current support on MDN or caniuse.com before relying on it.

Accessibility and performance checklist

  • Respect reduced motion. Some people get dizzy from motion effects. Disable parallax when the user has asked for reduced motion:
    @media (prefers-reduced-motion: reduce) {
      .parallax-bg { transform: none !important; animation: none !important; }
      .parallax { background-attachment: scroll; }
    }
  • Keep text readable. Add a dark overlay or text shadow if the image is busy, and check contrast.
  • Compress images. Use WebP or AVIF at sensible sizes; a 5 MB hero image will hurt load time far more than the effect helps.
  • Use it sparingly. One or two parallax sections per page is usually enough.
  • Test on a real phone, especially an iPhone, and at about 390px width to make sure nothing causes horizontal scrolling.

Which method to choose

MethodEffortWorks on iOSBest for
background-attachment: fixedOne lineNo (falls back to normal scroll)Quick desktop effect
CSS 3D perspectiveMediumYesCSS-only sites that can scroll in a container
JavaScriptMediumYesFull control, multiple layers
Scroll-driven animationsLowWhere supportedModern browsers, with a static fallback

To build the rest of the page, see responsive web design with HTML and CSS and CSS media queries. Pair the effect with CSS blur and filter effects, use transitions as in a CSS-only toggle switch, and put it all together in a portfolio website.

Placed Students

Our Clients

Partners

...