使用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:linear-gradient(to right, rgba(255,0,0,0), rgba(255,255,255,1));
这不是:
background-color:linear-gradient(to right, rgba(255,0,0,0), rgba(255,255,255,1));
其他回答
我注意到在文档中没有看到的一件事是使用 背景:url(“image.png”)
像上面这样的简写,如果图像没有找到,它会发送一个302代码,而不是像你使用的那样被忽略
background-image: url("image.png")
它们都是一样的。有多个背景选择器(即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;
这是最好的答案。速记(背景)是为了重置和干燥(与手写结合)。
你可以做一些非常巧妙的事情一旦你理解了你可以利用继承。然而,首先让我们了解一下这个文档的背景:
使用CSS3,你可以为元素应用多个背景。这些都是 与你提供的第一个背景相叠加 最后一个背景列在后面。只有最后一个背景 可以包含背景色。
所以当有人这样做的时候:
background: red;
他将背景色设置为红色,因为红色是最后列出的值。
当一个人这样做时:
background: linear-gradient(to right, grey 50%, yellow 2%) red;
红色仍然是背景色,但是你会看到渐变。
.box { 这个特性:50%; 宽度:200 px; 身高:200 px; 背景:线性渐变(向右,灰色50%,黄色2%)红色; } {前.box:: 内容:“”; 显示:块; margin-left: 50%; 高度:50%; 边界半径:0 100% 100% 0 / 50%; transform: translateX(70px) translateY(-26px) rotate(325deg); 背景:继承; } < div class = "盒子" > < / div >
现在对background-color做同样的事情:
.box { 这个特性:50%; 宽度:200 px; 身高:200 px; 背景:线性渐变(向右,灰色50%,黄色2%)红色; } {前.box:: 内容:“”; 显示:块; margin-left: 50%; 高度:50%; 边界半径:0 100% 100% 0 / 50%; transform: translateX(70px) translateY(-26px) rotate(325deg); background - color:继承; } < div class = "盒子" > < / div >
发生这种情况的原因是因为当我们这样做的时候:
background: linear-gradient(to right, grey 50%, yellow 2%) #red;
最后一个数字设置背景颜色。
然后在我们从background继承之前(然后我们得到渐变)或者背景颜色,然后我们得到红色。
使用background,你可以设置所有的背景属性,比如:
背景颜色 背景图像 平铺方式 背景位置 等。
使用background-color,你可以指定背景的颜色
background: url(example.jpg) no-repeat center center #fff;
VS.
background-image: url(example.jpg);
background-position: center center;
background-repeat: no-repeat;
background-color: #fff;
更多信息
(见说明:背景-速记属性)