无法滚动到溢出容器的flex项目的顶部的标题重写为:无法将溢出容器中的flex项目滚动至顶部
P粉854119263
P粉854119263 2023-08-22 14:58:06
[CSS3讨论组]
<p>在尝试使用flexbox创建一个有用的模态框时,我发现了一个似乎是浏览器问题的情况,并想知道是否有已知的修复方法或解决方法 - 或者对如何解决它有什么想法。</p> <p>我试图解决的问题有两个方面。首先,垂直居中模态窗口,这个功能按预期工作。第二个是让模态窗口滚动 - 外部滚动,所以整个模态窗口滚动,而不是其中的内容(这样你可以有下拉菜单和其他可以超出模态框范围的UI元素 - 如自定义日期选择器等)。</p> <p>然而,当将垂直居中与滚动条结合使用时,模态框的顶部可能变得无法访问,因为它开始溢出。在上面的示例中,您可以调整大小以强制溢出,在这样做时,它允许您滚动到模态框的底部,但无法滚动到顶部(第一段被截断)。</p> <p><br /></p> <pre class="brush:css;toolbar:false;">.modal-container { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background: rgba(0, 0, 0, 0.5); overflow-x: auto; } .modal-container .modal-window { display: -ms-flexbox; display: flex; flex-direction: column; align-items: center; justify-content: center; /* Optional support to confirm scroll behavior makes sense in IE10 //-ms-flex-direction: column; //-ms-flex-align: center; //-ms-flex-pack: center; */ height: 100%; } .modal-container .modal-window .modal-content { border: 1px solid #ccc; border-radius: 4px; background: #fff; width: 100%; max-width: 500px; padding: 10px }</pre> <pre class="brush:html;toolbar:false;">&lt;div class="modal-container"&gt; &lt;div class="modal-window"&gt; &lt;div class="modal-content"&gt; &lt;p class="p3"&gt;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.&lt;/p&gt; &lt;p class="p3"&gt;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.&lt;/p&gt; &lt;p class="p3"&gt;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre> <p><br /></p> <p>这影响(当前的)Firefox,Safari,Chrome和Opera。有趣的是,在IE10中,如果您取消注释IE10供应商前缀的CSS,它的行为确实是正确的 - 我还没有在IE11中进行测试,但假设行为与IE10相匹配。</p> <p>这是示例代码的链接(高度简化)</p> <p>https://jsfiddle.net/dh9k18k0/2/</p>
P粉854119263
P粉854119263

全部回复(2)
P粉846294303

我只用了3个容器就实现了这个效果。诀窍是将flexbox容器与控制滚动的容器分开。最后,将所有内容放入一个根容器以居中显示。以下是创建效果所需的关键样式:

.root {
  display: flex;
  justify-content: center;
  align-items: center;
}

.scroll-container {
  margin: auto;
  max-height: 100%;
  overflow: auto;
}

.flex-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<div class="root">
  <div class="scroll-container">
    <div class="flex-container">
      <p>Lorem ipsum dolor sit amet.</p>
    </div>
  </div>
</div>

我在这里创建了一个演示:https://jsfiddle.net/r5jxtgba/14/

P粉670838735

问题

Flexbox使居中变得非常容易。

只需将align-items: centerjustify-content: center应用于flex容器,您的flex项目将垂直和水平居中。

然而,当flex项目大于flex容器时,这种方法存在一个问题。

如问题中所指出的,当flex项目溢出容器时,顶部变得无法访问。

对于水平溢出,左侧部分变得无法访问(或者在RTL语言中为右侧部分)。

下面是一个LTR容器的示例,其中包含justify-content: center和三个flex项目:

有关此行为的说明,请参见本答案底部。


解决方案#1

为了解决这个问题,请使用flexbox自动边距,而不是justify-content

使用auto边距,溢出的flex项目可以在垂直和水平方向上居中,而不会失去对其任何部分的访问。

所以,不要在flex容器上使用这段代码:

#flex-container {
    align-items: center;
    justify-content: center;
}

在flex项目上使用这段代码:

.flex-item {
    margin: auto;
}

修订演示


解决方案#2(大多数浏览器尚未实现)

safe值添加到您的关键字对齐规则中,如下所示:

justify-content: safe center

或者

align-self: safe center

来自CSS Box Alignment模块规范

注意:Box Alignment模块适用于多个盒子布局模型,而不仅仅是flex。因此,在上述规范摘录中,方括号中的术语实际上是“对齐主体”、“对齐容器”和“start”。我使用了特定于flex的术语,以保持对这个特定问题的关注。


来自MDN的滚动限制说明:

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号