CSS flex a content-fit 带有未知高度的标题兄弟的图像
CSS flex a content-fit image with a title sibling of unknown height
我正在尝试调整 flex-column
中的 img
和 .title
元素。调整页面大小时,图像应 grow/shrink 同时保持其纵横比,标题文本应在屏幕底部保持相同的高度。但是,当我降低屏幕高度时,标题文本会被推离屏幕。
此外,.title
元素的高度可能并不总是单行,在渲染之前无法得知。
代码:https://jsfiddle.net/qk4wbfpe/
body {
height: 100vh;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.container > img {
flex-grow: 1;
flex-shrink: 1;
background-color: blue;
width: 100%;
height: 100%;
object-fit: contain;
}
.container .title {
padding: 10px;
color: white;
text-align: center;
background-color: gray;
}
<div class="container">
<img src="https://cdn.mos.cms.futurecdn.net/paWPwF85Vkcs8YUuyvA3YM-650-80.jpg.webp">
<div class="title">
Planet Earth
</div>
</div>
如果您将 min-height:0
添加到 .container>img
,它将给出所需的结果。
body {
height: 100vh;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.container>img {
flex-grow: 1;
flex-shrink: 1;
background-color: blue;
width: 100%;
min-height:0;
object-fit: contain;
}
.container .title {
padding: 10px;
color: white;
text-align: center;
background-color: gray;
}
<div class="container">
<img src="https://cdn.mos.cms.futurecdn.net/paWPwF85Vkcs8YUuyvA3YM-650-80.jpg.webp">
<div class="title">
Planet Earth
</div>
</div>
为您的容器添加包装,使其顺畅流动
.container {
display: flex;
flex-wrap: wrap;
flex-direction: column;
width: 100%;
height: 100%;
并在您的 child (img)
上添加一个 flex-basis
.container > img {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 25%;
background-color: blue;
object-fit: contain;
或者您可以删除 flex-grow,将其缩小并 shorthand 为
flex: 1 1 25%;
这是你需要的吗?
我正在尝试调整 flex-column
中的 img
和 .title
元素。调整页面大小时,图像应 grow/shrink 同时保持其纵横比,标题文本应在屏幕底部保持相同的高度。但是,当我降低屏幕高度时,标题文本会被推离屏幕。
此外,.title
元素的高度可能并不总是单行,在渲染之前无法得知。
代码:https://jsfiddle.net/qk4wbfpe/
body {
height: 100vh;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.container > img {
flex-grow: 1;
flex-shrink: 1;
background-color: blue;
width: 100%;
height: 100%;
object-fit: contain;
}
.container .title {
padding: 10px;
color: white;
text-align: center;
background-color: gray;
}
<div class="container">
<img src="https://cdn.mos.cms.futurecdn.net/paWPwF85Vkcs8YUuyvA3YM-650-80.jpg.webp">
<div class="title">
Planet Earth
</div>
</div>
如果您将 min-height:0
添加到 .container>img
,它将给出所需的结果。
body {
height: 100vh;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.container>img {
flex-grow: 1;
flex-shrink: 1;
background-color: blue;
width: 100%;
min-height:0;
object-fit: contain;
}
.container .title {
padding: 10px;
color: white;
text-align: center;
background-color: gray;
}
<div class="container">
<img src="https://cdn.mos.cms.futurecdn.net/paWPwF85Vkcs8YUuyvA3YM-650-80.jpg.webp">
<div class="title">
Planet Earth
</div>
</div>
为您的容器添加包装,使其顺畅流动
.container {
display: flex;
flex-wrap: wrap;
flex-direction: column;
width: 100%;
height: 100%;
并在您的 child (img)
上添加一个 flex-basis.container > img {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 25%;
background-color: blue;
object-fit: contain;
或者您可以删除 flex-grow,将其缩小并 shorthand 为
flex: 1 1 25%;
这是你需要的吗?