Responsive Background Image Sprites In CSS

Posted By Weston Ganger

Using sprites is pretty sweet I think. You will probably need to make it responsive at one point.

Heres how to do it using plain CSS.

.sprite-container{
  width: 400px;
}

.sprite{
  /* Create a Responsive Square */
  width: 100%;
  height: 0;
  padding-bottom: 100%;

  /* Set The Sprite as the Background */
  background-image: url("images/ourimage.png");
  background-position: 0 0;
  background-size: 200%; //number of images horizontally * 100
  display:block;
}

/* On Hover Show Second of Two Images */
.sprite:hover{
  background-position: 100% 0;
}

Related External Links:

Article Topic:Software Development - HTML / CSS

Date:March 15, 2016