Specifies the size of the background images.
Overview tableauto
backgroundSize
background-size: auto
background-size: contain
background-size: cover
background-size: length
background-size: percentage
cm
, mm
, in
, pt
, or pc
) or a relative units designator (em
, ex
, or px
).
For more information about the supported length units, see the CSS Values and Units Reference.
Basic list of syntax examples for background-size.
background-size: cover
background-size: contain
background-size: 50%
background-size: 3em
background-size: 12px
background-size: auto
background-size: 50% auto
background-size: 3em 25%
background-size: auto 6px
background-size: auto auto
background-size: auto, auto
background-size: 50%, 25%, 25%
background-size: 6px, auto, contain
background-size: inherit
HTML structure of a series of <div>
s that are identical except that they have different background-size
values applied to the background image.
<p>Original image is 273 x 286 px, and has a transparent area around the outside of roughly 45px.</p>
<div class="one"><code>background-size: auto auto;</code></div>
<div class="two"><code>background-size: contain;</code></div>
<div class="three"><code>background-size: cover;</code></div>
<div class="four"><code>background-size: 20% 25%;</code></div>
<div class="five"><code>background-size: 100px 400px;</code></div>
<div class="six"><code>background-size: 100% 250px;</code></div>
<div class="seven"><code>background-size: 100% 250px, 20% 25%;</code></div>
CSS applied to the HTML example seen above.
div {
width: 17%;
height: 200px;
padding: 10px;
border-radius: 20px;
box-shadow: 2px 2px 10px rgba(0,0,0,0.75);
float: left;
margin: 0 20px 20px 0;
background-color: rgba(0,0,0,0.25);
background-image: url(/logo/wplogo_transparent_xlg.png);
}
code {
background-color: rgba(255,255,255,0.7);
padding: 2px;
border-radius: 5px
}
.one {
background-size: auto auto;
}
.two {
background-size: contain;
}
.three {
background-size: cover;
}
.four {
background-size: 20% 25%;
}
.five {
background-size: 100px 400px;
}
.six {
background-size: 100% 250px;
}
.seven {
background-image: url(/logo/wplogo_transparent_xlg.png), url(/logo/wplogo_transparent_xlg.png);
background-size: 100% 250px, 20% 25%;
}
Notes Remarks
An auto
value for one dimension is resolved by using the image’s intrinsic ratio and the size of the other dimension. If either of these values is not available, the image’s intrinsic size is used. If the image’s intrinsic size is not available, it is assigned the value of 100%. If both values are auto
, use the intrinsic width, height, or both, of the image. If the image has neither an intrinsic width nor an intrinsic height, its size is determined as for contain
. Negative values are not allowed. In Windows Internet Explorer 9, the background of a box can have multiple layers. The number of layers is determined by the number of comma-separated values in the background-image property. Each of the images is sized, positioned, and tiled according to the corresponding value in the other background properties (background-attachment, background-clip, background-origin, background-position, background-repeat, and background-size). The first image in the list is the layer closest to the user, the next one is painted behind the first, and so on.
background-size
LayoutRect
style
Reference
Mozilla Developer Network : Article
Microsoft Developer Network: [Windows Internet Explorer API reference Article]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4