tags:

views:

58

answers:

3

Hello,

I have a web application whose performance I am working to enhance. In an attempt to do this, I decided to use css sprites. I have placed all of my images in a .png file called images.png.

CSS sprites have worked well for all css classes that just display an image once. However, several of my images need to be repeated. For instance, I have a banner.png image used for the banner background. Whenever I set the background-repeat property, it seems that the image does not repeat. To show my CSS definitions, here they are:

Before CSS Sprites
------------------
.ghwc {
  background-image: url(/images/layout/banner.png);
  background-repeat:repeat-x;
  color:White;
  width:300px;
}

After CSS Sprites
-----------------
.ghwc {
  background-image: url(/images/images.png);
  background-repeat:repeat-x;
  color:White; 
  background-position:60px 319px; 
  width:300px;
}

My question is, how do I use CSS sprites for repeated images like backgrounds?

Thank you,

+5  A: 

My question is, how do I use CSS sprites for repeated images like backgrounds?

You don't. That is simply not possible using CSS sprites. To do that, you would have to be able to specify an area of the image that is to be repeated, and to my knowledge that is impossible in both CSS 2 and 3.

Pekka
You could probably get away with images that only repeat in one direction, but it really doesn't sound like it's worth the effort. We need support for packed images...
Matti Virkkunen
@Matti yup, or multipart responses for that matter.
Pekka
you could create a bunch of divs w/the sprited background as the bg, set the width and height, and then have an absolutely positioned div on top :D but if you do that, i'm pretty sure that somewhere a unicorn is murdered
Jason
@Jason you would then have to start cloning the absolutely positioned DIV to achieve the `repeat` effect, possibly with the help of some jQuery. I think one Unicorn won't cut it, that sounds more like a herd :D
Pekka
A: 

Assuming your background image (images.png) shows at all, your code should work. If you want this to render correctly on Opera and Firefox, you'll need to add

background-attachment:fixed;

Edit: I just realized you're probably talking about a specific coordinate set in a "sprite" image comprised of several 'images'. You're not going to get any one particular area of an image to repeat like that. Crop the image to the size you're concerned about, then use the code you have.

Superstringcheese
I don't think that's his question, it's that he now wants to use the structure as a background image - which is impossible as far as I can see.
Pekka
Yeah, edited to reflect that. Didn't realize that's what he was talking about at first.
Superstringcheese
A: 

You can do this if you're only background-repeat:repeat-x; as in the example, you just need to make all backgrounds contained within the sprite image container the same width and lay the sprite image file out vertically. Then your background position property will always have the first x position be 0 and the sprite is located with the second y position (e.g. background-position:0 0; background-position:0 -100px; background-position:0 -200px; etc) . This might not work across all browsers if you can't specify the exact height and set overflow:hidden.

mVChr