I use CSS to place a lot of my images (as backgrounds for divs) and I often find that they load very slowly this way. They seem to be the last thing to load. Even small little icon images take a while to show up this way. Is there a way to tell the browser a priority for images? Or get it to download the images in the CSS file earlier and render them earlier?
|
|
CSS is for styling, not content. Browsers (rightly) try and display content before adding styling, hence the reason why images in style-sheets are usually downloaded last. If images are important to your content then add them via standard HTML |
|||||||||
|
|
Use absolute URI's from your stylesheet and add the images from IMG tags to a hidden div on the page (this assumes you're using the same images on every page; ideally in the footer so they're all loaded and cached on any given page call). Images on the page get priority and, once the images are cached, they'll render immediately on subsequent page requests. |
|||
|
|
|
For browsers that support the data URI type (see http://en.wikipedia.org/wiki/Data_Uri for info) to include the image in the CSS itself. This has a few disadvantages though:
|
|||
|
|
|
As of right now there is no way to specify which files download first. FYI, images specified in CSS files as background images download last probably because the browser sees them non-content and thus a lower priority so avoid using them for important images that you want to load quickly. |
||||
|
|
|
Use CSS sprites, especially for icons. |
|||
|
|
|
Make your images as small as possible. You can use smush.it to remove unnecessary bytes. |
|||
|
|