How to Create Parallax Scrolling Effects with SVG
Parallax scrolling adds depth to a web page by moving foreground and background elements at different speeds as the user scrolls. While CSS alone can achieve many parallax tricks,
How to Create Parallax Scrolling Effects with SVG
Parallax scrolling adds depth to a web page by moving foreground and background elements at different speeds as the user scrolls. While CSS alone can achieve many parallax tricks, combining it with scalable vector graphics (SVG) gives you sharp, resolution-independent visuals that stay crisp on any device. In this guide, you’ll learn practical patterns, small code snippets, and a lightweight approach you can adapt in SVG-first projects.
Why SVG for parallax?
SVG is resolution-independent, vector-based, and performs well across devices. When you group related shapes inside <g>
elements, you can apply per-layer transforms and animate or update them with minimal overhead. SVG also plays nicely with CSS variables and JavaScript, making it a natural fit for scroll-driven effects in UI components, hero sections, and data visualizations. For an SVG-centric workflow, explore more examples at SVGeenius.
Set up a simple multi-layer SVG parallax
Start with a clean two-layer example: a distant mountain scene in the back and a closer foreground element. We’ll move them at different speeds using a small script that reads a speed factor from a data-speed attribute. This keeps the markup approachable and reusable for more layers.
To make the movement feel natural, we translate each layer along the Y-axis at its speed factor when the user scrolls. The deeper layers move slower, creating a sense of depth while keeping a performant, GPU-friendly path for modern browsers.
Minimal JavaScript to drive the effect
The following snippet uses requestAnimationFrame to smoothly update the transform of each layer based on the scroll position. It keeps the code compact and easy to test in small projects or design explorations. You can adapt the speeds in data-speed attributes to taste.
// Simple parallax on scroll for SVG layers
(function(){
const parallax = document.querySelectorAll('.parallax svg .layer');
let ticking = false;
const onScroll = () => {
const scrollY = window.scrollY || window.pageYOffset;
parallax.forEach(el => {
const speed = parseFloat(el.getAttribute('data-speed')) || 0;
el.style.transform = `translate3d(0, ${scrollY * speed}px, 0)`;
});
ticking = false;
};
window.addEventListener('scroll', () => {
if (!ticking) {
window.requestAnimationFrame(onScroll);
ticking = true;
}
}, { passive: true });
// Initialize positions
onScroll();
})();
Tips for practical implementation
- Limit the number of layers to avoid layout jank. SVGs scale well, but many animated groups can add paint work. Start with 2–4 layers and test on mobile.
- Use data-speed values in a narrow range (e.g., 0.1–1). If you go above 1, foreground moves faster than scroll, which can feel jarring.
- Prefer Y-axis translations for vertical scroll effects. Horizontal parallax can be added but may require additional viewport checks to stay performant.
- Combine with CSS media queries to disable complex parallax on small screens where performance matters. See more guidance at SVGeenius resources.