使图像适合其容器的图像高度设置为容器的高度(Make an image fit its contai

2019-08-31 08:18发布

说我有container.a和它在像素一定高度,说我有container.a是container.a的80%之内的另一个container.b,现在说我要适应的是在像素倒入容器中一定高度的图像.B,我怎样才能使该图像是container.b的高度,然后使用CSS维持宽度的纵横比?

<div class="container.a">
  <div class="container.b">
    <img class="image.a" src="my_image.png" />
  </div>
</div>

.container.a { width: 200px; height: 300px; }
.container.b { width: 80%; height: 80%; }
.image.a { ? }

Answer 1:

给图像的100%的宽度

 .image.a{width:100%;}


Answer 2:

用这个:

.image.a { height:100%; width:auto }


文章来源: Make an image fit its container by setting the image height to the containers heights