CSS image scaling to fit within area not distort

后端 未结 6 681
栀梦
栀梦 2020-12-30 20:19

Is there a way with CSS or otherwise of making an image fit within an area. Lets say I have multiple images of different sizes and I want them all to fit within a div of 150

6条回答
  •  轮回少年
    2020-12-30 20:44

    Hope I am not late to the party ;)

    img {
          width:inherit;
          height:inherit;
          object-fit: cover;
    }
    

    if however you want the full image to display, use the code below

    img {
          width:inherit;
          height:inherit;
          object-fit: contain;
     }
    

    this should do the trick.

提交回复
热议问题