在试图使一个有用的模式使用flexbox,我发现什么似乎是浏览器的问题,我想知道是否有一个已知的修复或解决方案-或关于如何解决它的想法。

我要解决的问题有两个方面。首先,让模态窗口垂直居中,这是预期的工作。第二种方法是让模式窗口在外部滚动,所以整个模式窗口都在滚动,而不是其中的内容(这是为了让下拉菜单和其他UI元素可以扩展到模式的边界之外——比如自定义日期选择器等)。

然而,当将垂直居中与滚动条相结合时,模态的顶部可能会变得难以访问,因为它开始溢出。在上面的例子中,你可以调整大小来强制溢出,这样做可以让你滚动到模态的底部,但不能滚动到顶部(第一段被切断)。

.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 } <div class="modal-container"> <div class="modal-window"> <div class="modal-content"> <p class="p3">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.</p> <p class="p3">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.</p> <p class="p3">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.</p> </div> </div> </div>

这将影响(当前)Firefox, Safari, Chrome和Opera。有趣的是,如果你在IE10厂商的CSS前缀中注释,它在IE10中的行为是正确的——我还没有在IE11中测试,但假设行为与IE10相匹配。

下面是示例代码的链接(高度简化)

https://jsfiddle.net/dh9k18k0/2/


当前回答

根据MDN,现在可以为align-items和justify-content等属性提供安全值。具体描述如下:

如果项的大小溢出对齐容器,则将项改为对齐,就像启动对齐模式一样。

所以,它可以这样使用。

.rule
{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: safe center;
}

然而,目前还不清楚它有多少浏览器支持,我找不到任何使用它的例子,而且我自己也遇到过一些问题。在这里提到它是为了引起更多的关注。

其他回答

我还使用了额外的容器

HTML

<div class="modal-container">
  <div class="modal">
    <div class="content-container">
       <div class="content">
         <p>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.</p>
        </div>
      </div>
  </div>  
</div>

CSS

.modal-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: black;
}

.modal {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #aaa;
  height: 80%;
  width: 90%;
}

.content-container {
  background-color: blue;
  max-height: 100%;
  overflow: auto;
  padding:0;
}

.content {
  display: flex;
  background-color: red;
  padding: 5px;
  width: 900px;
  height: 300px;
}

在j藐视> https://j藐视dle.net/nas171/cpf4weq5/

更改.content的宽度/高度值并查看

我只用了3个容器就搞定了。诀窍是将flexbox容器与控制滚动的容器分开。最后,将所有内容放到根容器中,使其居中。以下是创造这种效果的基本风格:

CSS:

.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;
}

HTML:

<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/

不要使用justify-content: center,而是添加两个div,其中flex: 1作为flex容器的第一个和最后一个子容器。

html, body { background-color: blue; height: 100%; overflow: hidden; } .container { background-color: red; height: 100%; display: flex; flex-direction: column; overflow: hidden auto; } .flex-1 { flex: 1; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Static Template</title> <link rel="stylesheet" href="./index.css" /> </head> <body> <div class="container"> <div class="flex-1"></div> <h1> This is a static template, there is no bundler or bundling involved! </h1> <p> This is a static template, there is no bundler or bundling involved! This is a static template, there is no bundler or bundling involved! </p> <div class="flex-1"></div> </div> </body> </html>

根据MDN,现在可以为align-items和justify-content等属性提供安全值。具体描述如下:

如果项的大小溢出对齐容器,则将项改为对齐,就像启动对齐模式一样。

所以,它可以这样使用。

.rule
{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: safe center;
}

然而,目前还不清楚它有多少浏览器支持,我找不到任何使用它的例子,而且我自己也遇到过一些问题。在这里提到它是为了引起更多的关注。

试试这个!

<div class="flex-container">
    <div class="item">First item</div>
    <div class="item">Second item</div>
    <div class="item">Third item</div>
</div>
.flex-container {
    display: flex;
    flex-wrap: nowrap;
    white-space: nowrap;
    overflow-x: auto;

    .item:first-child {
        margin-left: auto;
    }

    .item:last-child {
        margin-right: auto;
    }
}