html - 带有无包装子元素的 Flex 子元素溢出屏幕
问题描述
我有一个非常具体的 flex 用例,我不太明白它为什么会这样。我有两列。右列具有固定宽度,并且flex-shrink: 0
. 左列有flex-shrink: 1
, flex-grow: 1
, 和,关键的是,一个孩子有width: 100%
, white-space: nowrap
, 和overflow-x: scroll
。
我期望的是左列填充剩余的宽度,然后溢出滚动。然而,实际发生的是左列将右列推离屏幕。
你可以在这里看到一个例子:https ://codepen.io/benlorantfy/pen/oNXKXOe
我正在寻找解释它为什么会这样以及我可以做些什么来解决它。
.flex-parent {
display: flex;
flex-direction: row;
}
.flex-child-1 {
background-color: red;
height: 200px;
flex-grow: 1;
flex-shrink: 1;
}
.nowrap-el {
white-space: nowrap;
width: 100%;
overflow-x: scroll;
background-color: grey;
}
.flex-child-2 {
background-color: lightblue;
height: 200px;
width: 400px;
flex-shrink: 0;
}
<div class="flex-parent">
<div class="flex-child-1">
<div class="nowrap-el">
This should fill remaining space instead of pushing flex-child-2 off the screen. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas in augue volutpat, sagittis ante et, tincidunt mauris. Morbi vitae convallis erat, eget vulputate lacus.
Nunc a justo aliquet, facilisis justo eget, semper velit. Nunc quis elementum tortor. Nam vestibulum aliquet nunc, id
</div>
</div>
<div class="flex-child-2">This should not be shoved off screen</div>
</div>
解决方案
我通过添加overflow-x: hidden
来解决这个问题flex-child-1
。(我也添加flex-basis: 0
了很好的措施,但我认为它没有任何作用)。这似乎告诉浏览器尊重孩子的宽度和溢出设置。我不确定为什么会这样,或者规范中的哪个地方这么说,但如果有人知道并想发表评论,请随意。
.flex-child-1 {
background-color: red;
height: 200px;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
overflow-x: hidden;
}
推荐阅读
- google-analytics - Google Analytics 中所有可能的国家/地区值列表
- c# - 使用 LINQ 查询语法 EF Core C# 的左外连接
- javascript - 带有 || 的 if 语句 什么都不做
- regex - 在 burp 中反转入侵者 grep 匹配
- sql - 如何用一个(非复合)候选键和所有非主属性构造 SQL 表?
- android - Android Room SUM 查询返回 null
- java - 如何在条件下启用 JButton?
- oauth - Asp.Net Core Twitch OAuth:关联失败
- selenium - 使用 RobotFramework 自动化测试 Webview2 应用程序
- javascript - 如何以 post 方法将数据从 Javascript 发送到 nodejs 服务器?