Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit image to parent div with scaling and without stretching

Tags:

html

css

sass

image

I have a div that has a certain fixed width and height. I want to fit inside an image (it's dimensions are previously unknown) without stretching it (keep aspect ratio). Object fit would work but for the sake of image being wrapped inside of another wrapper div (to apply shadow), so it cannot be used this time.

To make it clear: I can't use object-fit, nor making it a background image.

How is this done properly with CSS?

further images to demonstrate my intentions: (blue rectangle on the right represents the original image in its original size, on the left it is how it should display inside it's dashed parent) enter image description here enter image description here enter image description here

like image 264
maklmor Avatar asked Oct 18 '25 17:10

maklmor


1 Answers

If i understood correctly this should do it

object-fit: contain;
like image 150
Kristian Kaleta Avatar answered Oct 20 '25 07:10

Kristian Kaleta