Projects

Building a Parallax Scrolling Effect with CSS

Introduction to Parallax Scrolling Effects

Welcome to Building a Parallax Scrolling Effect with CSS! 🚀

In this project, we'll create a visually engaging parallax scrolling effect using CSS. A parallax scrolling effect is a technique where the background moves slower than the foreground, creating a sense of depth and immersion.

Here's what we'll cover in this tutorial:

By the end of this project, you'll have a fully functional and visually engaging parallax scrolling effect. Let's get started by setting up our project files!

Setting Up Your Workspace

Creating Your Project Folder and Files

Before we start coding, let's set up a workspace for our project:

Step 1: Create Your Project Folder

Think of this folder like a container where we'll keep all our project files - just like having a special drawer for your art supplies!

Step 2: Get Your Text Editor Ready

A text editor is like your crafting tool - it's where we'll write our code. We recommend Visual Studio Code because it's free and beginner-friendly!

  1. Download Visual Studio Code from code.visualstudio.com if you haven't already
  2. Install it on your computer
  3. Open VS Code and drag your "parallax-scrolling-effect" folder into the window

Step 3: Create Your Project Files

We need two special files - think of them as two pieces of paper where we'll write different things:

To create these files:

  1. Click "New File" in VS Code
  2. Save it as "index.html"
  3. Create another new file
  4. Save it as "styles.css"

🎯 Success Check: At this point, you should have:

👉 Need Help? If something's not working, try closing VS Code and opening it again, or create the files using the "File → New File" menu.

Setting Up the HTML Structure

Creating the Parallax Scrolling Effect HTML

Before we dive into CSS, let's set up the HTML structure for our parallax scrolling effect:

  1. 1. Open "index.html": Open your "index.html" file in your code editor.
  2. 2. Add the Parallax Scrolling Effect HTML: Copy and paste the following code into your "index.html" file:
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Parallax Scrolling Effect</title>
      <link rel="stylesheet" href="styles.css">
    </head>
    <body>
      <section class="parallax-section" id="section1">
        <div class="content">
          <h1>Section 1</h1>
          <p>This is the first section with a parallax background.</p>
        </div>
      </section>
      <section class="parallax-section" id="section2">
        <div class="content">
          <h1>Section 2</h1>
          <p>This is the second section with a parallax background.</p>
        </div>
      </section>
      <section class="parallax-section" id="section3">
        <div class="content">
          <h1>Section 3</h1>
          <p>This is the third section with a parallax background.</p>
        </div>
      </section>
    </body>
    </html>

Let's understand the HTML structure:

Save your "index.html" file. Now we have the basic HTML structure in place. In the next step, we'll start applying CSS styles to our parallax sections.

Styling the Body

Styling the Body

Let's start by styling the body of our HTML document to ensure a consistent and clean layout.

  1. 1. Open "styles.css": Open the "styles.css" file in your code editor.
  2. 2. Add Body Styles: Copy and paste the following CSS code into your "styles.css" file:
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      background-color: #f4f4f4;
      overflow-x: hidden;
    }

Let's understand the CSS rules we just added:

Save your "styles.css" file. Now, if you open "index.html" in a web browser, you'll see the basic layout applied to your page.

In the next step, we'll style the parallax sections.

Styling the Parallax Sections

Styling the Parallax Sections

Now, let's style the parallax sections to give them a clean and modern look.

  1. 1. Open "styles.css": Open the "styles.css" file in your code editor.
  2. 2. Add Parallax Section Styles: Copy and paste the following CSS code into your "styles.css" file:
    .parallax-section {
      position: relative;
      height: 100vh;
      overflow: hidden;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
    }
    
    .parallax-section::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-size: cover;
      background-position: center;
      background-attachment: fixed;
      z-index: -1;
    }
    
    #section1::before {
      background-image: url('https://via.placeholder.com/1920x1080');
    }
    
    #section2::before {
      background-image: url('https://via.placeholder.com/1920x1080');
    }
    
    #section3::before {
      background-image: url('https://via.placeholder.com/1920x1080');
    }
    
    .content {
      text-align: center;
      z-index: 1;
    }
    
    .content h1 {
      font-size: 3rem;
      margin: 0;
    }
    
    .content p {
      font-size: 1.5rem;
      margin: 10px 0;
    }

Let's understand the CSS rules we just added:

Save your "styles.css" file. Now, if you open "index.html" in a web browser, you'll see the styled parallax sections.

In the next step, we'll add smooth scrolling for a better user experience.

Adding Smooth Scrolling

Adding Smooth Scrolling

Let's add smooth scrolling to enhance the user experience of the parallax effect.

  1. 1. Open "styles.css": Open the "styles.css" file in your code editor.
  2. 2. Add Smooth Scrolling: Copy and paste the following CSS code into your "styles.css" file:
    html {
      scroll-behavior: smooth;
    }

Let's understand the CSS rule we just added:

Save your "styles.css" file. Now, if you open "index.html" in a web browser and scroll, you'll notice the smooth scrolling effect.

In the next step, we'll make the parallax effect responsive for different screen sizes.

Making the Parallax Effect Responsive

Making the Parallax Effect Responsive

Let's ensure our parallax effect looks great on various devices by adding responsive design using CSS media queries.

  1. 1. Open "styles.css": Open the "styles.css" file in your code editor.
  2. 2. Add Responsive Design Styles: Copy and paste the following CSS code into your "styles.css" file:
    @media (max-width: 768px) {
      .parallax-section {
        height: auto;
        padding: 50px 20px;
      }
    
      .content h1 {
        font-size: 2rem;
      }
    
      .content p {
        font-size: 1rem;
      }
    }

Let's understand the CSS rules we just added:

Save your "styles.css" file. Now, if you resize your browser window or view the parallax effect on different devices, you'll see the layout adapt to different screen sizes.

In the next step, we'll add final touches to the parallax effect.

Adding Final Touches

Adding Final Touches

Let's add the final touches to our parallax scrolling effect to ensure it is fully functional and visually appealing.

  1. 1. Open "styles.css": Open the "styles.css" file in your code editor.
  2. 2. Add Final Touches: Copy and paste the following CSS code into your "styles.css" file:
    .parallax-section:hover::before {
      transform: scale(1.1);
      transition: transform 0.5s ease;
    }

Let's understand the CSS rules we just added:

Save your "styles.css" file. Now, if you open "index.html" in a web browser, you'll see the final touches in action.

Congratulations! You've completed the CSS project, "Build a Parallax Scrolling Effect." You've learned how to create a visually engaging parallax scrolling effect using HTML and CSS.

Feel free to continue customizing your parallax effect and exploring more CSS techniques. Happy coding! 🚀