I just found out about sprites a few days ago and realized how much of an improvement it would be if I start using sprites.
Now I’m trying to use a sprite to get the “hover” image (bottom part of sprite) to dissolve (using opacity) into the “original” image (top part of sprite), but all it does is display the “original” image.
In another attempt, I tried doing this without the dissolve, but instead of replacing the “original” image with the “hover” image, it just moves the image up!
Yes, that’s because you are using transitions, which don’t play well with sprites. Browsers seem to handle the transitions differently. It’s early days with CSS3, I’m afraid. Probably the best thing to do here is forget about the transition, or resort to JS if you really must have it.
I guess this explains why the sprites + transitions I did see like this utilized JS for their transitions. The foremost reason I wanted to use sprites here is because:
Is there a way to avoid this first-time delay besides using sprites?
Can you provide the url to the image you are trying to pre-load? I’d like to see if I can help resolve your pre-load issue and run a few tests to see why the CSS :before didn’t work. Also, what browser are you using for testing?
I am testing it locally on Firefox 13.0.1 and Google Chrome 19.
I tried to recreate this scenario in jsfiddle, but for some strange reason, on jsfiddle images are automatically pre-loading by default in jsfiddle–even without :before.
Interesting, I used img.jpg and saw in Chrome Developer Tools (Network section) that it did indeed try to pull down the img.jpg file during the page load, so I am not sure why that didn’t work using :before
It turns out I was accidentally loading up the original image in :before instead of the hover image!
And thanks for letting me know about Chrome Developer Tools–it is very handy. Before you told me about it, I had only been using Web Developer and Firebug on Firefox.
Do you know if there is somehow a way to load up multiple images within the same element? For instance, the following CSS snippet does not work because the last one (img3) replaces any previous content that came before it in that element:
However, you should remember that most browsers will only display a background image when it is called for on the page. If you set it to display:none or visibility:hidden the browser probably won’t load it until you show it on the page with display:block and then it will get loaded (browsers do vary on this though). Moving images off screen is probably a better approach but some browsers may be clever enough to know that the image is off screen and therefore not load it until it comes on screen. The most reliable method is to make sure the image is on screen to start with in some manner (such as a sprite or stacking on top as in my example).