Adding Parallax to WordPress with Genesis

· 6 min read

pre { margin-bottom: 18px !important;} p { line-height: 1.8 !important; } h2 { margin: 32px 0 28px 0 !important; } Parallax describes the effect where objects up close appear to move faster than objects far away. You will see many websites that make the images appear to move slower then the scrolling text. There are a few ways to accomplish this. The simplest and most compatible way is to use just css and to basically fix the background image to the window. This gives an impression of parallax. This article by Nick explains this procedure the best. Simple Parallax Scrolling Effect. The same author has a tutorial on a more complex parallax method using both css and JavaScript. Based on what he said, the complex method would be better used with images with a repeatable pattern. Also, after reading the comments, there are compatibility issues. Stellar.js might be a robust library to use instead, but as of this writing, it hasn't been updated in 2 years or more. We're going to stick with the simple css method and see how to incorporate Nick's method into the Genesis markup.

Basic Parallax

If we just use the very minimal css for this, we could create a class called parallax and store it in our style.css file.


.parallax {
	background-size: cover;
	background-attachment: fixed;
}

NOTE: As shown in Nick's post one might also want to add 'background-position: 50% 50%;' as this will center the image in the browser window. Now we just add this class to the container we want it and add the css for a background-image. Let's say we have an image called lava.jpg for a background image in our theme's images directory. We'll want to give the container a width and height if we don't have any content in it.


.lava-box {
  background-image: url('./images/lava.jpg');
  height: 100px;
    width: 100%;
}

In our post, let's create the div:

That works but how often will we have the image in our theme folder. Most likely the image will be in our Media library. One would have to get the full url for the image by accessing the media library and then add this in our css. It would be better to change the url to a relative url. If you don't and you change your domain or move off of a staging server, this could become a broken link. For example, an image is uploaded to the library and it's full url becomes //jhtechservices.com/agenesischild/wp-content/uploads/2015/12/download.jpg. We would change this to a relative url. It's relative to where the css file is stored. If this is the theme's style.css file, the relative url to this image is '../../uploads/2015/12/download.jpg' Another way would be to just add the style to the div directly in the post edit window (making sure you are editing in 'Text' view). We don't want to change this to a relative url since most WordPress moving/restoring procedures will automatically change a full url that is in a post. Also, any url in a style written in the post is relative to the post url so this makes it difficult and if the permalink structure is changed, the relative urls in a post may not be valid.

NOTE: you'll want to give the div height and width and/or content to see the background image. Another NOTE: don't use the shorthand 'background:' css on this div as it will overwrite the background declarations of our parallax class.

Adding Parallax to Genesis basic structures

So this is good for adding parallax to post images, but how often will one do that. Parallax images are meant more for decoration or ambiance. They would mostly be used as hero/banner images or as a background to the page. Both of these usually span the entire width of the page. To span the width of the page we have to put our parallax class on an HTML container that spans the window. On a basic Genesis page, which containers normally span the entire window? Here's a basic overview of common Genesis HTML markup in the container:



    
    
     Your posts are in here

To span the width of the page, we will have use an html container outside of the div.site-inner container. We could put a container on the same level as the nav, header, and footer containers to make a parallax banner image or hero. To add parallax to your site's background, we would add the parallax class to the site-container div.

Adding a parallax hero image to the home page

Let's add a banner image below our main menu on our home page. In other words, between the