Subscribe

CSS only Parallax Scrolling

โœ๏ธ

Learn how to create a CSS only Parallax effect with background attachment. See the code examples in the Codepen!

15 Apr, 2020 ยท 3 min read

You have probably seen a parallax effect before. A parallax effect is when you scroll and it looks like the background image is sitting in one place and is unaffected by the scroll.

Some people take parallax scrolling over the top by adding multiple layers and create the most stunning effects!

For today's tutorial, we will learn how to add a parallax scrolling effect with pure CSS. It is the simplest example possible - to get you included in adding this wonderful effect to your website or project.

Parallax scrolling with CSS only

Yes, it is possible to do parallax with CSS only.

For the HTML, let's use the following setup to keep it simple:

<section class="header">
  <h1>Welcome! ๐Ÿ‘‹</h1>
  <h2>How are you?</h2>
</section>
<section class="bg-grey">
  <h2>Demonstrating parallax</h2>
  <br />
  <div>
    <a href="https://daily-dev-tips.com/" target="blank">Blog</a>
    <a href="https://twitter.com/DailyDevTips1" target="blank">Twitter</a>
  </div>
</section>

We included 2 sections which we will size at 100% of the viewport. The first one will contain the parallax image background.

For the CSS we are going to use the following:

section {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
section.header {
  background-image: url('https://images.unsplash.com/photo-1564415637254-92c66292cd64?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=975&q=80');
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  color: #fff;
}
.bg-grey {
  background: #efefef;
}

As you can see we make every section width: 100vw (100% of the viewport width) and height: 100vh (100% of the viewport height).

Next, we make this a flex container to center the headings and buttons inside of it. (Read more on alignment here)

Then for the header section, we add the parallax background and center it using background-position. Then I learned to always include background-repeat: no-repeat but you can omit this. We set background-size: cover this will make the background expand over the whole section.

And for the magic part ๐ŸŽฉ, we set background-attachment: fixed. This is what will make give the background a parallax scrolling effect!

See the Code examples in this Codepen

See for yourself in this pure CSS parallax example:

See the Pen Parallax demo by Chris Bongers (@rebelchris) on CodePen.

Browser Support

background-attachment has quite a good browser support, just on mobile it's a bit glitchy, but you can use other queries to fix this.

classList browser support

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

Bringing perspective to CSS

7 Aug, 2022 ยท 2 min read

Bringing perspective to CSS

Creating a 3D Cylinder shape in CSS

29 Jul, 2022 ยท 3 min read

Creating a 3D Cylinder shape in CSS

Join 1829 devs and subscribe to my newsletter