tags:

views:

279

answers:

6

Let's say I have my whole sites CSS in 1 css file so it is fairly large, I am wanting to know if a page uses 3 classes with something like below that request 3 images from the server, lets say there is something like 50 of these in the whole css file, does everyone get called/requested from the server or just the 3 that a page needs?

background-image:url(http://localhost/images/btn3.gif);
+5  A: 

The browser will load what it needs to display a page. Although I can imagine various browsers could employ certain caching techniques and prefetch everything they see in a CSS file.

Developer Art
+20  A: 

There's one easy way to find out (ok, not easier than just asking Stack Overflow). Put this into a CSS file:

#nonExistantElement {
    background-image: url(myScript.php);
}

and make that script record the hit by writing to a file or something.


Ok, I've just done that myself now. Turns out: no it doesn't get the file. (Tested on Firefox 3.5.2, IE7, Chrome 2.0)

nickf
thanks for the test, I was wondering if I should cut out on some old/unused stuff but it can't be hurting too much now
jasondavis
You could have simply used the NET tab in fire fox with an HTML that does not have the Element which uses the background image....
Itay Moav
@Itay, yeah, or I could have done this and tested it cross-browser ;)
nickf
+1 for boldly testing where noone tested before :)
peterchen
+5  A: 

Your answer lies with firebug

Jeff
Yes and no. Firebug does not log all HTTP requests made by Firefox. For example it doesn't log the `favicon.ico` request.
Ionuț G. Stan
@Ionut G. Stan: that's because the favicon is downloaded for use as part of the browser user interface and is thus requested by the browser itself, not the web page. All requests related to page content, including CSS background images, are recorded by Firebug. Well, they are if it's working ;-)
NickFitz
+5  A: 

I don't think so. Images required for hover pseudo-class (mouseover) are loaded when you hover, and there can be a noticeable lag on the first appearence (unless you use a cheat to get it preloaded).

Joel Goodwin
good example situation +1
jasondavis
A: 

No, a request for the image is made only when the class, or id is present on the page.

ErnieStings
A: 

If you do want an image that's not visible on page load to "preload", then there are a variety of tricks you can use, such as displaying the image off screen on load. The most common case where "preloading" is necessary is in the case of background images that change on hover, where there would otherwise be an unacceptable lag the first time the user hovers and causes the image to change. The most common solution to this problem is called "CSS Sprites". You combine the default, hover, and (if present) active images into one file, one above the other, and you simply change the background image offset on :hover and :active.

Thom Smith