1

.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>

在我看来,图片应该缩小以适合块,但这不会发生。为什么?

4

1 回答 1

3

指定您的图像block

指定高度和宽度:

width: 100%;
height: 100%;

使用object-fit: coverobject-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>

于 2021-03-25T08:57:30.560 回答