The syntax of (multiple) background shorthand is:
background: [ <bg-layer> , ]* <final-bg-layer>
Where
<bg-layer> = <bg-image> || <position> [ /
<bg-size> ]? || <repeat-style> || <attachment> ||
<box> || <box>
<final-bg-layer> = <bg-image> || <position> [ /
<bg-size> ]? || <repeat-style> || <attachment> ||
<box> || <box> || <'background-color'>
... If one <box> value is present then it sets both
‘background-origin’ and ‘background-clip’ to that value. If two values
are present, then the first sets ‘background-origin’ and the second
‘background-clip’.
- A double bar (||) separates two or more options: one or more of them must occur, in any order.
- An asterisk (*) indicates that the preceding type, word, or group occurs zero or more times.
- A question mark (?) indicates that the preceding type, word, or group is optional.
So the in order to include background-size
, you must specify the background-position
before it and place a /
in-between.
Assuming that you want to crop the image from the center instead of top-left you would write:
background: url(...) center / cover;
All four examples below use the same image:
h1 {
font: medium monospace;
}
.test {
display: inline-block;
}
.test-landscape {
width: 200px;
height: 150px;
}
.test-portrait {
width: 150px;
height: 200px;
}
.test-lefttop {
background: url(http://dummyimage.com/400x400/CCC/000000&text=%C3%97) left top / cover;
}
.test-center {
background: url(http://dummyimage.com/400x400/CCC/000000&text=%C3%97) center / cover;
}
<h1>background-position: left top<h1>
<div class="test test-landscape test-lefttop"></div>
<div class="test test-portrait test-lefttop"></div>
<h1>background-position: center</h1>
<div class="test test-landscape test-center"></div>
<div class="test test-portrait test-center"></div>