Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSS中的后台选择器声明_Css_Selector_Declaration - Fatal编程技术网

CSS中的后台选择器声明

CSS中的后台选择器声明,css,selector,declaration,Css,Selector,Declaration,在CSS中,选择器的声明如下所示: background-attachment: scroll; background-color: transparent; background-image: url(/images/ucc/green/btn-part2.gif); background-repeat: no-repeat; background-position: right top; 我想优化代码并将其更改为: background: scroll transparent ur

在CSS中,选择器的声明如下所示:

background-attachment: scroll; 
background-color: transparent; 
background-image: url(/images/ucc/green/btn-part2.gif); 
background-repeat: no-repeat; 
background-position: right top;
我想优化代码并将其更改为:

background: scroll transparent url(/images/ucc/green/btn-part2.gif) no-repeat right top;

我的问题是,这是正确的方式吗?它在IE7/8、Firefox、Safari中有效吗?

是的,这是正确的方式,在所有主流浏览器中都有效。您可以阅读有关CSS属性的更多信息,该属性可用于同时设置所有
background-*
属性

更新:是,以下规则适用:

background
{
    background: transparent url(/images/ucc/green/btn-part2.gif) no-repeat scroll 20px 40px;
}
除此之外,浏览器将尝试将此规则应用于DOM中的
元素。由于HTML中没有这样的元素,因此该规则永远不会应用于任何内容。:-)因此,您必须更改规则选择器以选择要应用背景属性的容器元素:

div#myDivIWantToSetBackgroundTo
{
    background: transparent url(/images/ucc/green/btn-part2.gif) no-repeat scroll 20px 40px;
}

顺便说一句,你可以在W3School网站上找到它。

是的,它可以工作。看看这里的第6点-

当使用速记属性时 属性值的顺序为:

* background-color
* background-image
* background-repeat
* background-attachment
* background-position

谢谢你的回答。我还有一个问题。如果我想给背景位置:20px 40px;那么CSS规则应该是这样的?背景{背景:透明url(/images/ucc/green/btn-part2.gif)无重复滚动20px 40px;}
background
{
    background: transparent url(/images/ucc/green/btn-part2.gif) no-repeat scroll right top;
}