Transferred from: http://www.igooda.cn/jsdt/20130827355.html
Background-size requires two values, which can be either pixels (px), percent (%), or auto, or cover and contain. The first value is the width of the background graph, and the other value specifies the height on the background graph, and if only 1 values are set, the 2nd default is auto, except when the values are cover and contain.
cover: maintains the image's width-to-height ratio, scaling the picture to exactly cover the defined background area, with one side and the same background.
Html:
<div class= "div" >
Css:
. div{width:160px;height:120px;border:1px Solid #ccc;p Adding:10px;background-image:url (bg.jpg); Background-size: Cover;}
The effect is as follows:
contain: maintains the width and height ratio of the image, zooms the picture to a width or height to fit the area that defines the background, but the background is still within the defined area and is included.
Css:
. div{width:160px;height:120px;border:1px Solid #ccc;p Adding:10px;background:url (bg.jpg) no-repeat;background-size : Contain;font-family:microsoft Yahei;}
The effect is as follows:
Length
. div{background-size:150px 80px;}
Display effect:
Here the background image is set to a wide height, the image will be stretched or scaled directly, not to maintain the original proportions. If you set only one value and the other defaults to auto, it will scale to the original proportions of the picture.
Percentage
. div{background-size:40% 60%;}
It is important to note that the image size is not calculated as a percentage of the background image size, but rather as a percentage of the element that loads the background graph.
Browser compatibilityBrowser support: IE (9), Firefox, Chrome, Opera, Safari.
/*mozilla*/-moz-background-size:auto| | <length>| | <percentage>| | cover| | contain;/*webkit*/-webkit-background-size:auto| | <length>| | <percentage>| | cover| | contain;/*presto*/-o-background-size:auto| | <length>| | <percentage>| | cover| | contain;/*w3c*/background-size:auto| | <length>| | <percentage>| | cover| | contain;
Note: Although I have written above that each browser needs to add its own unique way, but does not mean that the style must be added, in the case I wrote did not write their own style for each browse, still do well.
[html]background-size can be scaled smaller