Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Sprites with Dynamic Sizing

Tags:

css

sprite

I've decided to crate a sprite sheet for my entire site (+-30 images) so I can load 1 image and just reference positions, which reduces image load time and server calls.

My question: Is it possible to reference an image in the sprite sheet and then size that image to 100% of its parent container?

So For example:

#SomeDiv
{
    background: url("/Content/Images/SpriteSheet.png") -125px 0 no-repeat;
    width:100px;
}

The width of my div is 100px, but the sprite I want to reference is 20px(for example) - how can I streth the extracted sprite to grow to 100px?

Regards, Byron Cobb.

like image 252
Bob Avatar asked Sep 22 '10 11:09

Bob


1 Answers

Well, if you really want an answer, sure, why not. See: http://jsfiddle.net/3dsgn/3/

Basically we're working with CSS3 here, so IE support (except 9) is non-existent. You'll also have to use the version with the -moz- extension for Firefox 3.6 and below. The technique itself is also somewhat troublesome. You actually have to go and calculate the numbers yourself - percentages won't work, naturally.

#sprite-large {

    /* All of these numbers are 2x their normal, 
       though tweaked slightly so that they will look okay */
    width: 36px;
    height: 36px;
    background: url('url/to/your/image.png') -38px -112px;

    -moz-background-size: 448px 368px;
    background-size: 448px 368px;
}
like image 193
Yi Jiang Avatar answered Nov 22 '22 19:11

Yi Jiang