【发布时间】:2014-01-14 01:51:37
【问题描述】:
这两个 CSS 属性之间是否有区别:
background: none;
background: transparent;
- 它们都有效吗?
- 应该使用哪一个?为什么?
【问题讨论】:
标签: css background
这两个 CSS 属性之间是否有区别:
background: none;
background: transparent;
【问题讨论】:
标签: css background
它们之间没有区别。
如果您没有为background 是其简写形式的六个属性中的任何一个指定值,则将其设置为其默认值。 none 和 transparent 是默认值。
将background-image 显式设置为none,并将background-color 隐式设置为transparent。另一个则相反。
【讨论】:
background-color: transparent; background-image: none; 相同。用户样式表可能会覆盖这些值中的一个或两个,但它会完全像 background-color: transparent; background-image: none; 已被明确写入一样。
transparent 和none。
作为 @Quentin 回答的附加信息,正如他所说的,
background CSS 属性本身,是:
background-color
background-image
background-repeat
background-attachment
background-position
也就是说,您可以将所有样式归为一组,例如:
background: red url(../img.jpg) 0 0 no-repeat fixed;
这将是(在本例中):
background-color: red;
background-image: url(../img.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 0 0;
所以...当您设置:background:none;您是说所有背景属性都设置为 none...
你是说background-image: none; 和所有其他人都处于initial 状态(因为它们没有被声明)。
所以,background:none; 是:
background-color: initial;
background-image: none;
background-repeat: initial;
background-attachment: initial;
background-position: initial;
现在,当您仅定义颜色(在您的情况下为 transparent)时,您基本上是在说:
background-color: transparent;
background-image: initial;
background-repeat: initial;
background-attachment: initial;
background-position: initial;
我重复一遍,正如@Quentin 所说的那样default transparent 和 none 值在这种情况下是相同的,所以在你的例子中和您最初的问题,不,它们之间没有区别。
但是!.. 如果您说 background:none 与 background:red 是的... 有很大的不同,正如我所说,第一个会将所有属性设置为 none/default,而第二个只会更改color,其余部分保持default 状态。
简答:不,根本没有区别(在您的示例和原始问题中)
长答案:是的,有很大区别,但取决于直接在授予属性的属性上。
default)初始值是其普通属性的初始值的串联:
background-image: none
background-position: 0% 0%
background-size: auto auto
background-repeat: repeat
background-origin: padding-box
background-style: is itself a shorthand, its initial value is the concatenation of its own longhand properties
background-clip: border-box
background-color: transparent
background 描述here
Upd2:更好地阐明background:none; 规范。
【讨论】:
console jsfiddle.net/dnzy2/6
background-image 的初始值为none,所以background-image: none === background-image: initial,因此,background: none === background: transparent,我猜跨度>