Force an image to fit and keep aspect ratio

前端 未结 2 678
天涯浪人
天涯浪人 2020-11-30 03:58

I want an image to fill the 100% of its container\'s width, and I want it to have a max-heigth property set to it, all this keeping the aspect ratio but allowing to lose any

2条回答
  •  忘掉有多难
    2020-11-30 04:58

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

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

提交回复
热议问题