Creating a Parallax Website Design using Pure CSS Tutorial

In this tutorial, you will learn to Create a Parallax Website Design using Pure CSS. The tutorial aims to provide students, self-learners, and new programmers with a reference for learning some new things that can be used to build a creative and interactive website design using Cascading Stylesheets Scripts. Here, I will be providing a simple web page source code or script that demonstrates the creation of a simple Parallax Effect/Design.

What is Parallax?

A website's surfing experience can be enhanced by implementing a Parallax Scrolling Effect and making it more engaging and dynamic. In its most basic form, parallax scrolling is a three-dimensional (3D) effect that gives a website additional depth. This kind of effect is commonly used in a single-page website.

Why use Parallax Scrolling Effect on the website?

Parallax Scrolling Effect is not a must-have feature for websites or web application landing pages. This feature is just giving the end-user a better experience while exploring the information on the site page. If you wish to develop a creative and interactive website or landing pages with a faux-3D effect, Parallax may be useful.

How to Create a Parallax Scrolling Effect using Pure CSS?

Using some CSS property and pseudo-element, we can simply achieve the Parallax Scrolling Effect. Here are some of the properties and elements that are useful for creating Parallax Scrolling:

  • ::before pseudo-element
  • ::after pseudo-element
  • background-attachment:fixed
  • position:absoulute

Example

To have a better understanding of how to use the given CSS pseudo-elements and properties to create a Parallax Scrolling Effect on an actual website page.

Interface

Assuming that the following HTML script is the sample landing page of a site. The script contains a simple navigation bar, a footer, and 3 different content sections. The script below loads the Bootstrap 5 Framework and Google Fonts CDN for some of the design of the page which means an internet connection is required in order to browse the page properly. Save the file as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <meta charset="UTF-8">
  4.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>CSS - Parallax</title>
  7.     <link rel="preconnect" href="https://fonts.googleapis.com">
  8.     <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9.     <link href="https://fonts.googleapis.com/css2?family=Amatic+SC:wght@700&display=swap" rel="stylesheet">
  10.     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  11.     <link rel="stylesheet" href="assets/css/styles.css">
  12.     <script src="https://code.jquery.com/jquery-3.6.1.js" integrity="sha256-3zlB5s2uwoUzrXK3BT7AX3FyvojsraNFxCc2vC/7pNI=" crossorigin="anonymous"></script>
  13.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  14. </head>
  15.     <nav class="navbar navbar-expand-lg navbar-dark bg-gradient bg-dark fixed-top">
  16.         <div class="container">
  17.             <a class="navbar-brand" href="./">CSS - Parallax</a>
  18.             <a href="https://sourcecodester.com" class="text-light fw-bolder h6 text-decoration-none" target="_blank">SourceCodester</a>
  19.         </div>
  20.     </nav>
  21.     <div id="main-wrapper">
  22.         <section id="section-1" class="parallax-section">
  23.             <div class="text-center"><b>Welcome to XYZ Site</b></div>
  24.         </section>
  25.         <section id="section-2" class="parallax-section">
  26.             <div class="container h-100">
  27.                 <div class="h-100 w-100 d-flex justify-content-center align-items-center">
  28.                     <h3 class="text-light mx-auto" style="font-family:cursive">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nisi mauris, molestie in dolor quis, varius sodales sapien. Aenean lacinia vel justo at scelerisque. Vestibulum turpis magna, scelerisque id erat eu, elementum egestas sapien. Quisque ultricies orci libero, nec consequat mi egestas vitae.</h3>
  29.                 </div>
  30.             </div>
  31.         </section>
  32.         <section id="section-3" class="parallax-section">
  33.             <div class="text-center"><b>This is a sample Parallax</b></div>
  34.         </section>
  35.     </div>
  36.     <footer class="shadow-top py-4 col-auto bg-dark text-light">
  37.         <div class="">
  38.             <div class="text-center">
  39.                 All Rights Reserved &copy; <span id="dt-year"></span> | <span class="text-light fw-bolder">CSS - Parallax</span>
  40.             </div>
  41.             <div class="text-center">
  42.                 <a href="mailto:[email protected]" class="text-decoration-none text-light">[email protected]</a>
  43.             </div>
  44.         </div>
  45.     </footer>
  46. </body>
  47. </html>

In the HTML script above, we will use the #section-1, #section-2, and #section-3 content elements for Parallax Scrolling Effect.

Cascading Stylesheet (CSS) Script

The following script is the CSS code that creates the Parallax Scrolling Effect of the mentioned page sections. Save this file as styles.css and make sure to load this CSS file in your index.html.

  1.     /** Font Famaily **/
  2.     body #main-wrapper *{
  3.       font-family: 'Amatic SC', cursive;
  4.     }
  5.     html, body{
  6.       height: 100%;
  7.       width: 100%;
  8.       overflow-x: hidden;
  9.     }
  10.    
  11.     /** sections */
  12.     .parallax-section{
  13.       width: 100%;
  14.       height: 80vh;
  15.       position:relative;
  16.     }
  17.    
  18.     /** section2: Paragraph Panel */
  19.     #section-2{
  20.       background-color:#2e2e2e;
  21.     }
  22.    
  23.     /** section 1 & 3: Default Design */
  24.     #section-1, #section-3{
  25.       display: flex;
  26.       align-items: center ;
  27.       justify-content: center;
  28.       font-size: 7rem;
  29.       color: #fff;
  30.       text-shadow:0px 0px 5px #000
  31.     }
  32.     /** section 1: Background*/
  33.     #section-1:before {
  34.       position: absolute;
  35.       content: "";
  36.       width: 100%;
  37.       height: 100vh;
  38.       background-attachment: fixed;
  39.       background-image: url(./../img/bg-1.png);
  40.       background-size: cover;
  41.       background-repeat: no-repeat;
  42.       z-index: -1;
  43.     }
  44.     /** section 3: Background*/
  45.     #section-3:before {
  46.       position: absolute;
  47.       content: "";
  48.       width: 100%;
  49.       height: 100vh;
  50.       background-attachment: fixed;
  51.       background-image: url(./../img/bg-2.png);
  52.       background-size: cover;
  53.       background-repeat: no-repeat;
  54.       z-index: -1;
  55.     }

The script provided above will output a simple website or web application page with a simple parallax scrolling effect. The first and third content section has background images that keep their position while scrolling.

Snapshots

Here are the snapshots of the content section 1 and 3 of the web page.

Section 1

Simple Parallax Effect using Pure CSS

Section 3

Simple Parallax Effect using Pure CSS

DEMO VIDEO

I have also provided the source code zip file of the sample web application that demonstrates the Parallax Effect on this website and it is free to download. You can download it by clicking the Download button located below this tutorial's content.

There you go! I hope this Creating a Parallax Website Design using Pure CSS Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment