CSS Background-size and background images fill Div

Source: Internet
Author: User

Background-size and background picture fill Div

In development, it is often necessary to use a picture as a div's background image to fill the div's needs

The value and explanation of Background-size

Background-size a total of three properties, respectively,

Background-size:cover
MDN Document Explanation: Zoom background picture to completely cover the background area, may not be visible in the background picture part. A keyword that's the inverse of contain. Scales the image as large as possible and maintains image aspect ratio (image doesn ' t get squished). The image "covers" the entire width or height of the container. When the image and container has different dimensions, the image is clipped either left/right or top/bottom.

The key note here is the two regions of the red, which are 它会保持图片的宽高比 and 当图像和容器具有不同的尺寸时,图像被左/右或顶部/底部裁剪。 will be combined with examples to illustrate

Background-size:contain
MDN Document Explanation: Zoom the background picture to fit completely into the background area, possibly partially blank in the background area. A keyword that scales the image as large as possible and maintains image aspect ratio (image doesn ' t get squished). Image is letterboxed within the container. www.97yingyuan.org when the image and container has different dimensions, the empty areas (either top/bottom of left/right) are filled with the background-color.

The key note here is the two regions of the red, which are 它会保持图片的宽高比 and 当图像和容器具有不同的尺寸时,空区域(左/右/上/右)填充背景色。 will be combined with examples to illustrate

Background-size:width-value,height-value;

Divided into and 固定大小 百分比 and auto , the fixed size is to write dead; auto is to scale the background image proportionally to the background image.

The percentage of the
MDN document explains the description <percentage> value, specifying the percentage of the background picture relative 背景区 (background positioning area). 背景区由background-origin设置, the default is the content area and padding for the box model, or it can be set to only the content area, or it also includes a border. If the attachment is fixed, the back scenic area is the browser visual region (that is, the viewport), excluding the scroll bar. cannot be a negative value.

CSS Background-size and background images fill Div

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.