【发布时间】:2021-07-16 00:03:02
【问题描述】:
一个图像可以用这个代码适应父级的宽度:
#img-parent {
max-width: 100px;
}
#header-img {
width: 100%;
}
<div id="img-parent">
<img src="https://cdn.pixabay.com/photo/2013/09/16/22/23/yellow-183052_1280.png" id="header-img" alt="">
</div>
但是我设置父容器的高度并不关心:
#img-parent {
max-height: 100px;
}
#header-img {
height: 100%;
}
<div id="img-parent">
<img src="https://cdn.pixabay.com/photo/2013/09/16/22/23/yellow-183052_1280.png" id="header-img" alt="">
</div>
为什么会发生这种情况,如何使图像适应父母的身高?
【问题讨论】:
-
为父级设置
height值,而不仅仅是max-height。