# Jazz up a Static Webpage with Subtle Parallax

It's been one of the biggest web design buzz words for a while now, so today we're going to try our hand at a bit of Parallax. We're going to put together a simple responsive layout, then with the help of skrollr.js, we're going to apply a subtle parallax effect to the header.

## 1. Our Project

This tutorial will cover a few noteworthy aspects, so pay attention:

• Firstly, we're going to use a mobile-first fluid adaptation of the skeleton boilerplate.
• We'll need to grab an image, I'll be using “morning coffee” from Photodune.net.
• We'll grab a copy of Alexander Prinzhorn's skrollr.js to help with our parallax effect.
• In order to optimize my CSS I'll be using a Sass workflow. Don't panic! I won't actually be using any Sass syntax to speak of, just compiling all my stylesheets, giving me one tiny minified CSS file. If you don't want to follow that aspect of the tutorial, don't worry, you can use whatever approach you're comfortable with. However, if you haven't yet played with Sass, see this as the perfect chance to get started without actually using Sass (if that makes sense..)

## 2. Files 'n' Folders

Okay then! Let's kick off our project with some bare bones files. This is what you'll need to begin with:

Our index.html is empty at the moment, then we have an img directory with our coffee image within it, a css directory with a reset file plus our fluid grid and an empty styles.css, patiently awaiting our input. Lastly, we have a js directory with the skrollr.js file downloaded from GitHub.

## 3. Fleshing Out the Bones

Our index.html is going to begin with some very familiar elements:

I won't bore you with all these bits and pieces, but it is worth noting that the viewport meta tag is present, we've given the document a (nonsensical) title, linked to some favicons - and that we've pointed to just one css file: styles.css.

## 4. Mashing Our CSS Together

One tiny stylesheet to rule them all.

When I first talked about the files and folders I mentioned three stylesheets; normalize.css (to responsibly reset our styles), fluid_skeleton.css (which I've adapted myself from the skeleton boilerplate) and the actual styles.css. So why have we only linked to one in our HTML file?

Well, we're going to use this as an introduction to Sass - without actually using any Sass.

By using the .scss file extension instead of .css, we can pull them all into one file and minify the whole lot. Begin by renaming the CSS files like so:

• normalize.css → _normalize.scss
• fluid_skeleton.css → _fluid_skeleton.scss
• styles.css → styles.scss

That was easy, right? Notice the underscores on the first two, that prevents those files from being compiled into their own css equivalents. Instead, we're going to import them into our styles.scss, just as we would using a normal css @import rule:

Now when we compile we'll be given a styles.css file built up from all the rules in our .scss files. We only need to deploy this single file to our web server, saving our site two requests and several Kbs. Good job :)

### Compiling Sass

In order to actually compile the files as described above, we're going to need a compiling application (unless you fancy using the command line?). There are a few applications which will do the job for you, I use CodeKit (because it's awesome) but CompassApp will serve you just as well, plus it's available on Windows.

Setting up our project is as easy as dragging the project folder into the compiler. CodeKit is now watching all the files within that folder. Whenever I hit "save", no matter which SCSS file I'm editing, CodeKit will recompile all the pieces, churning out our updated CSS and, if our page is open in a browser, refreshing that for us too.

Even if you're not ready to look further into Sass this is an ideal workflow.

## 5. A Bit More Content

Let's now fill up the index.html file, after which we can really begin on the styling. As mentioned, I'm using fluid_skeleton.css as a basis for the responsive grid. It's a great, uncomplicated basis from which to kick off a simple layout. All it requires is that we use its grid structure to build our layout.

We need:

• A band div - which will stretch the full width of the screen.
• Within each of them we need a .container div - which holds all the grid columns.
• Then .column divs, each one having a second class depending on the width. .eight for example, totalling sixteen all together.

For example, we can build a quick four column layout like so:

The stylesheet dictates that these divs are all 100% wide, stacked neatly ontop of one another, until the viewport hits 767px wide. At that point everything shifts and these four columns will spread across the screen as nature intended.

This quick wireframe should give you an idea of how we're going to layout our structural markup.

And if you're after a quick copy/paste, this will help you out:

For more information on how Skeleton is put together take a look at Building a Responsive Layout With Skeleton: Starting Out.

## 6. Filler Text

It's always easier to see what you're doing when you have a bit of dummy text to work with, so throw a heading and a link into the header region at the top:

