If I have 2 CSS styles that assign background images to an element, and one style overrides the other. Will both images be downloaded by the browser or just the overriding one?
I reason I'm asking is I recently attended a workshop on conditional stylesheets for mobile devices. If I override my normal bg images with smaller ones to save bandwidth, will the larger images be loaded anyway? This seemed to be what the guy was saying but it seems strange to me it would work this way.
CSS allows you to add multiple background images for an element, through the background-image property. The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.
What will happen if we use CSS to set both a background image and a background color for the same element? The background image will show with a border around it in the background color. The background image and the background color will be blended and both will show.
To make a background image not repeat in HTML, specify no-repeat in the background-repeat property or the background shorthand property. The background image is set to 'no-repeat' using the 'background-repeat' property.
It seems in some cases the answer is yes:
http://www.cloudfour.com/css-media-query-for-mobile-is-fools-gold/
http://www.cloudfour.com/examples/mediaqueries/image-test/#t4
The overridden image will not be loaded.
Just to be clear, for example: http://jsfiddle.net/thirtydot/MjKfG/1/show/
<div id="test">test</div>
div {
background: url(http://dummyimage.com/600x400/000/fff)
}
#test {
background: url(http://dummyimage.com/200);
width: 200px;
height: 200px
}
Only http://dummyimage.com/200
will be loaded.
This is true in at least Chrome, Safari, Firefox, IE8/9, Opera.
I'd guess it's true in all browsers, because it's a simple and effective optimization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With