.block {
width: 500px;
height: 300px;
}
img {
max-width: 100%;
object-fit: contain;
}
<div class="block">
<img src="http://dialog.localhost/upload/postomat/04a/04aeacfed49dd7eda5f469f57b822f7f.jpg" alt="">
</div>
在我看来,图片应该缩小以适应块,但这不会发生。为什么?
将您的图像指定为 block
指定高度和宽度:
width: 100%;
height: 100%;
使用object-fit: cover
或object-fit: fill
.block {
width: 500px;
height: 300px;
border: 1px solid black;
}
.block img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="block">
<img src="https://material.angular.io/assets/img/examples/shiba2.jpg" alt="">
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句