Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to specify a max height or width for an image?

Tags:

html

css

I'd like to have an image to have either a height of 725 or a width of 500 and maintain it's aspect ratio. When I have images with a height of over 725 and thinner than 500 they get stretched out to fit a width of 500.

What is the best way to do this?

Below is what I am doing now:

<asp:Image Height="725" width="500" ID="img_DocPreview" /> 

Update: Changed it to this but have the same problem. If I specify just the height it will maintain the aspect ratio but it exceeds the max width of 500px that i want.

<img style="height:725px;width:500px;" id="img_DocPreview" src="Images/empty.jpg" /> 
like image 253
Abe Miessler Avatar asked Sep 09 '10 16:09

Abe Miessler


People also ask

How do you specify width and height of image?

The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.

Why we should specify the height and width of an image?

Adding the height and width attributes to your IMG SRC HTML tag allows the browser to know how much space to leave for an image. Without these values, the browser doesn't initially create a space for the image, which means elements surrounding the image are adjusted after it has loaded.

Which attribute specifies height for an image?

The height attribute specifies the height of an image, in pixels.

How do I limit the size of an image in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.


2 Answers

You can try this one

img{     max-height:500px;     max-width:500px;     height:auto;     width:auto; } 

This keeps the aspect ratio of the image and prevents either the two dimensions exceed 500px

You can check this post

like image 140
Daniel Nyamasyo Avatar answered Sep 17 '22 15:09

Daniel Nyamasyo


editied to add support for ie6:

Try

<img style="height:725px;max-width:500px;width: expression(this.width > 500 ? 500: true);" id="img_DocPreview" src="Images/empty.jpg" /> 

This should set the height to 725px but prevent the width from exceeding 500px. The width expression works around ie6 and is ignored by other browsers.

like image 31
Nico Burns Avatar answered Sep 19 '22 15:09

Nico Burns