Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

responsive sprite background image, how to

Tags:

html

css

Hi I have two columns of content within a container, the first column has text and the second is a span with a background sprite image. The problem is when I get to smaller screen resolutions, I want the background sprite image to have a width in percentage to be able to scale it along with the H5 with a percentage width, is there a way to do this?

h5{
    float:left;
    display:block;
    width:800px;
}

.sprite{
    background-image: url("assets/img/website_sprite_a.png");
    background-position: -60px -60px;
    float:left;
    display:block;
    width:64px;
}

<div class="container">
 <h5>Title
 </h5>
 <span class="sprite">
 </span>
</div>
like image 905
user1937021 Avatar asked Jan 23 '13 09:01

user1937021


People also ask

How can I make background image responsive?

Here's how to create responsive background images with CSS: Use the background-size property to encompass the viewport. Give this property a cover value that will tell a browser to scale the background image's heights and width so that they always remain equal to or greater than the height/width of the device viewport.

How do you make a sprite image responsive?

First upload the image file and add the CSS to your stylesheet. Then replace your images with <img> tag to load the sprite. CSS classes are generated from the image filenames you upload, so for example: <img src="icon. png"> might become <img class="icon" alt="" src="data:...">

How do I position a sprite image?

Step 1 : Open the sprite image in the Microsoft paint tool, and find the background position of a specific part. Above, we use the first row for the demo, here we show position of the first icon on Facebook. Now we can give width and height according to smaller images and differences of icons in sprite image.


1 Answers

In your case I would go with a single background-image, but in the case you will have a lot of images or you really want to do this you can use the background-size property.

From MDN:

The background-size CSS property specifies the size of the background images. The size of the image can be fully constrained or only partially in order to preserve its intrinsic ratio.

.sprite{
    background-image: url("assets/img/website_sprite_a.png");
    background-position: -30% -30%; //use % instead pixels
    float:left;
    display:block;
    width:64px;
    background-size: 100%; //play with this
}

You also should read this:

  • Scaling background images

I have played a little bit with this on JSFIddle. Resize the browser to see the effect.

like image 159
Tom Sarduy Avatar answered Sep 29 '22 10:09

Tom Sarduy