Creating a Parallax Effect using HTML, CSS, and JavaScript

In this comprehensive tutorial, I will guide you step-by-step on how to create a highly creative and visually attractive website feature known as the Parallax Effect. The parallax scrolling effect is a popular web design trend that adds depth, motion, and an engaging user experience to your website. By the end of this tutorial, you will learn how to implement stunning parallax animations that captivate visitors, improve your site's visual appeal, and boost overall engagement. Whether you're a beginner or an experienced web developer, mastering the parallax effect will take your website design skills to the next level.

What is Parallax Effect?

The parallax effect is a powerful visual technique where objects positioned closer to the viewer move faster than those positioned farther away, creating a stunning illusion of depth and dynamic 3D movement. This technique has become increasingly popular in modern web design for enhancing user experience and making websites more interactive and engaging.

In the context of web development, the parallax effect typically occurs when:

  • The background image or graphic moves at a slower pace than the foreground content as users scroll down the page, creating a layered visual effect.
  • This movement generates a sense of immersion, making the website feel more dynamic, visually appealing, and captivating for visitors.

By implementing the parallax scrolling effect, web designers can transform ordinary pages into vibrant, story-driven experiences that not only look professional but also keep users engaged longer.

In this tutorial, I will provide the complete HTML, CSS, and JavaScript code needed to create a simple yet visually appealing website featuring the Parallax Effect. In the source code, I have used two sample images to enhance the design. One of these images contains an isolated object extracted from the original image, which overlays both the background and the section content. This layering technique adds depth and realism to the parallax effect.

Beyond the parallax scrolling effect, I have also incorporated smooth animations to make the website more dynamic and visually engaging. By following this tutorial, you'll learn how to create an interactive scrolling experience that enhances user engagement and improves the overall aesthetics of your web pages.

Let's start the coding.

Step 1: Creating the HTML file

First, let's create a new HTML file and save it as index.html. This file will contain all the essential elements needed for building our sample website with a Parallax Effect. It will include the different sections and their respective content that structure the web page layout. Additionally, the CSS and JavaScript (JS) files will be linked within this HTML file to style the website and add interactive functionalities such as the parallax movement effect.

Here's the index.html code:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Simple Parallax Effect</title>
  7.     <link rel="stylesheet" href="style.css">
  8. </head>
  9.  
  10.     <!-- First Section -->
  11.     <section>
  12.         <h1 id="app-title">Simple Parallax Effect</h1>
  13.     </section>
  14.  
  15.     <!-- Second Section -->
  16.     <section></section>
  17.    
  18.     <script src="script.js"></script>
  19. </body>
  20.  
  21. </html>

Step 2: Creating the CSS file

Next, let's create a new Cascading Style Sheets (CSS) file for the website and save it as style.css. This stylesheet is responsible for designing the visual appearance of the elements, structuring the layout, and enhancing the overall look and feel of the website. Within style.css, we will define styles for sections, images, text, and other components to create a cohesive and attractive design.

Additionally, the background images that will be displayed on the website are loaded through this CSS file. The styling script will also include the necessary code to achieve the Parallax Effect and add extra animations, making the website more dynamic, interactive, and visually appealing for users.

