I'm trying to create a thumbnail grid, that will be used mainly on mobile.
The image size is unknown : it comes from an Input, and size is dependant of the camera on the mobile phone. So I don't know in advance if a photo will be landscape or portrait. And thus the styling for both thumbnails should be identical.
For a landscape image, I want it to be shown the full height and horizontally centered. For a Portrait image I want to show the full width of the image and vertically centered.
LANDSCAPE IMAGE PORTRAIT IMAGE
+++++++
+ +
+ +
++++++=======++++++ =======
+ ======= + =======
+ ======= + =======
+ ======= + =======
+ ======= + =======
++++++=======++++++ =======
+ +
+ +
+++++++
== = actual thumbnail = part of the full image that will be shown
+ = full image size
I guess object-fit: cover;
would be best fit.
This works for the landscape image (see codepen : The men on the right is not showw, which is expected)
Unfortunately this doesn't work for the portrait image. The width is correctly scaled to the width of the thumb, but the image is not vertically centered. (see codepen : the top of the building is still visible while the man in front disappeared. What I expected is the top of the building no longer visible, and the head if the man completely visible. I could fix this with a translate, but then the same code won't work with the portrait.
See codepen, using Tailwind https://codepen.io/pasdut/pen/MWbzvzX
Update : I don't know in advance if an image wille be portrait or landscape