The order does not matter. But order matters.
TL; DR;
The order does not matter, it is what it is in this documentation on MDN and is also confirmed by the W3C specifications. I will repeat this part verbatim:
A double bar ( || ) separates two or more parameters: one or more of them must occur in any order.
But do not proceed to this conclusion!
matters . Take your background syntax example:
[ <bg-layer> , ]* <final-bg-layer> where <bg-layer> = <bg-image> || <position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box> and <final-bg-layer> = <bg-image> || <position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box> || <background-color>
And, since it specifies the syntax of all the possible properties for multiple backgrounds, reduce complexity by choosing one background. It looks like this:
<bg-image> || <position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box> || <background-color>
Using this in a piece of code and linking to the documentation mentioned in your question and another answer, you can believe that all of these properties, which are separated by a double line ( || ), can be omitted and can happen in any order. Right
Wrong.
Although MDN is a good reference, it is not complete and may sometimes omit important details. In this particular case, both of these links ...
... specify the same syntax and explain all the properties, but leave one detail.
Now take a close look at the property called box . There are two box properties, separated by a double bar ( || ). This should mean that you can either omit box or specify box in any order. But this is not entirely correct. Although you, of course, may not indicate box at all from the transcript, you cannot put them in any order if you want without getting unintended results. This is not indicated in the MDN link to background above.
But, in another background-clip link by the same MDN here:
- MDN ref: https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip
It states:
Canonical order: a unique nondual order defined by formal grammar
This means that the order is determined by the ambiguity of the properties. If the properties are not ambiguous, then order or omission does not matter. If the properties are ambiguous, then order and / or omission matters.
In your abbreviated background example, two box properties (if any) are interpreted as the first of which is background-origin , and the second is background-clip .
If you read another source of documentation here:
- Opera dev ref: https://dev.opera.com/articles/css3-borders-backgrounds-boxes/
It states:
There are a few things you need to know when using the new shorthand background. If only one field value is specified, both background clips and background-origin are set to this value. If two are indicated, then first is used to start, and the second is used for the clip.
- And W3c is here: http://www.w3.org/TR/css3-background/#the-background-origin
Indicates the following:
Please note that if 'background-clip is' padding-box,' background-origin "border-box", "background-position" is "upper left" (initial value), and the element has a non-zero border, then upper and left The background image will be cropped.
So, if you omit one of the values, it will be interpreted as both background-origin and background-clip . But, if you provide both values, then the order is important, otherwise you will get strange results. Not only this, other background-position property affects the behavior.
This can be easily demonstrated using this example in the snippet below. In the first div background image is positioned 15px from the pad, but continues across the border. Note that the first value of box is padding-box , and the second is border-box . In the second div background image is a 15px position from the border, but stops where the fill ends. Notice that the first value is border-box and the second value is padding-box . In the third div background image starts at the border and continues along the border. In this case, one value is omitted, and both background-origin and background-clip get the value of border-box .
Excerpt:
div { width: 120px; height: 120px; display: inline-block; } #r { border: 15px #f00; border-style: dotted dashed dashed dotted; background: url(http://placehold.it/150) no-repeat 15px 15px padding-box border-box; } #g { border: 15px #0f0; border-style: dotted dashed dashed dotted; background: url(http://placehold.it/150) no-repeat 15px 15px border-box padding-box; } #b { border: 15px #00f; border-style: dotted dashed dashed dotted; background: url(http://placehold.it/150) no-repeat 15px 15px border-box ; }
<div id="r">Red</div> <div id="g">Green</div> <div id="b">Blue</div>
Bottomline:
Well refer to the documentation. But it’s better to even consult with several sources. This answer refers to 5 sources. Read the W3C specifications, but not just in one place, read all links to related properties to see how other properties and even order can influence one property. And do not forget to try the sample, you may never know that a surprise can wait for you!
.