gpt4 book ai didi

html - 如何在方向为 : column 的 flex 容器中垂直放置图像

转载 作者:行者123 更新时间:2023-11-27 23:28:50 28 4
gpt4 key购买 nike

我正在尝试将图像垂直放置在具有特定高度的 flex 容器中。

flex-directioncolumn,图像包含在flex-item中,flex-basis: 100 %.

另外,图片的max-height100%

如您在示例中所见,图像不适合红色容器。

#container {
background-color: red;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 200px;
width: 320px;
justify-content: space-between;
}

#container > * {
padding: 5px;
}

#img {
flex: 0 1 100%;
/* height: 100%; */
}
img {
max-height: 100%;
max-width: 100%;
}
<div id="container">
<div id="img">
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=img&w=340&h=500">
</div>
<div>
Something else
</div>
</div>

根据规范,图像不应该收缩以垂直放入 flex 容器吗?

我找到的解决方法是将 #imgheight 设置为 100%,但我感觉这不是办法应该这样做。

作为附加说明,如果我将 flex-direction: row 设置为容器,它会水平适合图像(这是我期望的行为)。

最佳答案

你写道:

The workaround I found is to set the height of #img to 100%, but I have the sensation that it's not the way it should be done.

实际上,它它应该做的方式。规范的主要实现要求在子级上使用百分比高度时将 height 属性应用于父级。 (虽然这正在慢慢发展。请参阅下面我的第二个引用资料。)

引用资料:

关于html - 如何在方向为 : column 的 flex 容器中垂直放置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37004526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com