我有以下 html :
<div class="main-container">
<h1>A title</h1>
<p>Some text</p>
<div class="sub-container">
<img src="">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
使用以下 CSS:
.main-container{
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.sub-container{
flex-grow:2;
background-color: green;
}
Run Code Online (Sandbox Code Playgroud)
请注意,我不知道“主容器”上方容器的大小。我使用 flex 是因为我希望“主容器”中的 div 占据底部的所有剩余空间。
我想要的是在“子容器”中有一个图像,它在两个方向(高度和宽度)都适合其父级的大小。现在,如果我将图像添加到“子容器”中,它会溢出并且根本不会缩放。我知道 flex 只适用于直接的孩子(即“子容器”而不是里面的图像)。我也尝试在“子容器”上使用 flex,但我无法取得任何令人满意的结果。
你可以使用这个类:
.img-fluid {
max-height:100%;
height:auto;
}
Run Code Online (Sandbox Code Playgroud)
不要忘记将 .img-fluid 添加到您的img