December 4, 2026
Parallax scrolling makes layers move at different speeds, so the page seems to have depth. Without a library you have three
choices: the CSS property background-attachment: fixed, a small script that shifts each layer with a transform,
or CSS scroll-driven animations. The script is the one that works everywhere.
How do the three compare?
| Method | Code | Works on iPhone | Smooth |
|---|---|---|---|
background-attachment: fixed | One line | No | Repaints a lot |
| Transform from scroll position | About 20 lines | Yes | Yes |
| CSS scroll-driven animation | A few lines | Check current support | Yes |
What is the script?
<section class="scene">
<img class="layer" data-speed="-0.3" src="hills-back.svg" alt="">
<img class="layer" data-speed="-0.15" src="hills-mid.svg" alt="">
<h2 class="layer" data-speed="0.1">Depth</h2>
</section>
const layers = [...document.querySelectorAll(".layer")];
const calm = matchMedia("(prefers-reduced-motion: reduce)");
function update() {
if (calm.matches) return;
for (const el of layers) {
const box = el.parentElement.getBoundingClientRect();
if (box.bottom < 0 || box.top > innerHeight) continue; // off screen
// distance of the section's centre from the centre of the window
const d = box.top + box.height / 2 - innerHeight / 2;
el.style.transform = `translate3d(0, ${d * el.dataset.speed}px, 0)`;
}
}
let queued = false;
addEventListener("scroll", () => {
if (queued) return;
queued = true;
requestAnimationFrame(() => { queued = false; update(); });
}, { passive: true });
update();
A negative speed makes a layer lag behind the page, which reads as far away. A positive speed makes it move ahead, which reads as close. Measuring from the centre of the window means every layer sits exactly in its designed position when the section is in the middle of the screen.
What is wrong with background-attachment: fixed?
It pins a background image to the window while the section scrolls over it. Two problems: iOS Safari ignores it, and on other browsers it forces the background to be repainted on every scroll frame. It is fine for one decorative section on a desktop-only page. Do not build a whole page on it.
What does the modern CSS version look like?
@supports (animation-timeline: view()) {
.layer-back {
animation: lag linear both;
animation-timeline: view();
}
@keyframes lag { from { translate: 0 -60px; } to { translate: 0 60px; } }
}
The animation's progress is tied to the element's journey through the window instead of to time. It runs off the main thread,
so it stays smooth even when scripts are busy. Wrap it in @supports so other browsers simply show a still layer.
How do I keep parallax tasteful?
- Keep speeds between minus 0.3 and 0.3. Larger values pull layers out of their sections.
- Leave room. A layer that moves 60 pixels needs 60 pixels of spare image above and below, or a gap will show.
- Do not move body text. Parallax is for pictures, shapes and large headings.
Which finished examples can I copy?
| Element | What it does |
|---|---|
| Layered parallax scene | A landscape of hill ridges, a sun and a headline tucked behind the front hill, each moving at its own speed. |
| Parallax pattern sections | Full-height sections whose patterned backgrounds drift slower than the page. |
| Alternate column scroll | A three-column image grid where the outer columns slide up and the middle one slides down. |
| Paper cutout parallax | Eight layered SVG paper cutouts that drift with weighted pointer parallax. |
| Depth map shader | A photo and its depth map drawn by a WebGL shader, so near parts move more than far ones. |
All are free in the Enrichr Library. It lists 22 parallax elements in total.
Where to go next
- Scroll animations without a library.
- How to make a horizontal scroll section.
- What to do with animations when a visitor asks for reduced motion.
FAQ
How do I make a parallax effect with CSS only?
The old way is background-attachment: fixed on a section with a background image. The modern way is a scroll-driven animation: animation-timeline: view() with keyframes that shift the layer. Check browser support before relying on the second.
Why does parallax not work on iPhone?
iOS Safari does not support background-attachment: fixed on scrolling pages and treats it as a normal background. Use a transform-based parallax instead.
Why is my parallax janky?
Either the work is done directly in the scroll event, or you are changing a property that forces layout or paint, such as top or background-position. Use transform inside requestAnimationFrame.
Is parallax bad for accessibility?
Large parallax movement can cause dizziness and nausea for people with vestibular disorders. Respect prefers-reduced-motion and keep the layers still when it is set.