Background-size attribute in css

Source: Internet
Author: User

Background-size attribute in css
Value Type of background-size: one or two values, which can be pixel px, percentage %, auto, or specific value cover or contain.

Background-size can be set to 2 values. One value is required and the other value is optional.
1st values are used to specify the width of the background image, and 2nd values are used to specify the height of the background image. If only one value is set for background-size, the default value of 2nd is auto height (the default height is auto, and the value increases with the applicable content. Generally, if you want to adjust the height, you do not need to set it) (except for the specific values of cover and contain ).

Div {
Background-image: url(test.png );
Background-repeat: no-repeat;
Background-size: 100px;
}

It is equivalent:

Div {
Background-image: url(test.png );
Background-repeat: no-repeat;
Background-size: 100px auto;
}

View the DEMO: background-size value definition. When you use firebug to capture the instance node, you will find that the second value is automatically added and the value is auto. Of course, you can also manually set 2nd values to auto, and then compare them with the DEMO instances, they will have the same effect.

Specific value of background-size:

Cover: maintain the high aspect ratio of the image, and scale the image to a region that exactly overwrites the defined background;
Contain: maintain the high aspect ratio of the image, and scale the image to the width or height to adapt to the area that defines the background;
Cover value:

Div {
Background-image: url(test.png );
Background-repeat: no-repeat;
Background-size: cover;
}

In the preceding example, the background image overwrites the entire div area. View the DEMO: background-size: cover.

Contain value:

Div {
Background-image: url(test.png );
Background-repeat: no-repeat;
Background-size: contain;
}

In the preceding example, the background image scales to any side of the width or height to adapt to the div area. View the DEMO: background-size: contain.

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.