How to Use Responsive Background Image Sprites – CSS Tutorial

At one time or another, you may have found yourself wanting to use sprites in your responsive website design. While at first this may seem like a very simple task, in reality it’s a little more difficult of a challenge. It can, however, be done. Here’s how! This trick is fully supported by all modern browsers other than IE 8 and earlier, in which the image resizing aspect will not work properly, though it will not break completely.

Let’s start with our HTML. Let’s say we have part of a website here, and what we want is for the background image of this div to change when we hover over it. The catch is that we are using a sprite for it (a single image containing both our normal and rollover images) and it’s a responsive design. Let’s take a look.

Truly groundbreaking.

Okay, the html isn’t really that relevant I realize now. I could make it more fancy to try to make it a “real world example,” but why bother? Keep it simple, stupid.

The secret is clearly all in the css. Let’s say this div is always a square,  using our trick to maintain the same aspect ratio for an element in a responsive design. This might be the CSS of the element before we’ve added the background image.

Now we have a square image we want to nestle nicely inside there. And when we rollover the element, we want it to change. How do we do that? By adding the following css:

Now here’s what’s really interesting about the background-size property. It does not STRETCH the image in both directions, it assumes that the image fits perfectly WIDTH-wise into our container, and then calls that 100%. If we want the image to be larger than what actually fits in the container, we go bigger. If we want it to be smaller and have to repeat, we go smaller. In our case, let’s say our image is 1000px wide and 500px tall. On the left 500px we have the first image, on the right 500px we have the rollover image. If we were to leave the setting at background-size: 100%;, we would end up with the image fitting completely within the div and repeating itself once underneath.

We don’t want this, however. We want the first half of the image only to fit. So we set background-size: 200%;, since we need it to be twice as large as the space we have assigned for it, since we only want the first half of the image to fit in our div.

Now it’s time to set everything up for the hover state. To do that, we add this to our css:

The only thing we need to change is the background position! The secret is to place the background image using only percents. This is what allows it to be responsive. Actually USING this positioning system can be a little confusing, so let me break it down for you.

Calculating the background position

This is actually quite complicated and not at all intuitive. The best way I can explain it in words is this: the distance between 0% and 100% for your background position is the distance between when the left edge of your background image is touching the left edge of your element, and when the right edge of your background image is touching the right edge of your element.

So let’s break this down into a formula I came up with. This formula assumes each image in the sprite is the same size, and that each image is the same size as the element (i.e.: fits it exactly).

Percent Increments to Use = 100% / (Discrete images in your sprite – 1)

So if you have 4 images in your sprite: 100% / (4-1) = 100% / (3) = 33.33333%

So to display the first image, you set background-position: 0 0;. The second, background-position: 33.33333% 0;. And so on. Again, it’s not that intuitive, but with the formula it’s a little easier.

Let’s Consider another example. You have FIVE images in one sprite that you wish to use, all in a row. You want to display the SECOND image in the row. What background position do you use? Well… 100% / (5-1) = 25%. So if I wanted to display the second image, I would have to use background-position: 25% 0;. Easy!

If you would like to see a real-life and real-creepy example I made using my own face, check it out here!

About Brian Johnson

Brian Johnson

Brian Johnson is the founder of PageCrafter, a Minneapolis-area web design and hosting company. He specializes in WordPress development, website security, hosting infrastructure, and ongoing website maintenance. Brian works hands-on with hundreds of business websites, helping organizations keep their WordPress sites secure, reliable, and up to date and has been building websites for almost two decades.

84 Comments on “How to Use Responsive Background Image Sprites – CSS Tutorial”

  1. Hello Brian,

    i have a css sprite with 8 pictures in it,
    each has width: 115px, height: 115px, left to right in total: 920px.
    it works successfully with this code:

    html:

    css:
    .sprite {
    background:url(multipic.jpg) no-repeat;
    width:115px;height:115px;
    }

    #pic1 {background-position:-0 -0;}
    #pic2 {background-position:-115px -0;}
    #pic3 {background-position:-230px -0;}
    …

    But now – for another purpose – i want to display it
    with smaller dimensions.
    That’s why i read your website yesterday with great pleasure.
    This is my new code for the first picture, copied from jour website:
    html:

    css:
    div.smallsprite {
    width: 20%;
    height: 0;
    padding-bottom: 20%;
    background-image: url(multipic.jpg);
    background-position: 0 0;
    background-size: 800%;
    display:block;
    }

    Unfortunately, no picture is displayed.
    Where is the mistake?
    I ask for your support!

    Kind regards

    1. My guess is that the parent element has no width. The size calculated by “width: 20%;” and “padding-bottom: 20%;” are both determined by the width of the parent element. If it has no width, then this element will not show.

      If you set up a fiddle with it I can maybe take a closer look. Hard to troubleshoot otherwise.

  2. Brilliant! Could not work out how to both contain the background image and use sprites. This works perfectly. Thank you.

  3. Brilliant! Could not work out how to both contain the background image and use sprites. This works perfectly. Thank you.

  4. Hey Brian, so cool how you are helping out with this resource!

    Wannabe “try to fix my own code” here! Been spending a gazillion hours trying to get my first css sprites in place – which I finally did – but when I see them Implemented the seem to move 1 px (see the three pics right under the fold – this didn’t happen when I tried it outside of my page). I used this website to generate the sprite and code http://responsive-css.spritegen.com/

    Is it easy for you to tell why it happens? Is this something that happens aaaaall the time? Any fix that comes to mind? Thanks a million

    1. I have seen that as an issue before! One trick We’ve seen to fix this is to just make the background very slightly larger than it needs to be. So calculate what percentage would essentially be 1px and add that on. Sometimes you might need to tweak other things slightly to get it all to work perfectly, but that should do the trick I believe. Other times, having things overlap that white space works, too.

  5. Hi Brian,

    Confusing myself with this one.

    I have an image on top of another images in a vertical stack. 1320×992 when together. Just want a 1320×496 image to show at first and then the other below it on rollower.

    At the moment I have

    div.sprite2 {
    margin:0 auto;
    width: 100%;
    height: 0;
    padding-bottom: 100%;
    background-image: url(“../img/image1.jpg”);
    background-position: 0 0;
    background-size: 200%;
    display:block;}

    div.sprite2:hover {
    background-position: 0 -100%;
    background-size: 200%;
    }

    Which makes the initial image way too big. Can you explain (in idiot terms) please how to make the images the correct size

    1. Hi there,

      I believe “background-size” when only setting one value actually specifies the width of the image. The height will follow in proportion (it doesn’t stretch it). So you actually want that set to 100% because your image is already the right width. And don’t change it on hover.

      I think that’s all you need to do, actually. Should work after that! Rest of your code looks right.

      1. Cheers Brian,

        Got it working. I was making the calculation the wrong way round.

        div.sprite2 {
        margin:0 auto;

        width: 100%;
        height: 0;
        padding-bottom: 37.5758%;
        background-image: url(“../img/image1.jpg”);
        background-position: 0 0;
        background-size: 100%;
        display:block;}

        div.sprite2:hover {
        background-position: 0 100%;
        background-size: 100%;
        }

        An interesting one with this techniques is I tried adding css effects to the images (just for fun). The hove