The backgroundSize
property sets or gets the size of the background images.
backgroundSize |
Yes | 10 | Yes | Yes | Yes |
Return the backgroundSize property:
var v = object.style.backgroundSize
Set the backgroundSize property:
object.style.backgroundSize='auto|length|cover|contain|intial|inherit'
The background-size Values:
Value | Description |
---|---|
length | Sets the width/height of the background image. The first value is width, the second value is height. Missing value is default to "auto". Negative lengths are not allowed. |
percentage |
Sets the width/ height of the background image in percent of the container. The first value is width, the second value is height. Missing value is default to "auto". Negative percentages are not allowed. |
contain | Scales the image, preserving the aspect ratio, to the largest size that can fit. |
cover | Scales the image, preserving the aspect ratio, to the smallest size that can fit. |
auto | Display the image at full size(default). |
Default Value: | auto |
---|---|
Return Value: | A string representing the background-size property |
CSS Version | CSS3 |
The following code shows how to set the size of a background image.
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {<!--from www .j av a 2 s.c o m-->
border: 1px solid black;
width: 300px;
height: 300px;
background: url('http://java2s.com/style/demo/border.png') no-repeat;
}
</style>
</head>
<body>
<button onclick="myFunction()">test</button>
<div id="myDIV">Hello</div>
<script>
function myFunction() {
document.getElementById("myDIV").style.backgroundSize = "60px 120px";
}
</script>
</body>
</html>
The code above is rendered as follows: