Parallax scrolling is a captivating web design technique where background images move slower than the foreground content, creating an illusion of depth and immersion. This effect can enhance the visual appeal of a website, making it more engaging for users. The great news is that you can achieve this stunning effect using CSS only, without the need for JavaScript. In this article, we will explore various methods to create CSS-only parallax scrolling effects, offering you practical and actionable insights to implement in your projects.
Understanding Parallax Scrolling
What is Parallax Scrolling?
Parallax scrolling is a technique where background content moves at a different speed than the foreground content as you scroll down the page. This effect adds a sense of depth and dimension to a webpage, making the browsing experience more dynamic and visually interesting.
Why Use Parallax Scrolling?
Parallax scrolling can make your website more engaging and memorable. It captures the user’s attention, encourages longer site visits, and can enhance storytelling on your site.
When used effectively, it can highlight key areas of your site and guide users through your content in a more interactive way.
Basic Parallax Scrolling with CSS
Setting Up the HTML Structure
To start, let’s create a simple HTML structure that includes sections for the parallax effect. We’ll use div elements for the background and content sections.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Parallax Scrolling</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="parallax"></div>
<div class="content">
<h1>Welcome to Our Website</h1>
<p>This is an example of a simple parallax scrolling effect using CSS only.</p>
</div>
<div class="parallax"></div>
<div class="content">
<h1>More Content Here</h1>
<p>Additional content goes here.</p>
</div>
<div class="parallax"></div>
<div class="content">
<h1>Even More Content</h1>
<p>Keep scrolling to see the parallax effect in action.</p>
</div>
</body>
</html>
Adding Basic CSS Styles
Next, we’ll add some CSS to style the parallax sections and content. The key to the parallax effect is using the background-attachment
property set to fixed
.
body, html {
margin: 0;
padding: 0;
height: 100%;
font-family: Arial, sans-serif;
}
.parallax {
background-image: url('background.jpg');
height: 500px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
In this example, the .parallax
class is applied to div elements with a background image that remains fixed while the content scrolls over it. This creates a simple parallax effect.
Advanced Parallax Scrolling Techniques
Layered Parallax Effect
To create a more sophisticated parallax effect, you can use multiple layers moving at different speeds. This adds depth and complexity to the visual experience.
HTML Structure for Layered Parallax
<div class="parallax-container">
<div class="parallax-layer layer1"></div>
<div class="parallax-layer layer2"></div>
<div class="parallax-layer layer3"></div>
</div>
<div class="content">
<h1>Layered Parallax Effect</h1>
<p>Experience a more immersive parallax effect with multiple layers.</p>
</div>
CSS Styles for Layered Parallax
.parallax-container {
position: relative;
height: 500px;
overflow: hidden;
perspective: 1px; /* Creates a 3D space for the parallax effect */
}
.parallax-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 500px;
background-repeat: no-repeat;
background-size: cover;
}
.layer1 {
background-image: url('layer1.png');
transform: translateZ(-1px) scale(2); /* Moves the layer back and scales it up */
}
.layer2 {
background-image: url('layer2.png');
transform: translateZ(-0.5px) scale(1.5);
}
.layer3 {
background-image: url('layer3.png');
transform: translateZ(0); /* This layer remains in the foreground */
}
Responsive Parallax Scrolling
Ensuring your parallax effect works well on all devices is crucial. You can achieve this by using media queries to adjust the background size and attachment properties for smaller screens.
Responsive CSS Example
@media (max-width: 768px) {
.parallax {
background-attachment: scroll; /* Disables fixed background on smaller screens */
}
}
@media (max-width: 768px) {
.parallax-container {
height: 300px;
}
.parallax-layer {
height: 300px;
}
}
This CSS ensures that the parallax effect is disabled on smaller screens, where it might not work as well or could affect performance.
Enhancing Parallax Scrolling with CSS
Vertical and Horizontal Parallax Scrolling
While vertical parallax scrolling is more common, horizontal parallax scrolling can add a unique twist to your design. Let’s explore how to create both vertical and horizontal parallax effects.
HTML Structure for Vertical and Horizontal Parallax
<div class="vertical-parallax"></div>
<div class="content">
<h1>Vertical Parallax Effect</h1>
<p>Scroll down to see the vertical parallax effect in action.</p>
</div>
<div class="horizontal-parallax-container">
<div class="horizontal-parallax"></div>
</div>
<div class="content">
<h1>Horizontal Parallax Effect</h1>
<p>Scroll sideways to see the horizontal parallax effect in action.</p>
</div>
CSS Styles for Vertical and Horizontal Parallax
.vertical-parallax {
background-image: url('vertical-background.jpg');
height: 500px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.horizontal-parallax-container {
width: 100%;
overflow-x: scroll;
white-space: nowrap;
}
.horizontal-parallax {
display: inline-block;
width: 200%;
height: 500px;
background-image: url('horizontal-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
In this example, .vertical-parallax
uses the fixed background to create a vertical parallax effect. For horizontal scrolling, .horizontal-parallax-container
and .horizontal-parallax
elements are styled to allow horizontal scrolling.
Parallax Scrolling with Multiple Backgrounds
Using multiple backgrounds allows for more complex and layered parallax effects. Each background image can move at a different speed, creating a more dynamic visual experience.
HTML Structure for Multiple Backgrounds
<div class="multiple-backgrounds"></div>
<div class="content">
<h1>Multiple Backgrounds Parallax Effect</h1>
<p>Experience a layered parallax effect with multiple backgrounds moving at different speeds.</p>
</div>
CSS Styles for Multiple Backgrounds
.multiple-backgrounds {
height: 500px;
background:
url('background1.jpg') 50% 0 / cover no-repeat,
url('background2.jpg') 50% 0 / cover no-repeat,
url('background3.jpg') 50% 0 / cover no-repeat;
background-attachment: fixed, fixed, fixed;
}
@media (max-width: 768px) {
.multiple-backgrounds {
background-attachment: scroll, scroll, scroll; /* Disable fixed background on smaller screens */
}
}
This CSS creates a parallax effect with three background images, each moving at different speeds for a layered effect.
Advanced Techniques for Parallax Scrolling
Using CSS Variables for Dynamic Effects
CSS variables allow for more dynamic and customizable parallax effects. You can adjust properties like speed and direction based on user input or other conditions.
HTML Structure with CSS Variables
<div class="dynamic-parallax"></div>
<div class="content">
<h1>Dynamic Parallax Effect</h1>
<p>Using CSS variables to create dynamic and customizable parallax effects.</p>
</div>
CSS Styles with CSS Variables
:root {
--parallax-speed: 0.5;
}
.dynamic-parallax {
height: 500px;
background-image: url('dynamic-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
transform: translateY(calc(var(--scroll) * var(--parallax-speed)));
}
body {
height: 2000px;
}
window.addEventListener('scroll', () => {
document.documentElement.style.setProperty('--scroll', window.scrollY + 'px');
});
In this example, CSS variables are used to dynamically adjust the parallax speed based on the scroll position, creating a more interactive effect.
Combining Parallax with CSS Animations
Combining parallax scrolling with CSS animations can create even more engaging and interactive web experiences. Animations can be triggered based on scroll position to enhance the parallax effect.
HTML Structure with CSS Animations
<div class="animated-parallax"></div>
<div class="content">
<h1>Animated Parallax Effect</h1>
<p>Combining parallax scrolling with CSS animations for a more interactive experience.</p>
</div>
CSS Styles with CSS Animations
.animated-parallax {
height: 500px;
background-image: url('animated-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
animation: moveBackground 5s infinite linear;
}
@keyframes moveBackground {
0% {
background-position: center 0;
}
100% {
background-position: center 100%;
}
}
In this example, the background image moves continuously, creating a dynamic effect that combines parallax scrolling with CSS animations.
Creating Parallax Effects with CSS Grid
CSS Grid can be used to create sophisticated parallax effects by layering grid items and adjusting their movement based on scroll position.
HTML Structure with CSS Grid
<div class="grid-parallax">
<div class="grid-item layer1"></div>
<div class="grid-item layer2"></div>
<div class="grid-item layer3"></div>
</div>
<div class="content">
<h1>CSS Grid Parallax Effect</h1>
<p>Using CSS Grid to create layered parallax effects.</p>
</div>
CSS Styles with CSS Grid
.grid-parallax {
display: grid;
grid-template-rows: 1fr 1fr 1fr;
height: 500px;
position: relative;
}
.grid-item {
grid-row: 1 / -1;
background-repeat: no-repeat;
background-size: cover;
position: absolute;
width: 100%;
height: 100%;
}
.layer1 {
background-image: url('layer1.jpg');
transform: translateY(calc(var(--scroll) * 0.1));
}
.layer2 {
background-image: url('layer2.jpg');
transform: translateY(calc(var(--scroll) * 0.2));
}
.layer3 {
background-image: url('layer3.jpg');
transform: translateY(calc(var(--scroll) * 0.3));
}
body {
height: 2000px;
}
window.addEventListener('scroll', () => {
document.documentElement.style.setProperty('--scroll', window.scrollY + 'px');
});
This setup uses CSS Grid to layer items and adjust their positions based on scroll, creating a dynamic and layered parallax effect.
Creating CSS-Only Parallax Scrolling Effects: Advanced Examples and Techniques

Parallax Scrolling with Background Blend Modes
Using background blend modes can enhance the visual appeal of your parallax effects by combining multiple backgrounds in creative ways.
HTML Structure
<div class="blend-parallax"></div>
<div class="content">
<h1>Background Blend Mode Parallax Effect</h1>
<p>Using background blend modes to enhance parallax scrolling effects.</p>
</div>
CSS Styles
.blend-parallax {
height: 500px;
background-image: url('background1.jpg'), url('background2.jpg');
background-blend-mode: multiply;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
This CSS applies a blend mode to combine two background images, enhancing the parallax effect with a unique visual style.
Parallax Scrolling with SVG Filters
SVG filters can be used to apply sophisticated visual effects to your parallax backgrounds, such as blurring or color shifts.
HTML Structure
<div class="svg-filter-parallax"></div>
<div class="content">
<h1>SVG Filter Parallax Effect</h1>
<p>Using SVG filters to create advanced parallax scrolling effects.</p>
</div>
CSS Styles
.svg-filter-parallax {
height: 500px;
background-image: url('background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
filter: url('#blur-filter'); /* Applies the SVG filter */
}
svg {
position: absolute;
width: 0;
height: 0;
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
SVG Filter Definition
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="blur-filter">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>
</defs>
</svg>
This setup uses an SVG filter to apply a blur effect to the parallax background, creating a unique and sophisticated visual effect.
Combining Parallax Scrolling with CSS Transitions
CSS transitions can be used to create smooth parallax effects that change based on user interactions, such as hovering or clicking.
HTML Structure
<div class="transition-parallax"></div>
<div class="content">
<h1>CSS Transition Parallax Effect</h1>
<p>Combining parallax scrolling with CSS transitions for a smoother experience.</p>
</div>
CSS Styles
.transition-parallax {
height: 500px;
background-image: url('background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
transition: transform 0.5s ease-out;
}
.transition-parallax:hover {
transform: scale(1.05); /* Slightly scales up the background on hover */
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
In this example, hovering over the parallax section scales up the background image slightly, creating a smooth and interactive visual effect.
Creating Horizontal and Vertical Parallax Scrolling
Combining horizontal and vertical parallax effects can create a multidimensional scrolling experience.
HTML Structure
<div class="multidimensional-parallax">
<div class="vertical-parallax"></div>
<div class="horizontal-parallax"></div>
</div>
<div class="content">
<h1>Multidimensional Parallax Effect</h1>
<p>Combining vertical and horizontal parallax scrolling for a unique experience.</p>
</div>
CSS Styles
.multidimensional-parallax {
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
.vertical-parallax {
height: 50%;
background-image: url('vertical-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.horizontal-parallax {
width: 200%;
height: 50%;
background-image: url('horizontal-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
transform: translateX(calc(var(--scroll) * -0.5));
}
body {
height: 200vh;
}
window.addEventListener('scroll', () => {
document.documentElement.style.setProperty('--scroll', window.scrollY + 'px');
});
This example combines vertical and horizontal parallax effects to create a multidimensional scrolling experience, enhancing the visual complexity of your design.
Implementing Parallax Scrolling in Sections
Parallax effects can be applied to specific sections of your website to highlight particular content or create visual breaks between sections.
HTML Structure
<section class="parallax-section">
<div class="parallax-background"></div>
<div class="section-content">
<h1>Section with Parallax Background</h1>
<p>This section uses a parallax background to stand out.</p>
</div>
</section>
<div class="content">
<h1>Regular Content Section</h1>
<p>This is a standard content section following the parallax section.</p>
</div>
CSS Styles
.parallax-section {
position: relative;
height: 500px;
overflow: hidden;
}
.parallax-background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('parallax-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.section-content {
position: relative;
z-index: 1;
padding: 20px;
background-color: rgba(255, 255, 255, 0.8); /* Semi-transparent background for content */
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
In this example, the parallax effect is applied to a specific section, making it visually distinct and engaging.
Combining Parallax Effects with Fixed Elements
Combining parallax effects with fixed elements, such as navigation bars or side panels, can create a layered experience that guides users through your content.
HTML Structure
<div class="fixed-element">Fixed Navigation</div>
<div class="parallax-container">
<div class="parallax-background"></div>
</div>
<div class="content">
<h1>Content with Fixed Navigation</h1>
<p>Experience a layered parallax effect with fixed navigation.</p>
</div>
CSS Styles
.fixed-element {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 50px;
background-color: #333;
color: #fff;
text-align: center;
line-height: 50px;
z-index: 2;
}
.parallax-container {
height: 500px;
position: relative;
}
.parallax-background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
This setup combines a fixed navigation bar with a parallax background, creating a layered effect that enhances the browsing experience.
Advanced CSS-Only Parallax Scrolling Effects: Further Exploration

Parallax Scrolling with CSS Keyframes
CSS keyframes can be used to create complex and smooth parallax scrolling effects by defining multiple stages of animation for background movement.
HTML Structure
<div class="keyframe-parallax"></div>
<div class="content">
<h1>Keyframe Parallax Effect</h1>
<p>Using CSS keyframes to create smooth and dynamic parallax scrolling effects.</p>
</div>
CSS Styles
.keyframe-parallax {
height: 500px;
background-image: url('keyframe-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
animation: parallaxAnimation 10s infinite linear;
}
@keyframes parallaxAnimation {
0% {
background-position: center 0;
}
50% {
background-position: center 100%;
}
100% {
background-position: center 0;
}
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
In this example, the parallaxAnimation
keyframe moves the background image smoothly, creating a dynamic parallax effect as the user scrolls.
Parallax Scrolling with CSS Clip-Path
Using the clip-path
property can create unique parallax effects by clipping the background or foreground elements in different shapes.
HTML Structure
<div class="clip-path-parallax"></div>
<div class="content">
<h1>Clip-Path Parallax Effect</h1>
<p>Using CSS clip-path to create visually engaging parallax effects.</p>
</div>
CSS Styles
.clip-path-parallax {
height: 500px;
background-image: url('clip-path-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%);
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
In this example, the clip-path
property clips the background in a polygon shape, adding a unique and stylish parallax effect.
Parallax Scrolling with CSS Perspective
Using CSS perspective
can create a 3D parallax effect, adding depth to your website’s design.
HTML Structure
<div class="perspective-container">
<div class="perspective-parallax"></div>
</div>
<div class="content">
<h1>Perspective Parallax Effect</h1>
<p>Using CSS perspective to create a 3D parallax effect.</p>
</div>
CSS Styles
.perspective-container {
perspective: 1000px; /* Creates a 3D space */
height: 500px;
overflow: hidden;
}
.perspective-parallax {
height: 500px;
background-image: url('perspective-background.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
transform: translateZ(-200px) scale(1.2); /* Moves the element back and scales it up */
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
This setup uses CSS perspective
to create a 3D parallax effect, adding depth and dimension to your background.
Combining Multiple Parallax Techniques
Combining various parallax techniques can create a rich and immersive user experience. Here, we’ll combine keyframes, clip-path, and perspective.
HTML Structure
<div class="combined-parallax">
<div class="parallax-layer layer1"></div>
<div class="parallax-layer layer2"></div>
</div>
<div class="content">
<h1>Combined Parallax Effect</h1>
<p>Combining keyframes, clip-path, and perspective for a complex parallax effect.</p>
</div>
CSS Styles
.combined-parallax {
position: relative;
height: 500px;
overflow: hidden;
perspective: 1000px; /* Creates a 3D space */
}
.parallax-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: cover;
animation: parallaxAnimation 10s infinite linear;
}
.layer1 {
background-image: url('layer1.jpg');
clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%);
transform: translateZ(-200px) scale(1.5); /* 3D effect */
}
.layer2 {
background-image: url('layer2.jpg');
clip-path: polygon(0 0, 100% 0, 100% 70%, 0 100%);
transform: translateZ(-100px) scale(1.2); /* 3D effect */
}
@keyframes parallaxAnimation {
0% {
background-position: center 0;
}
50% {
background-position: center 100%;
}
100% {
background-position: center 0;
}
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
In this example, we combine keyframes, clip-path, and perspective to create a complex and visually rich parallax effect.
Parallax Scrolling with CSS Grid and Transform
Using CSS Grid and the transform
property can create sophisticated parallax effects by transforming grid items based on the scroll position.
HTML Structure
<div class="grid-transform-parallax">
<div class="grid-item layer1"></div>
<div class="grid-item layer2"></div>
</div>
<div class="content">
<h1>Grid Transform Parallax Effect</h1>
<p>Using CSS Grid and transform to create advanced parallax effects.</p>
</div>
CSS Styles
.grid-transform-parallax {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr;
height: 500px;
position: relative;
overflow: hidden;
}
.grid-item {
grid-row: 1 / -1;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: cover;
position: absolute;
}
.layer1 {
background-image: url('layer1.jpg');
transform: translateY(calc(var(--scroll) * 0.3));
}
.layer2 {
background-image: url('layer2.jpg');
transform: translateY(calc(var(--scroll) * 0.6));
}
body {
height: 2000px;
}
window.addEventListener('scroll', () => {
document.documentElement.style.setProperty('--scroll', window.scrollY + 'px');
});
This example uses CSS Grid and the transform
property to create parallax effects where grid items move at different speeds based on the scroll position.
Further Advanced Techniques for CSS-Only Parallax Scrolling Effects

Interactive Parallax with CSS Variables and JavaScript
Using CSS variables with JavaScript allows for real-time interactive parallax effects based on user input, such as mouse movement or scrolling.
HTML Structure
<div class="interactive-parallax">
<div class="parallax-layer layer1"></div>
<div class="parallax-layer layer2"></div>
</div>
<div class="content">
<h1>Interactive Parallax Effect</h1>
<p>Using CSS variables and JavaScript for interactive parallax effects.</p>
</div>
CSS Styles
:root {
--mouse-x: 50%;
--mouse-y: 50%;
}
.interactive-parallax {
position: relative;
height: 500px;
overflow: hidden;
}
.parallax-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: cover;
}
.layer1 {
background-image: url('layer1.jpg');
transform: translate(calc(var(--mouse-x) * 0.1), calc(var(--mouse-y) * 0.1));
}
.layer2 {
background-image: url('layer2.jpg');
transform: translate(calc(var(--mouse-x) * 0.2), calc(var(--mouse-y) * 0.2));
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
JavaScript for Interactive Parallax
document.addEventListener('mousemove', (e) => {
const mouseX = e.clientX / window.innerWidth * 100;
const mouseY = e.clientY / window.innerHeight * 100;
document.documentElement.style.setProperty('--mouse-x', `${mouseX}%`);
document.documentElement.style.setProperty('--mouse-y', `${mouseY}%`);
});
In this example, the position of the layers is adjusted based on the mouse movement, creating a dynamic parallax effect that responds to user interactions.
Parallax Scrolling with Background Position
Another method to create parallax effects is by dynamically adjusting the background position based on the scroll position.
HTML Structure
<div class="background-position-parallax"></div>
<div class="content">
<h1>Background Position Parallax Effect</h1>
<p>Using background position for parallax scrolling effects.</p>
</div>
CSS Styles
.background-position-parallax {
height: 500px;
background-image: url('background.jpg');
background-attachment: scroll;
background-position: center 0;
background-repeat: no-repeat;
background-size: cover;
}
.content {
height: 500px;
padding: 20px;
background-color: #fff;
}
JavaScript for Background Position Parallax
window.addEventListener('scroll', () => {
const scrollY = window.scrollY;
document.querySelector('.background-position-parallax').style.backgroundPosition = `center ${scrollY * 0.5}px`;
});
In this setup, the background position is dynamically adjusted based on the scroll position, creating a smooth parallax effect.
Parallax Scrolling with CSS Transforms
Using CSS transforms, you can create advanced parallax effects by rotating, scaling, or skewing elements based on the scroll position.
HTML Structure
<div class="transform-parallax">
<div class="parallax-layer layer1"></div>
<div class="parallax-layer layer2"></div>
</div>
<div class="content">
<h1>Transform Parallax Effect</h1>
<p>Using CSS transforms for advanced parallax scrolling effects.</p>
</div>
CSS Styles
.transform-parallax {
position: relative;
height: 500px;
overflow: hidden;
}
.parallax-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: cover;
}
.layer1 {
background-image: url('layer1.jpg');
transform: translateY(calc(var(--scroll) * 0.2)) rotate(calc(var(--scroll) * 0.1deg));
}
.layer2 {
background-image: url('layer2.jpg');
transform: translateY(calc(var(--scroll) * 0.4)) scale(calc(1 + var(--scroll) * 0.001));
}
body {
height: 2000px;
}
window.addEventListener('scroll', () => {
document.documentElement.style.setProperty('--scroll', window.scrollY + 'px');
});
In this example, layers are rotated and scaled based on the scroll position, creating an engaging and dynamic parallax effect.
Multi-Directional Parallax Scrolling
Combining vertical and horizontal movements with different speeds can create a multi-directional parallax effect.
HTML Structure
<div class="multi-directional-parallax">
<div class="parallax-layer layer1"></div>
<div class="parallax-layer layer2"></div>
</div>
<div class="content">
<h1>Multi-Directional Parallax Effect</h1>
<p>Combining vertical and horizontal movements for advanced parallax scrolling effects.</p>
</div>
CSS Styles
.multi-directional-parallax {
position: relative;
height: 500px;
overflow: hidden;
}
.parallax-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: cover;
}
.layer1 {
background-image: url('layer1.jpg');
transform: translate(calc(var(--scroll-x) * 0.1), calc(var(--scroll-y) * 0.2));
}
.layer2 {
background-image: url('layer2.jpg');
transform: translate(calc(var(--scroll-x) * 0.2), calc(var(--scroll-y) * 0.4));
}
body {
height: 2000px;
}
window.addEventListener('scroll', () => {
const scrollY = window.scrollY;
const scrollX = window.scrollX;
document.documentElement.style.setProperty('--scroll-y', `${scrollY}px`);
document.documentElement.style.setProperty('--scroll-x', `${scrollX}px`);
});
In this setup, layers move in both vertical and horizontal directions based on scroll, creating a complex and engaging parallax effect.
Wrapping it up
Creating CSS-only parallax scrolling effects is a powerful way to enhance the visual appeal and interactivity of your website. By mastering various techniques such as vertical and horizontal parallax, layered backgrounds, CSS variables, SVG filters, CSS keyframes, clip-path, perspective, and combining these effects with JavaScript, you can create rich, dynamic, and immersive web experiences.
These advanced techniques allow you to deliver engaging and visually stunning websites that captivate your audience. Experiment with different methods to find the best combinations for your design and continuously explore new ways to innovate and engage your users. With these skills, you’ll be well-equipped to create exceptional, standout websites in the digital landscape.