CSS中background:和background-image:的区别

34

快速简单的问题 在下面这个外部CSS页面的例子中;

body {
  background-image: url(background.jpg);
}
header {
  background: url(background.jpg);
}

我知道它们作用于不同的元素选择器,我的问题是使用background与background-image有什么区别?它们之间是否有特定属性的访问权限?谢谢。


2个回答

41

在 background 属性中,您可以添加 background-colorrepeatno-repeat 和其他图像属性,但在 background-image 属性中,您只能添加图像。

background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;

并且在 background 属性中,您可以在一行中完成所有这些操作。

background: #ccc url(paper.gif) no-repeat;

1
使用background时,您需要指定其他参数来设置页面的背景。如果只使用了background-image,则只需要一个参数。
background: bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial|inherit;

不是这样的。例如,你可以只设置一个属性,像这样:background: url(someURL); - Scott Marcus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接