Then turn your attention to the columns and the footer. I've grabbed a load of filler text from T'Lipsum "A Yorkshire Lorem Ipsum Generator for Yorkshire Folks" (because I'm a proud Yorkshireman) beat that!

To give you an idea, the main body (our twelve column area) begins like this:

Stick some links in the footer and we're done here!

## 7. Kicking Off the Styling

Before we go any further I want to grab some fonts. Head on over to Google Web Fonts and take a look at PT Sans and PT Serif. They're a great duo, so add them both to your collection and gather the stylesheet link.

To save time, this is the link I was given, having selected the 400 and 700 weight versions of each:

You can paste this underneath our other imports in styles.scss. When compiled, the contents of this CSS file won't actually be dragged into our minified CSS, but the @import rule will remain and the font will be used all the same.

Now I'll set PT Sans as our base font, setting the font-size to 100% of the browser's default, with its line-height at 1.5em:

## 8. Taking Styling Further

Much of the styling we apply to this page is fairly generic. Colors and sizes for the typography, padding on the bands and so on. Pull the source files apart and you'll find the contents of styles.scss self-explanatory.

However, let's focus for a moment on the header area. In its mobile-first state, we want to give it a brown background. Only once the viewport is larger will we bring in the coffee image (to spare mobile users the bandwidth). This is what the styles for the header band look like now:

The padding top and bottom has been applied as a percentage of the browser width. This means that the header area grows in height as the browser window expands. It's quite a nice effect and keeps the fold (wherever that is these days) proportional to the browser window size.

Now let's style the heading we have within it.

We'll use the serif variant of PT for our headings, which pairs well with the body copy being in sans.

Lastly, we'll style our anchor to make it stand out:

Straightforward stuff, all em-based to ensure that it can grow and shrink if needs be - and if you're really going to embrace Sass you could make all still styling even slimmer! For now though, this is what you should have:

## 9. Throw in a Media Query

Having built our basic mobile experience, let's now embellish things for larger screens. How you choose to arrange your media queries is up to you, but as this is a fairly uncomplicated stylesheet I'm just going to pile all my extra rules into one media query at the end of styles.scss.

Let's add some rules which will kick in on screens larger than 767px. This breakpoint is fairly arbitrary and just happened to suit the design when I first built it. What better way to choose a breakpoint?!

There actually aren't many styles to add here, we just want to boost the font-size used throughout the document, make our button a bit more prominent and add a background image to the header area:

On larger screens we should now have something like..

## 10. Introducing skrollr.js

Currently, when you scroll down the page our header disappears from view much as you would expect. Let's now get to the basis of this whole exercise; parallax. You should already have downloaded skrollr.js from GitHub and placed it in the js folder.

Next, we need to reference it in our html, so place this link at the bottom, before the closing </body> tag:

This is a vanilla JavaScript plaything, so we've no need to pull in jQuery beforehand. Next, we need to call skrollr, which we do like so:

There are a number of options which we can toy around with, all of which have settings we can throw in between those curly braces, but for now we'll leave things exactly as they are.

## 11. Altering the Markup

In its current stage of development, skrollr requires us to butcher our markup a little, adding rules inline on our elements. Skrollr's maker Alexander Prinzhorn is busy with a project which separates styles from markup, but it's still in progress.

Anyway, I'm making it sound worse than it is, we're simply required to add our keyframe styling to data attributes on the elements we're targeting. For example, let's move the background image position of our header, depending on the scrollbar position.

We add our first keyframe styling like so:

Skrollr uses data- and then a value which represents the pixel position of the scrollbar. We then add some styling just as we would with CSS. We've literally just specified that when the scrollbar is at 0px (the top of the page) we want the background-position of this element to be 0px 0px.

We can then add as many other keyframes as we like, so let's throw another data-attribute in, directly after our first.

This then says; when the scrollbar has moved 500px down, we want the background-position to be 0px -250px (ie: 250px upwards). Skrollr will animate the process between these two keyframes, so we get a smooth transition.

Bingo! Our first parallax effect.

## 12. A Second Effect

We've acheived a very simple parallax effect which adds an element of dynamism whenever the user is scrolling downwards. We can apply whatever CSS rules we want with skrollr, so let's give our header region one last flourish.

We're going to add two keyframes; one for the initial state (0px) and another for when the scrollbar reaches 180px.

These, as you can see, dictate the opacity, another familiar CSS property. As our scrollbar moves down, before the header has quite left the page, the content within this div will smoothly fade away. A simple effect, but one which subtly influences the user experience of this page. Things like this should be used responsibly!

## 13. Extra Settings

Skrollr comes packaged up with several extra options, depending on how confident you're feeling. All of these settings can be passed to skrollr by means of key-value pairs like so:

For example, this instruction tells skrollr not to force the height of the document. By default skrollr will force the document to be at least as high as the highest keyframe you've defined. In our case, our furthest scrollbar position was specified as 500px, whilst our page is waaa-aay taller than that. We therefore didn't need to think about overriding the height forcing.

Another thing you may want to do is remove the smooth scrolling. Try our parallax effect once more; you see how the background image position accelerates and decelerates? That prevents any jerkiness, but you might not want the effect to appear that way. To make your tweening more linear, add this:

You could also add this per element by once again leaning on the (very elegant) data-attributes:

### Relative Mode

It's also worth mentioning relative mode, which allows us to set keyframes relative to the element, not the document as we have been doing. In this way, we could alter the height, margins etc. of our header (for example) without having to reassess our keyframe settings.

Using relative mode alters the syntax needed within the data-attributes, which I won't go into here, but it's well worth taking a look at.

For more idea of what's possible using skrollr's options, check out the examples and documentation.

## 14. Mobile Support

Here's something I didn't know until I read the skrollr documentation:

Mobile browsers try to save battery wherever they can. That's why mobile browsers delay the execution of JavaScript while you are scrolling. iOS in particular does this very aggressively and completely stops JavaScript.

Therefore, in order for skrollr to work on mobile platforms, Alexander had to build a specific bit of extra magic which is pulled in after a bit of browser-sniffing. As you've seen, our effect degrades very gracefully, so I'm not bothered about smartphone users missing out on a bit of parallax. However, if you want to make sure your project supports mobile, you'll need an extra something.

• After you've included the skrollr script, you'll need to run a wee browser check, which looks like this:

You'll notice that this then adds skrollr.mobile.min.js if the conditions are met (ie: a mobile device is being used), just make sure the path is correct as per your project. You'll find this extra script in the source download on GitHub.

• Lastly, make sure the first element after the opening <body> tag has  id="skrollr-body".

That's it! You don't need to understand why that works, it just does. If you're curious to learn more about mobile support, take a look at the documentation.

## Conclusion

Skrollr isn't just a parallax..thing, it does much more than that as we've shown. With any luck though, you'll now have been able to implement a bit of razzmatazz (whatever that is) to your own static web pages. Feel free to ask any questions you may have in the comments!