【发布时间】:2019-11-16 19:17:42
【问题描述】:
编辑:添加了 flex-direction: 列,在初始代码中错过了它。
当子级具有overflow:auto 而父级具有overflow:auto 时,滚动条会出现在子级上。
但是当overflow:auto从parent中移除时,滚动条出现在grand-parent上。
html,
body {
height: 100%;
width: 100%;
overflow: hidden;
font-family: "Roboto", sans-serif;
}
body {
margin: 0;
padding: 0;
}
.App {
display: flex;
height: 100%;
}
.grand-parent {
display: flex;
flex-direction: column;
background: red;
overflow: auto;
padding: 20px;
}
.parent {
display: flex;
overflow: auto;
padding: 20px;
background: green;
}
.child {
overflow: auto;
font-size: 156px;
}
<div class="App">
<div class="grand-parent">
<div class="parent">
<div class="child">
Some content which grows bigger
</div>
</div>
</div>
</div>
这是为什么呢?我仍然希望滚动条出现在孩子身上。 浏览器布局算法在这里是如何工作的?
编辑:
奇怪的是,这种行为似乎取决于grand-parent 拥有flex-direction: column。当flex-direction: row
在 Chrome 75、Firefox 67 上测试
这似乎与祖父上的flex-direction有关,如果flex-direction为row,则水平滚动显示此行为,如果flex-direction为column,则垂直滚动显示此行为
编辑: 在进一步的实验中,如果我们在 parent 上设置 min-height: 0,它的行为与预期一样,所以这个问题可能类似于
https://moduscreate.com/blog/how-to-fix-overflow-issues-in-css-flex-layouts/
【问题讨论】:
-
你指的是垂直滚动条还是水平滚动条?
-
垂直滚动条
-
什么浏览器?我在 chrome 上看不到这个
-
@TemaniAfif 我错过了一个属性,编辑了问题,请立即查看
-
@MaximeLaunois 我现在已将它添加到祖父母中,我可能会为这两种不同的行为附上图片