As I understand it, when you use the shorthand property background, the browser first sets all background properties to their default values and then puts in the values that you’re declaring. Is it then better to use background-color:white; instead of background:white? Does this change when you are putting in more values such as background position or background image? Or is it always best to declare attributes individually?
I’m thinking that there must be some sort of tipping point where the savings in bytes balance the processing time gained by specifying attributes individually. However, I could be completely wrong – that’s why I’m asking here.
I hear you about best practices, but as mentioned the differences in processing and even load time are negligible. There is no best practice for when to use these rules, aside from what makes sense in your stylesheet. The real difference is that they effect inherited properties differently. Setting
background-color: white;will only overwrite thebackground-colorrule (whether or not it was originally set withbackgroundorbackground-color) butbackgroundwill overwrite the any/allbackgroundrules set, thus potentially killing background images and associatedbackground-repeat, etc. Here’s an example:With HTML like:
.box1 will show the star.png image (with a blue background if the image is transparent), while .box2 will only show a green background, no image. The best practices lesson with these two rules is to evaluate CSS authoring and inheritance in general — not rendering performance. That in mind, it’s generally best to apply
backgroundto the most general/abstracted rule of an element, and then overwrite properties on more specific instances, using classes or IDs, withbackground-color,background-image, etc.