How to create a simple parallax effect

Default avatar.
July 18, 2013
How to create a simple parallax effect.

thumbnailAll 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="" /> <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="" /> <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.

Sara Vieira

Sara Vieira is a freelance Web Designer and Developer with a passion for HTML5/CSS3 and jQuery. You can follow her on twitter or check out her website.

Read Next

Surviving the Leap from College to Real-World Design

So, you’ve finished college and are ready to showcase your design skills to the world. This is a pivotal moment that…

20 Mind-Bending Illusions That Will Make You Question Reality

Mind-bending videos. Divisive Images. Eye-straining visuals. This list of optical illusions has it all. Join us as we…

15 Best New Fonts, February 2024

Welcome to February’s roundup of the best new fonts for designers. This month’s compilation includes some innovative…

The 10 Best WordPress Quiz Plugins in 2024

Whether it’s boosting your organic search visibility or collecting data for targeted email marketing campaigns, a great…

20 Best New Websites, February 2024

It’s almost Valentine’s Day, so this latest collection is a billet-doux celebrating the best of the web this month.

Everything You Need to Know About Image Formats In 2024

Always trying to walk the tightrope between image quality and file size? Looking to branch out from JPGs and PNGs this…

The 10 Best Logos of 2023 - Ranked

From vintage aesthetics to innovative new color schemes, we’ve seen a lot of creative logo designs this year. In this…

Exciting New Tools for Designers, February 2024

2024 is well underway and shaping up to be an interesting year. So, to help you keep your focus on working hard and…

The Art of Engineering AI Prompts

In the rapidly evolving world of artificial intelligence, the ability to communicate effectively with AI tools has…

15 Best New Fonts, January 2024

In this month’s roundup of the best new fonts, we find a large number of vintage, retro, and revival typefaces. Is it…

5 Web Design Disasters That Will Ruin Your 2024

Welcome to 2024, where web design is as fast-paced as ever. You know how crucial a killer website is nowadays — your…

14 Best Free SEO Tools in 2024

There is an ongoing debate about whether design is more art or more science. SEO is 100% alchemy; if you know where to…