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

Apple Opts for AR over VR at WWDC

An Apple VR headset has been one of the most widely-rumored devices of the last few years, and it was finally settled a…

Exciting New Tools for Designers, June 2023

We’re halfway through 2023 already, and the number of incredible apps, tools, and resources for designers is mounting.

3 Essential Design Trends, June 2023

This month we are focusing on three trends within a bigger website design trend – different navigation menu styles and …

15 Best New Fonts, May 2023

The choices you make when selecting a typeface have more impact on your design than almost any other decision, so it’s …

10+ Best Tools & Resources for Web Designers and Agencies (2023 updated)

Having the ability to envision a tastefully designed website (i.e., the role creativity plays) is important. But being …

20 Best New Websites, May 2023

This month, there are tons of great new agency websites to get excited about. 3D animated prisms are a popular theme, a…

How to Find the Right White Label Website Builder for Your Agency

Web design agencies face a lot of obstacles in closing the deal with new clients. One of the most common ones is the ar…

Exciting New Tools For Designers, May 2023

There are hundreds of new tools for designers and developers released each month. We sift through them all to bring you…

3 Essential Design Trends, May 2023

All three of the website design trends here mimic something bigger going on in the tech space, from a desire to have mo…

10 Best AI Tools for Web Designers (2023)

It’s time to stop worrying if AI is going to take your job and instead start using AI to expand the services you can of…

10 Best Marketing Agency Websites (Examples, Inspo, and Templates!)

Marketers are skilled in developing strategies, producing visual assets, writing text with high impact, and optimizing …

15 Best New Fonts, April 2023

Fonts are a designer’s best friend. They add personality to our designs and enable fine typography to elevate the quali…