How can I fill a div with an image while keeping it proportional?

前端 未结 15 1417
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 00:56

I found this thread — How do you stretch an image to fill a

while keeping the image's aspect-ratio? — that is not entirely the thing that I want.

I h

15条回答
  •  萌比男神i
    2020-11-28 01:20

    It's a bit late but I just had the same problem and finally solved it with the help of another stackoverflow post (https://stackoverflow.com/a/29103071).

    .img {
       object-fit: cover;
       width: 50px;
       height: 100px;
    }
    

    Hope this still helps somebody.

    Ps: Also works together with max-height, max-width, min-width and min-height css properties. It's espacially handy with using lenght units like 100% or 100vh/100vw to fill the container or the whole browser window.

提交回复
热议问题