html - 图像未采用父母身高
问题描述
我有以下内容html
:
<div class='fullHeight'>
<div class='flexbox'>
<div class='first'>
<p>foo</p>
</div>
<div class='second'>
<p>bar</p>
<img src='http://www.mandysam.com/img/random.jpg'>
</div>
</div>
</div>
和css
:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.fullHeight {
height: 100vh;
background-color: red;
}
.flexbox {
display: flex;
flex-direction: column;
height: 100%;
maxHeight: 100%;
width: 100%;
background-color: green;
}
.first {
background-color: magenta;
}
.second {
background-color: yellow;
flex: 1 1 auto;
max-height: 100%;
height: 100%;
widht: auto;
}
只要没有图像,一切正常:
但是只要一张图片进来,它就会溢出容器,而不是被缩小以适应可用的高度:
解决方案
你错过了
display: flex;
flex-direction: column;
在您的.second
班级中,这就是该flex
物业没有做任何事情的原因。它也应该是max-height
代替maxHeight
和width
代替widht
。
您可以将background-image
其用于您的目的。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.fullHeight {
height: 100vh;
background-color: red;
}
.flexbox {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
background-color: green;
}
.first {
background-color: magenta;
}
.second {
background-color: yellow;
display: flex;
flex-grow: 1;
flex-direction: column;
}
.container {
background-color: green;
flex-grow: 1;
background-image: url('http://www.mandysam.com/img/random.jpg');
background-size: contain;
background-repeat: no-repeat;
}
<div class='fullHeight'>
<div class='flexbox'>
<div class='first'>
<p>foo</p>
</div>
<div class='second'>
<p>bar</p>
<div class="container">
</div>
</div>
</div>
</div>
推荐阅读
- android - 导航组件共享元素转换适用于输入,但不适用于弹回
- r - 如何以快速(并行)方式正确地将函数应用于每一行
- react-native - 无法运行-android,因为无法解析 POM https://dl.google.com/dl/android/maven2/android/arch/lifecycle/runtime/1.1.1/runtime-1.1.1.pom
- mysql - 我该怎么做这个 SQL 查询?
- java - 如何将 StickyHeaders 添加到 recyclerView?
- ios - SwiftyJSON 响应中的 for 循环
- html - 有没有办法让我的复选框使用引导程序与固定数字水平对齐?
- python - 如何在 python 中处理“HTTP 错误 502”?
- django - 我不能将两个字段相乘。错误:int() 以 10 为底的无效文字:
- jdbc - 如何使用带有条件的jdbc在kafka中读/写