【发布时间】:2019-12-21 00:34:09
【问题描述】:
我需要使图像在 flex 容器中采用最大尺寸(采用最大宽度或最大高度)。
由于父容器没有固定的宽度和高度,我不能使用max-width 和max-height 和flex:1 也不起作用。
这是一个问题的例子:https://jsfiddle.net/vb26u0e5/2/
我希望图像自动占用所有可用的绿色空间(删除 width: 40px; 第 20 行)。
#mainContainer {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
color: white;
}
#imageContainer {
flex: 1;
display: flex;
justify-content: space-between;
background-color: green;
}
#image {
width: 40px;
}
#previous,
#next {
display: flex;
align-items: center;
justify-content: center;
font-size: 25px;
padding: 0 10px;
background-color: purple;
}
#title,
#footer {
text-align: center;
}
#title {
background-color: blue;
}
#footer {
background-color: red;
}
<div id="mainContainer">
<div id="title">TITLE</div>
<div id="imageContainer">
<div id="previous"><</div>
<img id="image" src="https://via.placeholder.com/1920x1080" />
<div id="next">></div>
</div>
<div id="footer">FOOTER</div>
</div>
【问题讨论】: