Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set max width of an image in CSS

On my website I would like to display images uploaded by user in a new window with a specific size (width: 600px). The problem is that the images may be big. So if they are bigger than these 600px, I would like to resize them, preserving the aspect ratio.

I tried the max-width CSS property, but it doesn't work: the image's size doesn't change.

Is there any way to solve this problem?

HTML:

<div id="ImageContainerr">     <img src="DisplayImage.do?ad_id=${requestScope.advert.id}" class="Image" /> </div> 

CSS:

img.Image { max-width: 100%;} div#ImageContainer { width: 600px; } 

I also tried setting the max-width: 600px for an image, but doesn't work. The image is streamed from a servlet (it's stored outside Tomcat's webapps folder).

like image 364
user1315305 Avatar asked Jun 18 '12 07:06

user1315305


People also ask

How do you maximize an image in CSS?

We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container. The max-width and max-height properties of CSS works better, but they are not supported in many browsers.

How do you scale an image in CSS?

Use the auto Value for Width and the max-height Property to Resize the Image in CSS. We can use the auto value to the width and set the max-height property to specify the width of an image to fit in a container. We will shrink the height of the image to the height of the container.


2 Answers

You can write like this:

img{     width:100%;     max-width:600px; } 

Check this http://jsfiddle.net/ErNeT/

like image 176
sandeep Avatar answered Sep 19 '22 21:09

sandeep


I see this hasn't been answered as final.

I see you have max-width as 100% and width as 600. Flip those.

A simple way also is:

     <img src="image.png" style="max-width:600px;width:100%"> 

I use this often, and then you can control individual images as well, and not have it on all img tags. You could CSS it also like below.

 .image600{      width:100%;      max-width:600px;  }       <img src="image.png" class="image600"> 
like image 33
Merle_the_Pearl Avatar answered Sep 20 '22 21:09

Merle_the_Pearl