Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

background overwrites background-size

Tags:

html

css

I already know the solution to my problem but I wonder why this is happening. Why background overwrites background-size? Please don't write about cross browsing.

HTML

<div class="icon"></div>

CSS

.icon {
    height: 60px;
    width: 60px;
    background-size: 60px 60px;
    background: transparent url("icon.png") no-repeat 0 0;

}

DEMO

http://jsfiddle.net/K74sw/2/

SOLUTION

Insert background-size instruction below background

like image 846
Bartek Bielawa Avatar asked Jun 26 '13 20:06

Bartek Bielawa


People also ask

What does background-size 100% 100% mean?

background-size:100%; = background-size:100% auto; = the width is set to be 100% large and the height of the background image follows respecting the image aspect ratio.

What is background-size?

The background-size CSS property sets the size of the element's background image. The image can be left to its natural size, stretched, or constrained to fit the available space.

How do I fit a full background image in CSS?

There are four different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height), and the multiple background syntax (separated ...


1 Answers

background is a CSS "shorthand property" for easily combining several associated background properties into one declaration (background-color, background-image, background-repeat, background-position, etc).

Thus it overwrites any properties not directly specified in the shorthand with their default value (as far as I know... according to the W3C specification you can specify background-size in the background shorthand property after background-position but I have not tested this and I have no idea what browser support for this is at the moment but I suspect it's not great). As I understand it this is because the background shorthand implicitly sets ALL the properties it represents, not just the ones you specify, so any that are not defined in the shorthand declaration are set to their default value.

To fix it either:

1) put the background-size property AFTER the background property

2) put the background-size property declaration in a more specific selector i.e a.icon rather than .icon

3) don't use background shorthand at all but instead use the individual properties to specify

like image 67
Ennui Avatar answered Oct 17 '22 01:10

Ennui