How to Create a Parallax Scrolling Effect in CSS?


By specifying different speeds for background and foreground content, we can create a parallax scrolling effect using CSS. On scrolling up and down, you can easily notice the difference, Mostly, you can find such effect on the home page of a website.

Set the image

First, place and image using the background-image property. With the url, the link of the background image is sourced −

background-image: url("https://www.tutorialspoint.com/static/images/home/coding-groundhero.svg");

Set the height

You need to set a minimum height using the min-height property −

min-height: 500px;

Create the parallax scrolling effect

The parallax scrolling effect can be set using the background-attachment property. The fixed value is set here −

background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;

Set the height for the content

A div is set for the content that would be visible on scroll with the parallax scrolling effect. The height is set using the height property −

.dist {
   height:1000px;
   background-color: rgba(102,234,99,0.5);
}

Example

The following example shows the CSS parallax scrolling effect −

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <style>
      .scrollEffect {
         background-image: url("https://www.tutorialspoint.com/static/images/home/coding-groundhero.svg");
         min-height: 500px; 
         background-attachment: fixed;
         background-position: center;
         background-repeat: no-repeat;
         background-size: cover;
      }
      .dist {
         height:1000px;
         background-color: rgba(102,234,99,0.5);
      }
   </style>
</head>
<body>
   <p>The parallax scrolling effect can be seen when a user scrolls the web page up and down.</p>
   <div class="scrollEffect"></div>
   <div class="dist">
      Sed a porttitor enim. Integer euismod dui massa, at posuere sem fermentum at. Fusce vehicula fringilla tortor, ut mattis sapien tristique ac. Phasellus eleifend malesuada orci, et facilisis arcu egestas vel. Duis in tempus libero. Nunc nibh arcu, tempus quis lectus ut, blandit tincidunt felis. Suspendisse potenti.
   </div>
</body>
</html>

Updated on: 08-Dec-2023

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements