Are overriden css properties cancelled or just overriden?

核能气质少年 提交于 2019-12-24 03:49:11

问题


I mean,

If i do:

#item { background:url('image.jpg');}

and then in another stylesheet i do

#item {background:red;}

will the browser load image.jpg and then cancell it and set red, or will go straight to red? (without loading the image)

thanks!


回答1:


It will do both, since those two statements are the same as

#item { background: red url('image.jpg'); }

or

#item { background-color: red; }
#item { background-image: url('image.jpg'); }

In both cases, the background will have an image, and the entire area of the item element in red. So an opaque image, like a .png with transparency, would for instance be filled with red in the transparent areas as well.

However, as the script parses the CSS in a cascading fashion,

#item { background: url('image.jpg'); }
#item { background: url('anotherimage.jpg'); }

it will load "anotherimage" and ignore the other one. The request isn't sent until the CSS is done compiling and determined the order of specificity. In other words, the first image is overridden, and is therefore never requested.

The 'background' is actually a short hand property, like 'border', combining all of the different properties into a single statement. Here is a link about the 'background' property, scroll down and you can read about it.




回答2:


In this specific case, the browser should load the image and apply red at the same time, because those two definitions don't really overlap. It's like setting the attributes "background-color" and "background-image" individually. It makes sense as there could be a little background-image and a red background for the rest of the larger element.

In general, definitions are overridden, though, depending on what is called the specificity. Check this page for more on that http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/



来源:https://stackoverflow.com/questions/7428183/are-overriden-css-properties-cancelled-or-just-overriden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!