Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding CSS background image styles - both images are loaded?

Tags:

html

css

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.

like image 477
Evanss Avatar asked Sep 08 '11 08:09

Evanss


People also ask

Can you have 2 background images CSS?

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?

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.

How do I stop a background image from repeating CSS?

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.


2 Answers

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

like image 160
Evanss Avatar answered Oct 15 '22 15:10

Evanss


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.

like image 42
thirtydot Avatar answered Oct 15 '22 13:10

thirtydot