693

Is there a way to resize (scale down) images proportionally using ONLY CSS?

I'm doing the JavaScript way, but just trying to see if this is possible with CSS.

Vadim Ovchinnikov
  • 10,848
  • 4
  • 43
  • 73
codingbear
  • 13,753
  • 20
  • 44
  • 64
  • 1
    Ethan Marcotte recently [investigated this issue very thoroughly](http://unstoppablerobotninja.com/entry/fluid-images/). The short answer is yes, it's possible, but not in all browsers. – Mark Hurd Apr 25 '09 at 00:02
  • If you don't want to burn bandwidth, [slimmage.js](http://github.com/imazen/slimmage) can help; it reads the resulting `max-width` value to adjust which size image is requested. – Lilith River Jul 08 '13 at 16:05
  • You can just set width of image, height is automaticly adjusted. – Rik Telner Apr 08 '14 at 18:54

18 Answers18

792

To resize the image proportionally using CSS:

img.resize {
    width:540px; /* you can use % */
    height: auto;
}
Zanon
  • 22,850
  • 18
  • 101
  • 110
Mkk
  • 7,961
  • 2
  • 13
  • 2
  • 8
    This works even if the img tag has a height and width attributes. +1 – Surreal Dreams Jun 11 '12 at 19:48
  • 68
    +1 You can also use `max-width` instead of `width` if desired. The key is to use `height:auto` to override any `height="..."` attribute already present on the image. – Phrogz Jul 30 '12 at 17:30
  • 3
    In my case I used, the height is fixed and I set the width to auto :) Thanks! – KarenAnne Jan 28 '13 at 08:45
  • 20
    A common use is to set `max-width: 100%; height: auto;` so large images don't exceed their containers width. – OdraEncoded Oct 26 '13 at 22:00
  • 4
    This doesn't work if you want to make the image bigger. The image lose its aspect ratio. Unless, of course, the container has the same aspect ratio of the image. – GetFree May 20 '14 at 21:04
  • This isn't working for me in IE. – Chase Roberts Jan 20 '15 at 21:16
  • what about `auto\9` for IE, does that still work? – Peter May 22 '18 at 07:48
  • This one does not work on image with height > width (e.g. Portrait) – Raynal Gobel Aug 28 '18 at 07:53
  • @SurrealDreams I thnk it is because the second declaration of a property override the first declaration, so here the browser would only receive the second `height:auto` declaration – Webwoman Jun 23 '19 at 02:21
  • This answer is incorrect. Nowhere does the OP say "proportionally to the containing div". So we have to assume s/he means scale image proportionally to itself. Which means the `transform: scale` answers are the only correct ones. – clayRay May 18 '20 at 00:13
252

Control size and maintain proportion :

#your-img {
    height: auto; 
    width: auto; 
    max-width: 300px; 
    max-height: 300px;
}
Cherif
  • 4,863
  • 7
  • 29
  • 52
  • The thing is that max-width and max-height are not interpreted by all browsers, like IE, right? – alexserver Apr 09 '14 at 19:15
  • 1
    @alexserver It works in MSIE >= 7: http://www.cssportal.com/css-properties/max-width.php – gouessej Jun 17 '15 at 08:31
  • 5
    This answer is way better... You can set max bound for both width and height while keeping the ratio. – olivarra1 Oct 30 '15 at 10:47
  • 4
    Yep, this is the correct answer to the question as it takes into account image orientation (i.e. landscape vs. portrait). Thanks! – katamayros Dec 14 '15 at 12:00
122

If it's a background image, use background-size:contain.

Example css:

#your-div {
  background: url('image.jpg') no-repeat;
  background-size:contain;
}
lenooh
  • 8,806
  • 5
  • 50
  • 44
83

Try

transform: scale(0.5, 0.5);
-ms-transform: scale(0.5, 0.5);
-webkit-transform: scale(0.5, 0.5);
orome
  • 35,904
  • 38
  • 156
  • 345
  • 8
    Actually this works pretty well if you have to scale down each image proportionally to its size. With this code images become 2x smaller. +1 BUT! With this solution the images still take up the space! – Mārtiņš Briedis Feb 26 '14 at 11:31
  • @MārtiņšBriedis: Isn't that the point? Is there a way to get images on the server to take up less space "using ONLY CSS" (see OP)?! – orome Sep 30 '14 at 12:10
  • 10
    Not that kind of space. The images still have the same bounds, width, height in document, only they "look" 2 times smaller. Imagine - an image with size 100x100. Then you apply scale(0.5), and it is the same as image 50x50, but with invisible 25px borders on both sides. The image still would take up the 100x100 space. – – Mārtiņš Briedis Sep 30 '14 at 12:25
  • @MārtiņšBriedis: Hmmm. I'll have to try it. It's been a while and it seemed to do what was wanted. – orome Sep 30 '14 at 12:30
  • 2
    Here's a jsfiddle for you: http://jsfiddle.net/oy6t2xgL/ – Mārtiņš Briedis Sep 30 '14 at 12:52
  • if both values are the same, only one needs to be given. it will be applied to both axes. [https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale](https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale) – clayRay May 18 '20 at 00:10
  • This, and the answer from cREcker are the only correct ones. Nowhere does the OP say "proportionally to the containing div". So we have to assume they mean scale image proportionally to itself. – clayRay May 18 '20 at 00:16
59

You can use object-fit property:

.my-image {
    width: 100px;
    height: 100px;
    object-fit: contain;
}

This will fit image, without changing the proportionally.

