[Html]background-size can scale size

Source: Internet
Author: User

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 compatibility

Browser 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.