sta*_*tor 261 css background background-color
使用background
和指定背景颜色有什么区别background-color
?
片段#1
body { background-color: blue; }
Run Code Online (Sandbox Code Playgroud)
小片#2
body { background: blue; }
Run Code Online (Sandbox Code Playgroud)
fca*_*ran 242
假设那些是两个不同的属性,在你的具体例子中,结果没有区别,因为background
实际上是一个简写
background-color
background-image
background-position
background-repeat
background-attachment
background-clip
background-origin
background-size
因此,除了background-color
使用background
快捷方式之外,您还可以添加一个或多个值,而不会多次重复任何其他 background-*
属性.
选择哪一个基本上取决于您,但它也可能取决于您的样式声明的特定条件(例如,如果您只需要覆盖从父元素background-color
继承其他相关background-*
属性的时间,或者您需要删除所有值除了background-color
).
Anr*_*rgh 146
background
将取代所有之前的background-color
,background-image
等规格.它基本上是一种速记,但也是一种重置.
我有时会用它来覆盖background
模板自定义中的先前规范,我想要以下内容:
background: white url(images/image1.jpg) top left repeat;
如下:
background: black;
因此,所有参数(background-image
,background-position
,background-repeat
)将重置为默认值.
l2a*_*lba 80
关于CSS性能:
background
vs background-color
:
比较18个颜色样本在页面上呈现100次作为小矩形,一次使用背景,一次使用背景颜色.
虽然这些数字来自单页重新加载,随后刷新时渲染时间发生了变化,但每次的百分比差异基本相同.
在Safari 7.0.1中使用背景而不是背景颜色时,节省了近42.6毫秒,几乎快了两倍.Chrome 33似乎大致相同.
这真是让我感到震惊,因为最长的时间有两个原因:
- 我通常总是争论CSS属性的明确性,尤其是背景,因为它可能会对未来的特性造成不利影响.
- 我认为,当浏览器看到时
background: #000;
,他们真的看到了background: #000 none no-repeat top center;
.我这里没有资源的链接,但我记得在某处读过这篇文章.
参考: https ://github.com/mdo/css-perf#background-vs-background-color
alp*_*nyx 23
有了background
你可以设置所有background
的属性,如:
background-color
background-image
background-repeat
background-position
有了background-color
你可以指定背景的颜色
background: url(example.jpg) no-repeat center center #fff;
Run Code Online (Sandbox Code Playgroud)
VS.
background-image: url(example.jpg);
background-position: center center;
background-repeat: no-repeat;
background-color: #fff;
Run Code Online (Sandbox Code Playgroud)
(参见标题:背景 - 速记属性)
其中一个区别是:
如果您以这种方式使用图像作为背景:
background: url('Image Path') no-repeat;
Run Code Online (Sandbox Code Playgroud)
那么你不能用"background-color"属性覆盖它.
但是,如果您使用背景来应用颜色,则它与背景颜色相同并且可以覆盖.
例如:http://jsfiddle.net/Z57Za/11/和http://jsfiddle.net/Z57Za/12/