Andrii Bogachenko
  • 995
  • 10
  • 17
  • I use width 100% and height 100% and also object fit contain. and it works like a charm :). And object fit contain solution is simple – Faris Rayhan Nov 20 '16 at 06:47
  • note the browser support: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit as of now, no IE and android 4.4.4+. – qix Feb 06 '17 at 20:56
  • 4
    This should be the accepted answer – Zack Shapiro Jul 04 '17 at 21:12
  • `object-fit` works well - just seems to require both `height` and `width` or both `max-height` and `max-width` to not cause the image to overflow the container in either direction. – tschumann Nov 19 '18 at 00:18
54

Notice that width:50% will resize it to 50% of the available space for the image, while max-width:50% will resize the image to 50% of its natural size. This is very important to take into account when using this rules for mobile web design, so for mobile web design max-width should always be used.

UPDATE: This was probably an old Firefox bug, that seems to have been fixed by now.

andreszs
  • 2,589
  • 2
  • 22
  • 28
  • 7
    Um... this doesn't seem to be the case on the desktop at least: http://jsfiddle.net/nLh1oh5e/ – Campbeln Oct 09 '14 at 03:07
  • 6
    This does not appear to be the case for me either. – jasonleonhard Nov 24 '14 at 22:15
  • 1
    Thanks for calling this out. It makes a big difference. (at least, I noticed) – contactmatt Feb 24 '16 at 03:11
  • 1
    True, thanks. But how do you handle the size wrapper of the image? It is still acting like the image is 100% wide instead of 50%, causing an unwanted border. I'd like it to fit tightly around the downscaled image. – Micros Jun 09 '16 at 12:56
  • The problem with this approach is that `max-width` has nothing to do with the original dimensions of the image. `max-width` of `50%` means "50% of the width of the containing block". ([Docs on `width`](https://developer.mozilla.org/en-US/docs/Web/CSS/width)) – rinogo Feb 27 '20 at 22:12
50

To scale an image by keeping its aspect ratio

Try this,

img {
  max-width:100%;
  height:auto;
}
Prasanth K C
  • 6,307
  • 5
  • 33
  • 58
29

Revisited in 2015:

<img src="http://imageurl" style="width: auto; height: auto;max-width: 120px;max-height: 100px">

I've revisited it as all common browsers now have working auto suggested by Cherif above, so that works even better as you don't need to know if image is wider than taller.

older version: If you are limited by box of 120x100 for example you can do

<img src="http://image.url" height="100" style="max-width: 120px">
PetrV
  • 1,288
  • 2
  • 15
  • 29
24
<img style="width: 50%;" src="..." />

worked just fine for me ... Or am I missing something?

Edit: But see Shawn's caveat about accidentally upsizing.

Thanatos
  • 37,926
  • 14
  • 76
  • 136
Adrien
  • 2,900
  • 19
  • 17
24

The css properties max-width and max-height work great, but aren't supported by IE6 and I believe IE7. You would want to use this over height / width so you don't accidentally scale an image up. You would just want to limit the maximum height/width proportionately.

Shawn
  • 18,369
  • 19
  • 95
  • 151
17
img{
    max-width:100%;
    object-fit: scale-down;
}

works for me. It scales down larger images to fit in the box, but leaves smaller images their original size.

pheon
  • 2,255
  • 2
  • 22
  • 32
  • 1
    Interesting solution, though it's important to note the footprint of the image still takes up whatever the `height` attribute is. For example a 100x100 image can be scaled down to 80x80 but is then centered in the middle of the 100x100 footprint. – Scott Leonard Dec 19 '19 at 19:52
  • This answer is a better and scalable solution. – Sleek Geek Mar 16 '21 at 03:58
15

I believe this is the easiest way to do it, also possible using through the inline style attribute within the <img> tag.

.scaled
{
  transform: scale(0.7); /* Equal to scaleX(0.7) scaleY(0.7) */
}

<img src="flower.png" class="scaled">

or

<img src="flower.png" style="transform: scale(0.7);">
cREcker
  • 1,923
  • 1
  • 14
  • 13
  • 3
    I tested it, but I think it is very inefficient because doing that way, the border-box model remains the same. You probably won't wanna this, but I recognize this can be useful, maybe – Leonardo Maffei Sep 19 '18 at 16:49
13

Use this easy scaling technique

img {
    max-width: 100%;
    height: auto;
}
@media {
  img { 
    width: auto; /* for ie 8 */
  }
}
Geniusknight
  • 619
  • 1
  • 7
  • 19
4
img {
    max-width:100%;
}

div {
    width:100px;
}

with this snippet you can do it in a more efficient way

Private
  • 2,232
  • 18
  • 35
4

We can resize image using CSS in the browser using media queries and the principle of responsive design.

    @media screen and (orientation: portrait) {
img.ri {
    max-width: 80%;
  }
}

@media screen and (orientation: landscape) {
  img.ri { max-height: 80%; }
}
Moh K
  • 447
  • 4
  • 17
2

You always need something like this

html
{
    width: 100%;
    height: 100%;
}

at the top of your css file

nst
  • 3,841
  • 1
  • 29
  • 40
MikeStr
  • 83
  • 2
2

Try this:

div.container {
    max-width: 200px;//real picture size
    max-height: 100px;
}

/* resize images */
div.container img {
    width: 100%;
    height: auto;
}
ovod
  • 1,080
  • 1
  • 17
  • 29
1

image_tag("/icons/icon.gif", height: '32', width: '32') I need to set height: '50px', width: '50px' to image tag and this code works from first try note I tried all the above code but no luck so this one works and here is my code from my _nav.html.erb:
<%= image_tag("#{current_user.image}", height: '50px', width: '50px') %>

neo7
  • 89
  • 1
  • 10