使用background和background-color指定背景色之间的区别是什么?
片段# 1
body { background-color: blue; }
段# 2
body { background: blue; }
使用background和background-color指定背景色之间的区别是什么?
片段# 1
body { background-color: blue; }
段# 2
body { background: blue; }
当前回答
它们都是一样的。有多个背景选择器(即background-color, background-image, background-position),你可以通过更简单的背景选择器或更具体的背景选择器访问它们。例如:
background: blue url(/myImage.jpg) no-repeat;
or
background-color: blue;
background-image: url(/myImage.jpg);
background-repeat: no-repeat;
其他回答
其中一个区别是:
如果你以这种方式使用图像作为背景:
background: url('Image Path') no-repeat;
然后你不能用"background-color"属性覆盖它。
但是如果你使用背景来应用颜色,它和background-color是一样的,并且可以被覆盖。
例如:http://jsfiddle.net/Z57Za/11/和http://jsfiddle.net/Z57Za/12/
有一个关于背景和背景颜色的错误
这个的区别, 当使用后台时,有时当你创建网页时 在CSS中 background: #fff //可以覆盖蒙版图像块(“顶部项目,文本或图像”)) 所以最好总是使用背景色 为了安全起见,在你的设计中,如果它是单独的
比较18个色块在一个页面上渲染100次的小 矩形,一次带有背景,一次带有background-color。
我重新创建了CSS性能实验,现在的结果明显不同。
background
Chrome 54: 443(µs/div)
Firefox 49: 162(µs/div)
边缘10:56(µs/div)
background-color
Chrome 54: 449(µs/div)
Firefox 49: 171(µs/div)
边缘10:58(µs/div)
如你所见,几乎没有区别。
关于CSS性能:
背景vs Background -color:
比较18个色块在一个页面上渲染100次的小 矩形,一次带有背景,一次带有background-color。
While these numbers are from a single page reload, with subsequent refreshes the render times changed, but the percent difference was basically the same every time. That's a savings of almost 42.6ms, almost twice as fast, when using background instead of background-color in Safari 7.0.1. Chrome 33 appears to be about the same. This honestly blew me away because for the longest time for two reasons: I usually always argue for explicitness in CSS properties, especially with backgrounds because it can adversely affect specificity down the road. I thought that when a browser sees background: #000;, they really see background: #000 none no-repeat top center;. I don't have a link to a resource here, but I recall reading this somewhere.
参考:https://github.com/mdo/css-perf#background-vs-background-color
Background是以下属性的简写:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
你可以在这里找到每一处房产的详细信息
属性的顺序
在大多数浏览器实现中(我认为可能旧浏览器会出现问题),属性的顺序并不重要,除了:
background-origin and background-clip: when both of this properties are present, the first one refer to -origin and the second to -clip. Example: background: content-box green padding-box; Is equivalent to: background-origin: content-box; background-color: green; background-clip: padding-box; background-size must always follow background-position and the properties must be separated by / if background-position is composed by two numbers, the first one is the horizontal value and the second the vertical value.