Header Image Parallax Scrolling Effect with CSS

Categories: Code

Tags: , ,

I am going to create a parallax scrolling effect using CSS background-image position.

When we create a parallax image banner with CSS we usually use ‘background-attachment: fixed;’ style. I like the fixed image effect but the fixed image is not as effective as the scrolling image at a slower pace giving the web page an instant depth that I absolutely love.

This recipe requires ingredients of HTML, CSS and a pinch of JS, and only works when the header image is positioned at the top of the page. Let’s start cooking.

Firstly we need to create a sample web page with a large header image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<div class="sample-header">
  <div class="sample-header-section">
    <h1>Scroll down to see the parallax effect</h1>
    <h2>Background landcape scrolls with its own depth </h2>
  </div>
</div>
<div class="sample-section-wrap">
  <div class="sample-section">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
      quis, sem. Nulla consequat massa quis enim. </p>
    <p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean
      vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam
      ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. </p>
    <p>Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut
      libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. </p>
    <p>Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut,
      mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. </p>
    <p>Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet
      nec, imperdiet iaculis, ipsum. </p>
    <p>Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut.</p>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
      quis, sem. Nulla consequat massa quis enim. </p>
    <p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean
      vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam
      ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. </p>
  </div>
</div>

Next, let’s add CSS to the HTML document.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
.webmadewell {
  background-color: white;
}
body {
  margin: 0;
}
.sample-header {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  background-image: url(https://webmadewell.com/wp-content/uploads/2018/01/img-bg-sample-parallax-header.jpg);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}
.sample-header::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: MidnightBlue;
  opacity: 0.3;
}
.sample-header-section {
  position: relative;
  padding: 15% 0 10%;
  max-width: 640px;
  margin-left: auto;
  margin-right: auto;
  color: white;
  text-shadow: 1px 1px 4px rgba(0, 0, 0, 0.5);
  font-family: 'Montserrat', sans-serif;
}
h1 {
  font-weight: 500;
}
h2 {
  font-weight: 400;
}
.sample-section-wrap {
  position: relative;
  background-color: white;
}
.sample-section {
  position: relative;
  max-width: 640px;
  margin-left: auto;
  margin-right: auto;
  padding: 40px;
}

We have all the elements laid out on the plate, but it doesn’t look quite there yet. The paragraphs are laid over the header image. Why? Because the ‘.sample-header’ class has ‘position:fixed;’ (CSS line 08) so it doesn’t push down the element below. We can amend it by giving ‘.sample-section’ element a ‘margin-top’. But we need to do it with jQuery, so the ‘margin-top’ value can change as we scroll the screen.

1
2
3
4
5
6
7
8
function parallax_height() {
  var scroll_top = $(this).scrollTop();
  var sample_section_top = $('.sample-section').offset().top;
  var header_height = $('.sample-header-section').outerHeight();
  $('.sample-section').css({ 'margin-top': header_height });
  $('.sample-header').css({ height: header_height - scroll_top });
}
parallax_height();

It looks good as planned, but when we scroll the window, the header image is still fixed, not moving. To add a parallax scrolling effect to the header image, we need to add ‘scroll’ event to the ‘parallax_height’ function.

9
10
11
$(window).scroll(function() {
  parallax_height();
});

Finally, we have a beautifully scrolling header image. You can also see it in action at my CodePen page.