Using CSS to achieve proportional scaling of pictures

Source: Internet
Author: User

With the growth of smartphones, mobile web sites can also do more personalized, recently in the revision of their own mobile website
This one, encountered a website picture size problem, mobile phone screen is relatively small, how to upload the rules of the picture on the phone show more beautiful?
Then use the powerful features of CSS.
First enclose the code:
IMG {
max-width:500px;
Myimg:expression (Onload=function () {
This.style.width= (This.offsetwidth > 500)? " 500px ":" Auto "}
);
}
Let's analyze some of these numbers.
This set the maximum size is 500px, more than 500px automatically proportional to the reduction to 500px
The above code may not be effective in some browsers, such as the existence of a highly automatic scaling problems
Then you can try the following code
We attach a piece of code that is valid for the overall situation, if it is for the body content, add a limit before the img
For example I write below, otherwise may cause the logo and so on picture distortion
. Context img{
max-width:500px;
IE7, FF and other non-IE browsers the maximum width of 500px;
width:500px;
The size of the picture in all browsers is 500px;
Width:expression (document.body.clientwidth>300?) 300px ":" Auto ");
When the picture size is greater than 500px, automatically reduced to 500px;
Overflow:hidden;
}

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.