What's the difference between specifying a background color using background
and background-color
?
Snippet #1
body { background-color: blue; }
Snippet #2
body { background: blue; }
What's the difference between specifying a background color using background
and background-color
?
Snippet #1
body { background-color: blue; }
Snippet #2
body { background: blue; }
You can do some pretty neat stuff once you understand that you can play with inheritance with this. However first let's understand something from this doc on background:
So when one do:
He is setting the background-color to red because red is the last value listed.
When one do:
Red is the background color once again BUT you will see a gradient.
Now the same thing with background-color:
The reason this happens is because when we are doing this :
The last number sets the background-color.
Then in the before we are inheriting from background (then we get the gradient) or background color, then we get red.
The difference is that the
background
shorthand property sets several background-related properties. It sets them all, even if you only specify e.g. a color value, since then the other properties are set to their initial values, e.g.background-image
tonone
.This does not mean that it would always override any other settings for those properties. This depends on the cascade according to the usual, generally misunderstood rules.
In practice, the shorthand tends to be somewhat safer. It is a precaution (not complete, but useful) against accidentally getting some unexpected background properties, such as a background image, from another style sheet. Besides, it’s shorter. But you need to remember that it really means “set all background properties”.
I recreated the CSS performance experiment and the results are significantly different nowadays.
Chrome 54: 443 (µs/div)
Firefox 49: 162 (µs/div)
Edge 10: 56 (µs/div)
Chrome 54: 449 (µs/div)
Firefox 49: 171 (µs/div)
Edge 10: 58 (µs/div)
As you see - there's almost no difference.
About CSS performance :
background
vsbackground-color
:Ref : https://github.com/mdo/css-perf#background-vs-background-color
This is the best answer. Shorthand (background) is for reset and DRY (combine with longhand).
One of the difference:
If you use a image as background in this way:
then you cannot override it with "background-color" property.
But if you are using background to apply a color, it is same as background-color and can be overriden.
eg: http://jsfiddle.net/Z57Za/11/ and http://jsfiddle.net/Z57Za/12/