Control the image size on the webpage

Source: Internet
Author: User

In web pages, sometimes the referenced image is too large, so that only a part of the image can be displayed. To scale the image, follow these steps:

1. Use css to control the image size:
 
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> test </title>
<Style type = "text/css">
Img {
Border: 0;
Margin: 0;
Padding: 0; www.2cto.com
Max-width: 1280px;
Width: expression (this. width & gt; 1280? "1280px": this. width );
Max-height: 720px;
Height: expression (this. height> 720? "720px": this. height );
}
</Style>
</Head>
<Body>
<Div>

</Div>
</Body>
</Html>
Alternatively, you can use the following methods:

[Html]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> test </title>
</Head>
<Body>
<Div style = "width: 200px; height: 100px;">
</Div>
</Body>
</Html>
The two methods are scaled proportionally according to the original image size.

 

2. Use js to implement:

[Html]
<Body>
<Div id = article> </div>
<Script type = "text/javascript">
// Scale the image to a proper size
Function ResizeImages ()
{
Var myimg, oldwidth, oldheight;
Var maxwidth = 1280;
Var maxheight = 720;
Var imgs = document. getElementById ('Article'). getElementsByTagName ('img '); // if your defined id is not article, modify it here
 
For (I = 0; I Myimg = imgs [I];
 
If (myimg. width> myimg. height)
{
If (myimg. width> maxwidth)
{
Oldwidth = myimg. width;
Myimg. height = myimg. height * (maxwidth/oldwidth );
Myimg. width = maxwidth;
}
} Else {
If (myimg. height> maxheight)
{
Oldheight = myimg. height;
Myimg. width = myimg. width * (maxheight/oldheight );
Myimg. height = maxheight;
}
}
}
}
// Scale the image to a proper size
ResizeImages ();
</Script>
</Body>
In this way, you can set the width and height of the image.

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.