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

15 Best New Fonts, July 2024

Welcome to our monthly roundup of the best fonts we’ve found online in the last four weeks. This month, there are fewer…

20 Best New Websites, July 2024

Welcome to July’s round up of websites to inspire you. This month’s collection ranges from the most stripped-back…

Top 7 WordPress Plugins for 2024: Enhance Your Site's Performance

WordPress is a hands-down favorite of website designers and developers. Renowned for its flexibility and ease of use,…

Exciting New Tools for Designers, July 2024

Welcome to this July’s collection of tools, gathered from around the web over the past month. We hope you’ll find…

3 Essential Design Trends, July 2024

Add some summer sizzle to your design projects with trendy website elements. Learn what's trending and how to use these…

15 Best New Fonts, June 2024

Welcome to our roundup of the best new fonts we’ve found online in the last month. This month, there are notably fewer…

20 Best New Websites, June 2024

Arranging content in an easily accessible way is the backbone of any user-friendly website. A good website will present…

Exciting New Tools for Designers, June 2024

In this month’s roundup of the best tools for web designers and developers, we’ll explore a range of new and noteworthy…

3 Essential Design Trends, June 2024

Summer is off to a fun start with some highly dramatic website design trends showing up in projects. Let's dive in!

15 Best New Fonts, May 2024

In this month’s edition, there are lots of historically-inspired typefaces, more of the growing trend for French…

How to Reduce The Carbon Footprint of Your Website

On average, a web page produces 4.61 grams of CO2 for every page view; for whole sites, that amounts to hundreds of KG…

20 Best New Websites, May 2024

Welcome to May’s compilation of the best sites on the web. This month we’re focused on color for younger humans,…