在试图使一个有用的模式使用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/


当前回答

我想我找到解决办法了。它适用于大量文本和少量文本。你不需要指定任何东西的宽度,它应该在IE8中工作。

.wrap1 { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background: rgba(0, 0, 0, 0.5); overflow-y: auto; } .wrap2 { display: table; width: 100%; height: 100%; text-align: center; } .wrap3 { vertical-align: middle; display: table-cell; } .wrap4 { margin: 10px; } .dialog { text-align: left; background-color: white; padding: 5px; border-radius: 3px; margin: auto; display: inline-block; box-shadow: 2px 2px 4px rgba(0, 0, 0, .5); } <div class="wrap1"> <div class="wrap2"> <div class="wrap3"> <div class="wrap4"> <div class="dialog"> <p>Lorem ipsum dolor sit amet.</p> </div> </div> </div> </div> </div>

其他回答

您可能应该使用margin: auto技术,但如果出于某种原因不想使用flexbox,则可以使用带有vertical align hack的pseudo元素来实现这一点。

例子

如果codepen不起作用

<div class="wrapper">
  <div class="modal"></div>
</div>

<style>
.wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #000;
  overflow-y: auto;
  text-align: center;
}

/* Required for centering */
.wrapper:before {
  content: '';
  height: 100%;
  width: 0;
  vertical-align: middle;
  display: inline-block;
}

.modal {
  /* Required for centering */
  display: inline-block;
  vertical-align: middle;


  text-align: left;
  width: 320px;
  height: 320px;
  background-color: #fff;
  border-radius: 25px;
}
</style>

它的工作原理是创建一个内联元素,作为父元素的全高,并显示为内联块,就像你的目标元素(.modal)一样,然后在两个元素上都使用vertical-align: middle,浏览器会发挥它的魔力——它会将.modal和pseudo元素对齐,就像它们是常规文本一样。你也可以在模态div上使用上下垂直对齐。结合文本对齐,模态可以放置在任何位置。

你可以在父控件上使用text-align: center将对话框水平居中。应该支持在任何浏览器和溢出也工作。

在撰写本文时(2021年9月15日),以隐身模式转到谷歌会显示一个cookie策略模式,该模式使用这种技术来居中。

根据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;
    }
}

2容器Flex方法与表回退测试IE8-9, Flex工作在IE10,11。 编辑:编辑,以确保垂直中心时,最小的内容,增加了传统的支持。

正如Michael回答的那样,这个问题源于高度继承自视口大小,这会导致儿童溢出。https://stackoverflow.com/a/33455342/3500688

something more simple and use flex to maintain the layout within the popup container(content): #popup { position: fixed; top: 0; left: 0; right: 0; min-height: 100vh; background-color: rgba(0,0,0,.25); margin: auto; overflow: auto; height: 100%; bottom: 0; display: flex; align-items: flex-start; box-sizing:border-box; padding:2em 20px; } .container { background-color: #fff; margin: auto; border: 1px solid #ccc; border-radius: 4px; background: #fff; /* width: 100%; */ max-width: 500px; padding: 10px; /* display: flex; */ /* flex-wrap: wrap; */ } <!--[if lt IE 10]> <style> #popup { display: table; width:100%; } .iewrapper { display: table-cell; vertical-align: middle; } </style> <![endif]--> <div id="popup"> <!--[if lt IE 10]> <div class="iewrapper"> <![endif]--> <div class="container"> <p class="p3">Test</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> <!--[if lt IE 10]> <div class="iewrapper"> <![endif]--> </div>

好吧,正如墨菲定律所言,我在提出这个问题后所做的阅读产生了一些结果——虽然没有完全解决,但还是有些有用的。

在发布之前,我对最小高度进行了一些研究,但没有意识到内在的大小限制,这对规范来说是相当新的。

http://caniuse.com/#feat=intrinsic-width

向flexbox区域添加min-height: min-content确实解决了Chrome中的问题,使用供应商前缀也可以修复Opera和Safari,但Firefox仍然没有解决。

min-height: -moz-min-content; // not implemented
min-height: -webkit-min-content // works for opera and safari
min-height: min-content // works for chrome

还在寻找关于Firefox的想法,以及其他潜在的解决方案。