How to set max width of an image in CSS

HtmlCssImageImage Resizing

Html Problem Overview


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).

Html Solutions


Solution 1 - Html

You can write like this:

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

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

Solution 2 - Html

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">

Solution 3 - Html

The problem is that img tag is inline element and you can't restrict width of inline element.

So to restrict img tag width first you need to convert it into a inline-block element

img.Image{
    display: inline-block;
}

Solution 4 - Html

Given your container width 600px.

If you want only bigger images than that to fit inside, add: CSS:

#ImageContainer img {
    max-width: 600px;
}

If you want ALL images to take the avaiable (600px) space:

#ImageContainer img {
    width: 600px;
}

Solution 5 - Html

Try this

 div#ImageContainer { width: 600px; }
 #ImageContainer img{ max-width: 600px}

Solution 6 - Html

Your css is almost correct. You are just missing display: block; in image css. Also one typo in your id. It should be <div id="ImageContainer">

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

<div id="ImageContainer">
    <img src="http://placehold.it/1000x600" class="Image">
</div>

Solution 7 - Html

Wrap the element in a div with the fixed width/height:

<div style="width: 600px;">
  <img src="whatever" />
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1315305View Question on Stackoverflow
Solution 1 - HtmlsandeepView Answer on Stackoverflow
Solution 2 - HtmlMerle_the_PearlView Answer on Stackoverflow
Solution 3 - HtmlKamlesh UikeyView Answer on Stackoverflow
Solution 4 - HtmlLeo ArmentanoView Answer on Stackoverflow
Solution 5 - HtmlKrishView Answer on Stackoverflow
Solution 6 - HtmlRhythm RupareliaView Answer on Stackoverflow
Solution 7 - HtmljstaabView Answer on Stackoverflow