gpt4 book ai didi

html - 如何控制 flex 元素的子div高度

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

描述性插件:http://plnkr.co/edit/remH1RtkY1qBk0A7J4Tp?p=preview

我有一个 flexbox 容器,其中包含两个 flex 元素(“header”和“content”)。内容 div 有一个我想控制其高度的子 div。但是无论我如何设置这个 div 的高度,高度都保持默认。

这是标记/CSS:

<!DOCTYPE html>
<html>
<head>
<style>
div { padding:15px; }

.container
{
background-color:gold;
height:500px;
display:flex;
flex-flow:column nowrap;
}
.header
{
background-color:pink;
/* no flex shrink/grow */
}
.content
{
background-color:aquamarine;
flex:1 1;/* allow filling space */
}
.myContentItem
{
background-color:khaki;
display:block;
height:50%;/* <-----------this is ignored */
}
</style>
</head>

<body>

<div class="container">
<div class="header">header<br />yo</div>
<div class="content">
<div class="myContentItem">I want this to be 50% the height of content div</div>
</div>
</div>

</body>

</html>

编辑:我知道我可以使用 display:absolute 让它在这个人为的例子中工作。我更好奇为什么会发生这种情况,以及如何以适当的方式处理它而不是寻找解决方法。

最佳答案

这似乎是一个错误或尚未实现的功能。 flex 元素没有确定的大小(在本例中为高度),因此您无法确定 child 的百分比大小。

the docs状态:

If a percentage is going to be resolved against a flex item’s main size, and the flex item has a definite flex basis, the main size must be treated as definite for the purpose of resolving the percentage, and the percentage must resolve against the flexed main size of the flex item...

我的理解是,“正确的方法”是定义一个明确的 flex 基础。因此,在您的示例中 flex:1 1; 应该可以解决问题...但是,这是最近的更改/错误修复,因此我希望几乎没有浏览器支持(看起来它已添加到march 2014 中的规范)。

我的建议是使用 absolute position workaround .

另请参阅:https://code.google.com/p/chromium/issues/detail?id=428049https://code.google.com/p/chromium/issues/detail?id=346275 .

关于html - 如何控制 flex 元素的子div高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29049715/

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