gpt4 book ai didi

html - Flex 元素在 Chrome 和 IE11 中重叠

转载 作者:太空狗 更新时间:2023-10-29 15:36:48 25 4
gpt4 key购买 nike

我正在尝试创建一个固定高度的 flexbox 布局,当内部内容太大时滚动内部内容。

此外,如果内容不会导致滚动,我想在容器底部修复一个带有按钮的 div。

我有一个在 Firefox 中完美运行的布局,但在 Chrome 中,当底部按钮 div 大到足以导致滚动时,它会导致内容顶部剪辑。

这是在 Firefox 中正确呈现的布局:

Firefox proper rendering

这是它在 Chrome 中不正确呈现的方式:

Chrome improper rendering

此外,如果没有足够的内部内容导致滚动,它应该是这样的:

Small content rendering

.container {
height: 150px;
width: 300px;
display: flex;
flex-direction: column;
overflow-y: auto;
border: 1px solid #000;
padding: 4px;
}

.options {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}

.spacer {
flex: 1 1 auto;
}

.buttons {
display: flex;
flex-direction: row;
justify-content: space-between;
}
<div class="container">

<div class="options">
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
</div>

<div class="spacer"></div>

<div class="buttons">
<input type="button" value="Cancel">
<input type="button" value="Save">
</div>

</div>

https://jsfiddle.net/tdghqyuu/1/

最佳答案

解决方案

将此添加到您的代码中:

.container > * {
flex-shrink: 0;
}

解释

flex 元素的初始设置,根据 flexbox specification , 应该是 flex-shrink: 1 .这意味着允许 flex 元素收缩以避免溢出容器。

Chrome 和 IE11 似乎遵守了这条准则。

其他浏览器,例如 Firefox、Edge 和 Safari,似乎默认将 flex-shrink 设置为 0

另一方面,这可能与 flex-shrink 关系不大或没有关系。该问题可能与 flex 元素的默认 min-heightmin-width 设置有关。 (参见此处:Why don't flex items shrink past content size?)

或者两者兼而有之。这真的取决于浏览器。

事实上,浏览器的实现各不相同。这就是您必须进行测试的原因。

规范的初始设置并不完全可靠,因为

  1. 浏览器代表可以为所欲为的独立实体,并且
  2. “干预”的使用。

Interventions

An intervention is when a user agent decides to deviate slightly from a standardized behavior in order to provide a greatly enhanced user experience.

换句话说,无论规范指南如何,浏览器都会自行提供它认为最适合用户的设置。

Chrome 似乎经常这样做。以下是一些示例:

有一件事似乎是确定的:如果您禁用 flex-shrink,您的布局将适用于所有浏览器。将此添加到您的代码中:

.container > * {
flex-shrink: 0;
}

.container>* {
flex-shrink: 0;
}

.container {
height: 150px;
width: 300px;
display: flex;
flex-direction: column;
overflow-y: auto;
border: 1px solid #000;
padding: 4px;
}

.options {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}

.buttons {
display: flex;
flex-direction: row;
justify-content: space-between;
margin-top: 10px;
/* new; for demo only */
}
<div class="container">

<div class="options">
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
</div>

<div class="buttons">
<input type="button" value="Cancel">
<input type="button" value="Save">
</div>

</div>

您可能还想考虑将滚动条仅移动到选项框,这样您就真正将按钮固定到了屏幕底部。对您的代码进行此调整:

.container {
height: 150px;
width: 300px;
display: flex;
flex-direction: column;
/* overflow-y: auto; */
border: 1px solid #000;
padding: 4px;
}

.options {
display: flex;
flex-direction: row;
flex-wrap: wrap;
overflow-y: auto; /* NEW */
flex: 1; /* NEW; may be necessary for overflow to work in some browsers */

}

.container > * {
flex-shrink: 0;
}

.container {
height: 150px;
width: 300px;
display: flex;
flex-direction: column;
/* overflow-y: auto; */
border: 1px solid #000;
padding: 4px;
}

.options {
display: flex;
flex-direction: row;
flex-wrap: wrap;
overflow-y: auto; /* NEW */
flex: 1; /* NEW; may be necessary for overflow to work in some browsers */
}

.buttons {
display: flex;
flex-direction: row;
justify-content: space-between;
margin-top: 10px; /* new; for demo only */
}
<div class="container">

<div class="options">
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
<div class="option">
<input type="checkbox"> Option
</div>
</div>

<div class="buttons">
<input type="button" value="Cancel">
<input type="button" value="Save">
</div>

</div>

此外,我删除了您的间隔元素。当有许多干净的方法可以在 flex 容器中的元素之间创建空间时,这似乎没有必要。

关于html - Flex 元素在 Chrome 和 IE11 中重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48294014/

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