How to use simple code to achieve the image adaptive display effect? (CSS code example)

Source: Internet
Author: User
In the construction of the site, the basic requirements of the site can be self-adaptive effect, then the implementation of image adaptation is a very important operation. The display of a picture can not be separated from the width and height of these two values. First of all, we need to understand the relevant knowledge points in CSS about the image width and height value properties. Here is a brief summary of the people.

1. The Width property sets the length of the element. This property defines the width of the element's content area.

Possible values are: auto auto width, length px, CM and other units defined width;% percent width; inherit specifies that the value of the width property should be inherited from the parent element.

2. Height property sets the altitude of the element. This property defines the height of the element's content area.

Possible values are: auto automatic height. length px, CM and other units define the height. % percent height. Inherit specifies that the value of the height property should be inherited from the parent element.

First, the CSS background Image Adaptive screen code is as follows (via Background-image: () introduce the picture):

. Img{width:100%;height:100%;background-position:center;background-size:cover;background-repeat:no-repeat;} Background-image:url (picture path);

The above code can make the CSS image adaptive non-deformation, the principle here is to set the percentage.

Second, the CSS Image Adaptive screen code is as follows (through the IMG tag into the picture):

img {height:auto; width:auto\9; width:100%;}

The above code allows the IMG image to be adaptive to the div size, which is the screen size. Note Here is the use of Width:auto, that is, the width of the automatic meaning.
Then \9 is a hack CSS style, this in the normal CSS code after adding "\9" way, only IE browser can be recognized, other browsers will ignore this statement. In this way, the browser can be differentiated to achieve the purpose of compatible browsers.

This article about CSS how to make the picture adaptive introduction, I hope to have the need for friends to help.

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.