Navigation

How to create a simple parallax effect

By Sara Vieira | Web Development | Jul 18, 2013

All over the web, sites are springing up, that use what is known as a parallax scroll. In essence, a parallax scroll is when content scrolls at different speeds, creating a sense of perspective and therefore depth.

It’s an appealing effect, and can be applied to as many layers as you like. In this article I’m going to introduce the basic principles by showing you how to build a simple two layer parallax effect.

 

The HTML

To start off we need some HTML, we’ll include some filler text to be placed within a section and then another <div> that will hold our background:<div class=”bg”> </div>

<section>
<h1>Home page</h1>
<p>We are a fairly small, flexible design studio that designs for print and web. We work flexibly with clients to fulfil their design needs. Whether you need to create a brand from scratch, including marketing materials and a beautiful and functional website or whether you are looking for a design refresh we are confident you will be pleased with the results.</p><p>We offer the following services:</p>
<ul>
<li>Branding</li>
<li>Logos</li>
<li>Websites</li>
<li>Web applications</li>
<li>Web development – HTML5, CSS, jQuery</li>
<li>Content Management Systems</li>
<li>Responsive Web Design</li>
<li>Illustration</li>
<li>Business cards</li>
<li>Letterheads and compliment slips</li>
<li>Flyers</li>
<li>Mailers</li>
<li>Appointment cards</li>
</ul>
<h1>Sub page</h1>
<p>Before you choose us to take on your project you will probably want to know a bit more about us, so meet the team:</p>
<img alt="" src="http://lorempixum.com/500/600" /> <p>Ross has over 10 years experience in the industry. He is our Creative Director, digital designer, web designer and front-end developer. He is also pretty good with a sketchbook. Before starting the company Ross worked as a designer and studio manager for a design house who boasted a number of big name clients. Ross has brought his vast experience from this role to the work he does now.</p>
<p>Monica is Ross’ sister, our Art Director and specialises in graphic and print design. She has also worked with some big names and her designs have won her a number of industry awards.</p> <p>Rachel and Chandler are our Junior Designers. Rachel is a web designer with knowledge of HTML and CSS and supports Ross on projects. Chandler has just finished his Graphic Design degree and enjoys continuing to learn from Monica and building his experience.</p> <img alt="" src="http://lorempixum.com/500/600/sports" /> <p>Joey and Phoebe focus on bringing new business to the company. They have won a number of big clients recently and both also have qualifications in project management to ensure that the projects run smoothly from start to finish.</p>
</section>

This is all the HTML we’re going to need. All of the text is just to ensure that we cover the whole page so that scrolling is necessary. Let’s more on to the CSS:

 

The CSS

The CSS required to create a parallax effect is actually pretty simple if you understand why it’s written the way it is. We first need to set the background image of the .bg div and then we need to stop the div from scrolling because the scroll action will be applied by jQuery; so we need to set its position to fixed. Next we set the width to 100% and the height to 300% to make sure the div is bigger than the actual screen. We position it at the top left and finally give it a z-index of -1 to make sure it is rendered underneath the text.

.bg {
  background: url('bg.jpg') repeat;
  position: fixed;
  width: 100%;
  height: 300%;
  top:0;
  left:0;
  z-index: -1;
}

This is all the CSS we need for the bg div, now we just need to style the rest of our page (although this is entirely optional, it doesn’t affect the parallax scroll):

section {
  color: #fff;
  font-family: arial;
  width: 500px;
  margin: auto;
  line-height: 20px;
  font-size: 16px;
}

Try scrolling the page now and you’ll see that the text scrolls but the background stays fixed, we’re going to change that with our jQuery:

 

The jQuery

What we want the jQuery to do is check how far the user has scrolled and move the background at a slower speed. We’re going to create a function called parallax and create a variable that will hold the value of the pixels the user has scrolled:

function parallax(){
    var scrolled = $(window).scrollTop();

Now, to make the background scroll at the same speed as the text we set the top value of the div to be the negative value of the scroll, then close the function. Like so:

    $('.bg').css('top', -(scrolled) + 'px');
}

However the point of parallax scrolling is to move at a different speed, so to adjust the speed we multiply the value by a fraction, for example 0.2 for 20%:

function parallax(){
    var scrolled = $(window).scrollTop();
    $('.bg').css('top', -(scrolled * 0.2) + 'px');
}

There’s just one more thing to do to get the effect running and that’s call the function every time the scroll event is triggered:

$(window).scroll(function(e){
    parallax();
});

Having done this, our code is complete. If you test the file you’ll see it working. To change the speed, we need to change the fraction in the function; smaller fractions reduce the speed and higher fractions increase it. You can see the final result of this code in this pen I created.

 

Conclusion

As you can see creating a parallax effect isn’t as hard as you may have expected. Of course, this is just a simple example, but you can build on this to create any kind of complex parallax effect that you like.

 

Have you used a parallax effect in a project? Do you have a better method? Let us know in the comments.

Featured image/thumbnail, sliding perspective image via Shutterstock.

Share this post
Comments (no login required)
  • Laura Montgomery

    Nice and simple, thanks for sharing :)

  • Square Gate

    Parallax has definitely become more popular, this is a really nice and easy to use example to get people started. Thanks!

  • chrisarnoldco

    Hi Sara, good article thanks, just recreated this myself as not used the parallax effect yet. Just so you know there’s one to many closing brackets in your jQuery function call in the article (CodePen demo is fine though).

    • sheriffderek

      I think that second o from “too” just snuck off over that the code and was trying to blend in. Lol

  • ConstantStrategies

    Nice.. But, is there anyway to do this without JQuery, only CSS?

    • http://alexmuraro.me/ Alex Muraro

      Dont think so, because you need to know the amount of scrolling that’s been done and change scroll ratio of the background as opposed to the foreground

    • Peter Chon

      considering you need to capture the event (scroll) there is no way to do it using css.

    • Davey
  • seocognition

    HTML and CSS ONLY? Where and how? Can you share some examples of doing parallex like effects without using javascript?

  • chrisblakejones

    Nice and simple solution. Thanks for sharing.

  • Francis Derequito

    Thanks for the great article, now everything about parallax-effect is clear on what and how it is done in simple ways…

    :)

  • Daniel Wiklund

    I would highly recommend that you use “translate3d” instead of “top” to offset the element you want to apply the parallax effect to. Of couse you would have to make some fallback code (where you could use “top”) for older browsers. But “translate3d” offers much better performance then offsetting it with the “top” value.

  • Yurii Iefimov

    Nice article :) thx

  • http://www.blackbookoperations.com/ Black Book Operations

    easy, learnful, easy

  • http://www.blackbookoperations.com/ Black Book Operations

    Nice one as well… Now let’s make it way more complicated and use several layers of bg images all scrolling in diff directions, lol, just because we can!

  • iyakiggun

    Great and simple TIP.

  • http://tracywongphoto.com/ Tracy Wong

    Great and seems simple enough! Will try it out soon for my website.

  • http://danmathisen.com/ dmathisen

    Any reason or need for the ‘e’ here?
    $(window).scroll(function(e){
    parallax();
    });
    In any case, perfect and simple tutorial. Thanks!

  • Aldi Unanto

    Thanks for share. Great and simple trick to use parallax!

  • Dan

    About time I gave this a go, thanks!

  • Ryan

    will this work even on a mobile phone if I have a responsive website?

  • http://www.technology4turnover.co.uk/ Jeremy Griffin

    Thanks for this great, clear and concise intro. Just got around to investigating how to bake this technique into concrete5 theme. This start point has enabled a working prototype that I’m going to build on. Thanks for sharing.

  • Cesar

    instead of using top you can use background-position-y so It would bug out when you do background size: cover.

    Here how I did.

    function parallax(){
    var scrolled = $(window).scrollTop();
    $(‘.parallax’).css(‘background-Cposition-y’, -(scrolled * 0.4) + ‘px’);
    }

  • pradyut

    my background is fixed, its not scrolling may be my jquery is not working. Please help me out.

    • Kristin

      Try adding :

  • Name

    I’m really irritated… the jquery is not working :/

  • lauren

    hi! thanx for your tuto. can you explain me how to make this in wordpress please?

  • http://riastuff.com/ Ria Parish

    Wow, it finally makes sense to me. I really didn’t get the basics before, but you’ve really simplified it. Beautifully demonstrated, thank you.

  • Andy Soesilo

    nice article…thanx for sharing :)
    btw are you Friends’s fans too?? same with me :D

  • Rayyan

    i wrote the code right after the and it didn’t work. where am i supposed to write the code so that the parallax effect works ? (I’m in dying need to know because my deadline is in 2 weeks and i need to have my website done !!!)

  • http://jamiegoodwin.net/ Jamie Goodwin

    Simplest tutorial I’ve found yet! Brilliant :-)

  • Arkymedes

    If I’ m not mistaken, this works only until jQuery version 1.9.1

  • http://nomidesigns.com/ nomi

    Great post! Nice and easy. Thanks a lot

  • Truthonaplate

    Beautiful as you are :)

  • Zeus McLaren

    Demo. Demo. Demo before the code.

    • http://scottohara.me/ Scott O’Hara

      She did provide a demo.

      The link is right before the “Conclusion”

  • nada

    nice , but I have a problem ,the example not working!

  • Sam Cavender

    Great help, thank you! I was getting stuck trying to customise a parallax theme and the code was all over the place. This clarified the theory totally so I learnt what I caould chop :)

  • guest

    i want the same to work for different sections but its not working

  • guest

    please some one help me out

  • Denise Chiang

    Parallax is very trendy. If you are learning web development you can’t escape parallax. Thanks for this step-by-step tutorial!

  • Gabriel Glauber

    Very nice and userfull examample! I search every ware and your is the best. Tanks

  • Rana Faiz

    Awesome tutorial. Really nice for starters

  • http://miladonintendo.blogspot.com/ Andalbor

    Simply amazing! Thanks for sharing this! I had a similar idea on my mind, but never realized how to develop it :)

    I made a little change to your code that I would like to show you: I use “background-position” in the jQuery function instead of “top”, because with “top” the function moves the complete div, but with “background-position” it moves just the background image. Thanks to that, I can use “height: 100%” in the “bg” div, so there is no problem with the height of the webpage :)

    Again, thank you!

  • Rama Ibrahim

    this is great.. i hate complicated jquery library when all that i want is just a simple scrolling effect.. cheers sara

  • refaelok

    best example thanks !