我这里有这段代码,它在Safari中的行为不同于在Chrome和Firefox中的行为,为什么?
.parent {
height: 150px;
display: flex;
flex-direction: column;
background: salmon;
border: 2px solid black;
}
.child1 {
background: red;
}
.child2 {
background: grey;
height: 100%;
}
.grandchild2 {
height: 100%;
background: blue;
}
<div class="parent">
<div class="child1">
child 1
</div>
<div class="child2">
<div class="grandchild2">
</div>
</div>
</div>
我不明白为什么grandchild2
的存在会使child2
具有与parent
相同的高度。这是已知的Safari错误吗?
如果我移除grandchild2
,则child2
不会溢出。
是否存在一种使Safari像Chrome和Firefox一样运作的解决方法?
答案 0 :(得分:0)
尝试从child2移除高度,然后改用flex-grow:1
答案 1 :(得分:0)
使用child2
的flex容器似乎可以带来我期望的行为。
.parent {
height: 150px;
display: flex;
flex-direction: column;
background: salmon;
border: 2px solid black;
}
.child1 {
background: red;
}
.child2 {
background: grey;
flex-grow: 1; /* <---- added that */
display: flex; /* <---- added that */
flex-direction: column; /* <---- added that */
}
.grandchild2 {
flex-grow: 1; /* <---- added that */
background: blue;
}
<div class="parent">
<div class="child1">
child 1
</div>
<div class="child2">
<div class="grandchild2">
</div>
</div>
</div>
阅读此线程后:Chrome / Safari not filling 100% height of flex parent
似乎我必须一直使用flex,而不能混用height: 100%
和flex-grow:1
。