html Object-fit:包含div元素

swvgeqrz  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(103)

<img/>有css prop:object-fit
当值为contain时:img将显示满,并且有白色(如果图像的比例与<img/>不同)。
现在有一个div,有一个比率3/2。父节点是divheightwidth是动态的,父节点没有比率。
怎么能让孩子们总是在父母(包含)在任何比例的父母?

disbfnqx

disbfnqx1#

您可以设置img { max-width: 100%; max-height: 100%; }

img {
  max-width: 100%;
  max-height: 100%;
}

div {
  outline: solid 1px blue;
  margin: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

div:nth-child(1) {
  width: 100px;
  height: 100px;
}

div:nth-child(2) {
  width: 160px;
  height: 60px;
}

div:nth-child(3) {
  width: 60px;
  height: 80px;
}
<div><img src="https://picsum.photos/300/200" alt=""></div>
<div><img src="https://picsum.photos/300/200" alt=""></div>
<div><img src="https://picsum.photos/300/200" alt=""></div>

相关问题