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

前端 未结 15 1441
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  悲&欢浪女
    2020-11-28 01:26

    You can achieve this using css flex properties. Please see the code below

    .img-container {
      border: 2px solid red;
      justify-content: center;
      display: flex;
      flex-direction: row;
      overflow: hidden;
      
    }
    .img-container .img-to-fit {
      flex: 1;
      height: 100%;
    }

提交回复
热议问题