html - 图像溢出固定高度的 flex 容器
问题描述
我正在使用固定高度的 flex 容器来进行布局。在容器中,共有三个组件:页眉、内容、页脚。我想强制内容使用剩余的高度(即内容高度=固定高度减去页眉和页脚)。在我的内容中,它将包括一张图片和一些文字。
但是,即使提供 max-height: 100% 来限制高度,图像总是会溢出固定高度的弹性容器,但我想将页眉、内容和页脚放入固定的弹性容器中。
有谁知道如何修理它?
代码:https ://codepen.io/mrchung402/pen/wvGPJxz
<div class="container">
<div class="header">
header
</div>
<div class="content">
<div class="my-img">
<img src="http://via.placeholder.com/274x295">
</div>
<div class="my-text">
my-text
</div>
</div>
<div class="footer">
footer
</div>
</div>
.container {
border: solid 1px red;
display: flex;
max-width: 500px;
height: 200px;
flex-direction: column;
}
.header {
flex: 0 0 auto;
background: #A7E8D3;
}
.content {
display: flex;
flex-direction: column;
}
.footer {
flex: 0 0 auto;
background: #D7E8D4;
}
.my-img {
max-height: 100%;
height: auto;
max-width: 100%;
}
img {
max-height: inherit;
height: inherit;
}
.my-text {
background: #C7A8D4;
}
解决方案
而不是造型.my-img
,你应该造型.my-img img
.my-img img{
height: 100%;
width: 100%;
}
推荐阅读
- c - 重新定义 + 冲突类型错误,但没有重新定义或实际冲突
- python - 在运算符中使用 XCOM 值
- php - 如何添加随机名称以使文件不会替换 php
- python - 如何使用自定义数据加载器在 fastai 数据包中加载测试集?
- javascript - 使用 PHP 和 JS 确认更改 SELECT
- java - 如何避免两个对象实际上相同(具有相同的内存地址)?
- spring-boot - 如何在使用“云原生 buildpacks”创建映像时将自定义文件添加到 docker 文件系统
- c# - 在运行时更改 MicrosoftAccountAuthenticationOptions
- sql - 将 2 个月的两行合并为 1 个月的一行,将空值合并为一行
- python - Sqlalchemy 和 Flask-SQLAlchemy 泛型关联表