简体   繁体   English

CSS3中的背景速记顺序

[英]background shorthand order in CSS3

There are questions in the HTML5 microsoft certification about the correct order of selectors in the CSS3 background shorthand... but I searched, searched... but not found somewhere a valid reference to what is the real order precedence in that shorthand (if any)... 关于CSS3 背景中的选择器的正确顺序,HTML5微软认证中存在一些问题...但是我搜索,搜索了......但是找不到某个地方有效的参考,这个简写中的实际顺序优先级(如果有的话) )...

the W3C recommendation on this matter is not very explicit on this subject (at least, is not evident to find that information).... 关于这个问题的W3C建议在这个问题上并不十分明确(至少,找到这些信息并不明显)....

is ancient CSS2 "see, I rap" ( CIRAP ) acronim was more or less catchy... but even there W3C didn't clearly exprimed a order?!... 是古老的CSS2“看,我说唱”( CIRAP )acronim或多或少吸引人......但即使有W3C没有明确表达订单?!...

What is the meaning for this kind of questions in certifs, if no one knows the standard? 如果没有人知道标准,那么这些问题在证书中的含义是什么? What is, finally, the correct order to take in CSS3? 最后,CSS3中的正确顺序是什么?

their examples are confusing... by ex, the following makes believe that the order is CIRAPCOS 他们的例子令人困惑......通过ex,以下人员相信订单是CIRAPCOS

Another example shows equivalence: 另一个例子显示了等价:

 div { background: padding-box url(paper.jpg) white center } div { background-color: white; background-image: url(paper.jpg); background-repeat: repeat; background-attachment: scroll; background-position: center; background-clip: padding-box; background-origin: padding-box; background-size: auto auto } 

then the specification mentions: 然后规范提到:

Given a valid declaration, for each layer the shorthand first sets the corresponding layer of each of 'background- image ', 'background- position ', 'background- size ', 'background- repeat ', 'background- origin ', 'background- clip ' and 'background- attachment ' to that property's initial value, then assigns any explicit values specified for this layer in the declaration. 给定一个有效的声明,对于每一层,速记首先设置每个“背景图像 ”,“背景位置 ”,“背景大小 ”,“背景重复 ”,“背景原点 ”,“背景”的相应图层。 - 将''和'background- attachment ' 剪辑到该属性的初始值,然后在声明中指定为此图层指定的任何显式值。 Finally 'background- color ' is set to the specified color, if any, else set to its initial value. 最后,'background- color '设置为指定的颜色(如果有),否则设置为其初始值。

so, this is how we obtain IPSROClACo 所以,这就是我们获得IPSROClACo

Is is explicitely written : 是明确写的:

Value: 值:

[ <bg-layer> , ]* <final-bg-layer>

Where: 哪里:

<bg-layer> = 
       <bg-image> 
    || <position> [ / <bg-size> ]? 
    || <repeat-style> 
    || <attachment> 
    || <box>{1,2} 
<final-bg-layer> = 
       <bg-image> 
    || <position> [ / <bg-size> ]? 
    || <repeat-style> 
    || <attachment> 
    || <box>{1,2} 
    || <'background-color'>

An example, which give us IPSRABC : 一个给我们IPSRABC的例子

background: 
    url("content.jpg") 
    left top / 200px 70px
    no-repeat
    scroll
    content-box content-box
    white;
/* or */
background-image: url("content.jpg") ;
background-position: left top;
background-size: 200px 70px;
background-repeat: no-repeat;
background-attachment: scroll;
background-clip: content-box;
background-origin: content-box;
background-color: white;

If you want a mnemonic for it, here is one (and ya, I'm fully opened to any better sentence !) : 如果你想要一个助记符,这里有一个(而且,我完全打开任何更好的句子!):

I pee a sir, Abby ! 我小便先生,艾比! See ? 看? (IPSRABC) (IPSRABC)

Note : you can put background-repeat before background-position / background-size , which give you IRPSABC . 注意:您可以在background-position / background-size之前放置background-repeat ,它们可以为您提供IRPSABC

Q: What are the correct order of 问:什么是正确的顺序 selectors 选择 values in the CSS3 background shorthand? CSS3背景中的值是简写吗?

A: any order 答: 任何订单


MSDN documentation for background states... 关于背景状态的MSDN文档 ......

Up to five of the following space-delimited values, in any order : 任何顺序最多五个以下以空格分隔的值:

  • color - Any of the range of color values available to the background-color property. color - background-color属性可用的任何颜色值范围。
  • image - Any of the range of image values available to the background-image property. image - background-image属性可用的任何图像值范围。
  • repeat - Any of the range of repeat values available to the background-repeat property. repeat - background-repeat属性可用的任何重复值范围。
  • attachment - Any of the range of attachment values available to the background-attachment property. 附件 - background-attachment属性可用的任何附件值范围。
  • position - Any of the range of position values available to the background-position property. position - background-position属性可用的任何位置值范围。

The MSDN documentation lacks the updated CSS3 shorthand properties, but the answer remains...any order MSDN文档缺少更新的CSS3速记属性,但答案仍然是......任何订单


Digging further, even MDN documentation for background (which does include CSS3 syntax) states... 进一步挖掘,甚至MDN文档背景 (包括CSS3语法)声明......

One or more of the following, in any order : 以下任何一种或多种顺序

<'background-attachment'> See background-attachment <'background-attachment'>请参阅背景附件

<'background-clip'> See background-clip <'background-clip'>请参阅background-clip

<'background-color'> See background-color <'background-color'>请参阅background-color

<'background-image'> See background-image <'background-image'>请参阅background-image

<'background-origin'> See background-origin <'background-origin'>参见background-origin

<'background-position'> See background-position <'background-position'>参见background-position

<'background-repeat'> See background-repeat <'background-repeat'>请参阅background-repeat

<'background-size'> See background-size. <'background-size'>请参阅background-size。 This property must be specified after background-position, separated with the '/' character. 必须在background-position之后指定此属性,并以“/”字符分隔。


A basic example of this: jsfiddle example 一个基本的例子: jsfiddle例子

.box1 {
    background-image: url(//placehold.it/100/f00);
    background-position: center center;
    background-color: red;
}
.box2 {
    background: 
        url(//placehold.it/100/f00) /* background-image */
        center center /* background-position */
        red /* background-color */;
}
.box3 {
    background: 
        center center /* background-position */
        url(//placehold.it/100/f00) /* background-image */
        red /* background-color */;
}
.box4 {
    background: 
        red /* background-color */
        url(//placehold.it/100/f00) /* background-image */
        center center /* background-position */;
}

The correct order specified by Microsoft is: Microsoft指定的正确顺序是:

  • color 颜色
  • position 位置
  • size 尺寸
  • repeat 重复
  • origin 起源
  • clip
  • attachment 附件
  • image 图片

I have learnt NOT to use the background shorthand property at all, because of the following reasons: 我学会了根本不使用background速记属性,原因如下:

  1. Mixed Implementation across browsers and editors 跨浏览器和编辑器的混合实现
  2. It sets missing properties to their initial values - this will overwrite any previously set values 它将缺少的属性设置为其初始值 - 这将覆盖任何先前设置的值
  3. Some properties share the same values, and this causes a lot of confusion. 某些属性共享相同的值,这会导致很多混淆。

In this particular case, choose clarity and consistency over performance. 在这种特殊情况下,选择清晰度和一致性而不是性能。 I should also mention that the performance gains are negligible. 我还要提一下,性能提升可以忽略不计。

Source: Todd McLeod 资料来源: Todd McLeod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM