90

Can you do this? I'm having users upload images into a container that is width:100%, but I don't want the images to be larger than their original size. Many thanks!

nikk wong
  • 8,059
  • 6
  • 51
  • 68
  • 3
    The image should naturally be the size that it is. Are you setting the image to width:100 also? – Theodore Enderby Jun 13 '14 at 04:30
  • 3
    No. You cannot. There is no way for CSS to know the actual size of a random image. You'd need javascript to detect the image size and scale the image. – Koala Yeung Jun 13 '14 at 04:32

2 Answers2

141

Just don't set the width of the image, only the max-width.

img {max-width:100%; height:auto}

(height:auto is not really necessary, since auto is the default, but I put it in there as a reminder to myself that I want the image to have its natural proportions.)

This snippet has two boxes, one that is smaller than the image and one that is larger. As you can see, the image in the smaller box gets scaled down, while the one in the bigger box has its normal size.

div {border:2px outset green; margin:6px 0}
.box1 {width:100px; height:70px;}
.box2 {width:200px; height:100px;}
img {max-width:100%; height:auto}
<div class="box1">
    <img src="https://i.stack.imgur.com/FuQYf.png" alt="" />
</div>
<div class="box2">
    <img src="https://i.stack.imgur.com/FuQYf.png" alt="" />
</div>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 10
    If someone is wondering (like me) how to override an inherited width setting, just add width:auto in the new class that you will assign to the image. – grasskode Mar 29 '16 at 15:10
  • 8
    This sets the max width to the size of the container, rather than to the original (or natural) size of the image. – Tom Mar 26 '20 at 22:32
  • @Tom that's right, as per the conditions in the OP's question. Do you have a different scenario? In that case, it might be better to ask a new question. – Mr Lister Mar 27 '20 at 07:56
  • 3
    That’s not how I read the OPs requirements. He’s asking for it to not exceed the original / natural dimensions, rather than to not exceed the container. – Tom Mar 28 '20 at 11:00
  • @Tom You read the OP's requirements correctly, and that's what my solution does; if `max-width` is set, but `width` is not, the image cannot become larger than its natural width, only smaller (if the container is smaller than the image). – Mr Lister Mar 28 '20 at 12:00
  • @MrLister nowhere does it say that the container size is equal to the image's natural size, so your answer doesn't answer the OP's question as far as I understand – Tom Mar 29 '20 at 17:17
  • @Tom The OP did not ask for a container equal to the image size, but for an image no larger than its original size. – Mr Lister Mar 30 '20 at 08:11
  • Exactly, so your answer that sets the max width to the size of the container, rather than to the original (or natural) size of the image, is not an answer to the question. – Tom Mar 31 '20 at 11:00
  • @Tom You are mistaken about what `max-width` does. – Mr Lister Mar 31 '20 at 12:21
  • 2
    @MrLister https://developer.mozilla.org/en-US/docs/Web/CSS/max-width max-width as a percentage defines the max-width as a percentage of the containing block's width – Tom Apr 01 '20 at 18:00
  • 1
    What is working here is the absence of setting a width, which typically means an image sizes itself according to native size. What max-width does is set a limit how big the display size can be. At least this is what happens in modern browsers in 2020, but I think this was commonly the case in 2014 as well. I think Mr Lister said as much already, just clarifying, – MiB Dec 01 '20 at 19:20
  • 1
    The suggestion is for landscape images, where width is the predominant factor. It would have to set max-height for a portrait image. In general, the choice will depend on the aspect ratio (H/W) but this would be impractical to find at run-time. – ACProctor Nov 06 '21 at 10:45
  • IF the image tag is inside a parent (or parents tree) element that has height or width defined, there is not way to set the width or max-width to it's natural size. you can set it only using javascript (see https://stackoverflow.com/questions/16342936/how-do-i-get-natural-dimensions-of-an-image-using-javascript-or-jquery) IF the image is inside of parents that does not have width/height set, then it will show it's natural size. – albanx Jan 05 '22 at 11:29
0

You can set the max width of the image in a style tag on the image itself. Then in the style sheet set the display type to "block", the width to whatever percentage of the container you want, and the height to auto. Then add margin: 0px auto to that and it should center perfectly and will never be larger that it's original size.

If these are user-uploaded images and you are using PHP for example, find the image width by using getImageSize() and add the style tag programmatically to the image.