Javascript examples for CSS Style Property:backgroundSize
The backgroundSize property sets or gets the background images size.
Value | Description |
---|---|
auto | Default value. The background-image contains its width and height |
length | Sets width/height of the background image. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto" |
percentage | Sets the width/height in percent of the parent element. The first value sets the width, the second value sets the height. If only one value is given, the second is set to "auto" |
cover | Enlarge the background image to cover the element. |
contain | Scale the image to fit inside the content area |
initial | Sets this property to its default value. |
inherit | Inherits this property from its parent element. |
Item | Value |
---|---|
Default Value: | auto |
Return Value: | A String, representing the background-size property of an element |
CSS Version | CSS3 |
Set the size of a background image:
<!DOCTYPE html> <html> <head> <style> #myDIV {/*from w w w .j a v a2 s .c om*/ border: 1px solid black; width: 300px; height: 300px; background: url('http://java2s.com/resources/a.png') no-repeat; } </style> </head> <body> <button onclick="myFunction()">Test</button> <div id="myDIV"> <h1>Hello</h1> </div> <script> function myFunction() { document.getElementById("myDIV").style.backgroundSize = "60px 120px"; } </script> </body> </html>