How to create a simple parallax effect

By Sara Vieira Posted Jul. 18, 2013 Reading time: 4 minutes

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.



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>

<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>
<li>Web applications</li>
<li>Web development – HTML5, CSS, jQuery</li>
<li>Content Management Systems</li>
<li>Responsive Web Design</li>
<li>Business cards</li>
<li>Letterheads and compliment slips</li>
<li>Appointment cards</li>
<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>

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 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%;
  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:


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.



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.