CSS 通过将图像高度设置为容器高度,使图像适合其容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16037566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:29:16  来源:igfitidea点击:

Make an image fit its container by setting the image height to the containers heights

cssimageaspect-ratio

提问by Masu

Say I have container.a and it has some height in pixels, say I have another container.b within container.a that is 80% of container.a, now say I want to fit an image that has some height in pixels into container.b, how can I make the image be the height of container.b, and then maintain the width aspect ratio using CSS?

假设我有 container.a 并且它有一些像素高度,假设我在 container.a 中有另一个 container.b,它是 container.a 的 80%,现在说我想将具有一定像素高度的图像放入容器中.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 { ? }

回答by Akhil Thesiya

Give 100% width of image

给图像的 100% 宽度

 .image.a{width:100%;}

回答by otinanai

Use this:

用这个:

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