2

The following code is a perfect solution to make an image:

  • fit in a given container
  • be centered horizontally and vertically in the container
  • use the maximum possible size
  • without changing the aspect ratio

Perfect, except that the library html2canvas doesn't work with object-fit: contain; (see also Html2canvas Ignores Object fit for image).

Question: how to obtain the same behaviour with standard CSS rules, without the relatively-new object-fit?

.container { width: 200px; height: 200px; background: blue; }
img { height: 100%; width: 100%; object-fit: contain; }
1:
<div class="container"><img src="https://via.placeholder.com/100x50"></div>
2:
<div class="container"><img src="https://via.placeholder.com/50x100"></div>
3:
<div class="container"><img src="https://via.placeholder.com/500x300"></div>
4:
<div class="container"><img src="https://via.placeholder.com/300x500"></div>

I've tried various answers from How do I auto-resize an image to fit a 'div' container? but they don't work in all the 4 cases shown before.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • Not an answer as this example will work JUST when image bigger than container. would be easy to solve the first 2 containers with a bit of javascript but not a that tag in your question https://jsfiddle.net/9stp0mjr/ – Alvaro Menéndez Apr 06 '22 at 14:06
  • @AlvaroMenéndez Do you think JS is mandatory if we want to 1) enlarge the img to the largest possible size and 2) avoid `object-fit`? I have added the tag `[javascript]` now. – Basj Apr 06 '22 at 14:09
  • 1
    I think so but as I'm not 100% sure You may better wait a couple hours in case someone else have a css only solution. Sometime people make some very creative ways to use CSS (often called hacks). but the js would be quite easy and I'm far from beign even decent. Just get the width and the height of the image and if < of container width or height add the container width to the image (or height if height > width.. portrait) – Alvaro Menéndez Apr 06 '22 at 14:11
  • I think you can post an answer with this @AlvaroMenéndez! – Basj Apr 06 '22 at 14:29
  • Do you want the images to display larger than their native resolution? Or should their maximum width (and height) be the smaller of the container width or image width? – Quasipickle Apr 06 '22 at 15:50
  • Also, does html2canvas work better with background images? – Quasipickle Apr 06 '22 at 15:54
  • @Quasipickle `if imageheight < containerheight and imagewidth < containerwidth`, the image should be enlarged to occupy full width or height of the container (keeping the aspect ratio), see my examples #1 and #2 in the code snippet. About your 2nd question, the only thing I know is that html2canvas doesn't like `object-fit` :) – Basj Apr 06 '22 at 16:40
  • 1
    Ok, I just wanted to verify that enlarging was intended & desired. Try something like this maybe? https://codepen.io/quasipickle/pen/abEYONj?editors=1100 I don't want to put it as an answer because I don't know if it'll work with html2canvas. – Quasipickle Apr 06 '22 at 16:56

2 Answers2

1

Use background-image instead

.container { 
  width: 200px; 
  height: 200px;
  background-color:blue;
  background-image:var(--bg-img);
  background-repeat:no-repeat;
  background-size:contain;
  background-position: center center;
  
}
<div class="container" style = "--bg-img:url(https://via.placeholder.com/100x50)"></div>
Quasipickle
  • 4,383
  • 1
  • 31
  • 53
0

As I said before in the comments maybe not possible without javascript for images smaller than containers but in case you won't find a css solution I have made this easy example:

https://jsfiddle.net/zq7vw2y8/1/

Edit. I have made this other JSFIDDLE with much compact script and no jquery

Alvaro Menéndez
  • 8,766
  • 3
  • 37
  • 57