Here's the CSS script:

  1. @import url('https://fonts.googleapis.com/css2?family=Eagle+Lake&display=swap');
  2.  
  3. * {
  4.     box-sizing: border-box;
  5.     font-family: "Eagle Lake", serif;
  6.     font-style: normal;
  7. }
  8.  
  9. html,
  10. body {
  11.     margin: unset;
  12.     padding: unset;
  13.     width: 100%;
  14.     max-width: 100%;
  15.     min-height: 100%;
  16.     height: 100%;
  17.     overflow: auto;
  18.     overflow-x: hidden;
  19. }
  20.  
  21. /* Parallax Title */
  22. #app-title {
  23.     font-size: 4rem;
  24.     font-weight: bolder;
  25.     text-align: center;
  26.     color: #fff;
  27.     text-shadow: 6px 6px 11px #000;
  28.     animation: slideUp 1s linear infinite;
  29.     animation-play-state: paused;
  30.     animation-delay: calc(var(--scroll) * -1s);
  31.     animation-iteration-count: 1;
  32.     animation-fill-mode: both;
  33.  
  34. }
  35.  
  36. /* Content Sections */
  37. section{
  38.     position: relative;
  39.     width: 100%;
  40.     height: 100%;
  41.     display: flex;
  42.     align-items: center;
  43.     justify-content: center;
  44. }
  45.  
  46. /* First Section */
  47. section:nth-child(1)::before {
  48.     content: "";
  49.     position: absolute;
  50.     top: 0;
  51.     left: 0;
  52.     width: 100%;
  53.     height: 100%;
  54.     background-image: url(./imgs/nigh-savannah-landscape.jpg);
  55.     background-attachment: fixed; /* Creates the Parallax Effect */
  56.     background-size: 100% 100%;
  57.     background-repeat: no-repeat;
  58.     background-position: center center;
  59. }
  60.  
  61.  
  62. section:nth-child(1)::after {
  63.     content: "";
  64.     position: absolute;
  65.     top: 0;
  66.     left: 0;
  67.     width: 100%;
  68.     height: 100%;
  69.     background-image: url(./imgs/nigh-savannah-landscape-2.png);
  70.     background-size: 102% 100%;
  71.     background-repeat: no-repeat;
  72.     background-position: center center;
  73.     transform: translateX(10px);
  74. }
  75.  
  76. /* Additional Animation/Effect for the foreground image */
  77. section:nth-child(1)::after{
  78.     animation: shrink 1s linear infinite;
  79.     animation-play-state: paused;
  80.     animation-delay: calc(var(--scroll) * -1s);
  81.     animation-iteration-count: 1;
  82.     animation-fill-mode: both;
  83. }
  84.  
  85. /* Second Section */
  86. section:nth-child(2)::before {
  87.     content: "";
  88.     position: absolute;
  89.     top: 0;
  90.     left: 0;
  91.     width: 100%;
  92.     height: 100%;
  93.     background-image: url(./imgs/night-landscape-mountain.jpg);
  94.     background-attachment: fixed; /* Creates the Parallax Effect */
  95.     background-size: 100% 100%;
  96.     background-repeat: no-repeat;
  97.     background-position: center center;
  98. }
  99.  
  100. section:nth-child(2)::after {
  101.     content: "";
  102.     position: absolute;
  103.     top: 0;
  104.     left: 0;
  105.     width: 100%;
  106.     height: 100%;
  107.     background-color: #ffffffd6;
  108. }
  109.  
  110. /* Additional Animation/Effect for the foreground image */
  111. section:nth-child(2)::after{
  112.     animation: reveal 1s linear infinite;
  113.     animation-play-state: paused;
  114.     animation-delay: calc(var(--scroll) * -1s);
  115.     animation-iteration-count: 1;
  116.     animation-fill-mode: both;
  117. }
  118.  
  119. @keyframes slideUp {
  120.     30% {
  121.         transform: translateY(-10px);
  122.     }
  123.     100% {
  124.         transform: translateY(1000px);
  125.     }
  126. }
  127.  
  128. @keyframes shrink {
  129.     to {
  130.         transform: scaleX(1.2);
  131.     }
  132. }
  133.  
  134. @keyframes reveal {
  135.     to {
  136.         opacity: 0;
  137.     }
  138. }

Step 3: creating JS file

Lastly, let's create the JavaScript file for the website and save it as script.js. This file primarily handles the interactive behavior of the site during user scroll actions. Within script.js, we will add an event listener that tracks the user's scrolling activity. As the user scrolls, the event listener updates a custom property—referred to as the scroll—on the document body.

This dynamic scroll variable is crucial for triggering and controlling additional animations throughout the website, making the scrolling experience smoother, more responsive, and visually engaging. Although simple, this JavaScript file plays an important role in bringing the Parallax Effect and extra animations to life.

  1. document.body.addEventListener(
  2.     "scroll",
  3.     () => {
  4.         // Update Scroll Variable for CSS
  5.         document.body.style.setProperty(
  6.             "--scroll",
  7.             document.body.scrollTop / document.body.offsetHeight
  8.         );
  9.     },
  10.     false
  11. );

There you have it! You can now try to browse the website using your preferred web browser. The result of the website must show the Parallax Effect and some of the animation while you scroll the page.

Snapshots

Creating a Parallax Effect using HTML, CSS, and JavaSctipt

The image above shows how the website would looks like when it is loaded.

Creating a Parallax Effect using HTML, CSS, and JavaSctipt

The image above illustrates the transition of the website from the first section to the second section.

I have also provided the complete source code of the sample website, including the sample images used in the project. The source code zip file is available for free download on this website. Feel free to download it and modify the code to further enhance your knowledge about creating a Parallax Effect. The download button is located below this article.

There you go! I hope that this Parallax Effect Tutorial using HTML, CSS, and JavaScript will help you with what you are looking for and useful for your current or future website development.

Explore more on this website for more Tutorials, Free Source Codes, and Articles covering various programming languages.

Happy Coding =)

Add new comment