【发布时间】:2016-05-22 12:34:35
【问题描述】:
我网站的各个部分是垂直堆叠的,每个部分的内容都以flexbox垂直和水平居中。
每个部分至少应填满整个屏幕,但如果内容不合适,则垂直增长。以下适用于除 IE11 之外的所有主要浏览器(如果您愿意,可以使用 fiddle)。
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
/* comment the following out to see the desired outcome */
/* display: flex;
flex-direction: column; */
}
section {
/* relevant stuff */
display: flex;
justify-content: center;
align-items: center;
min-height: 100%;
/* unimportant aesthetic stuff */
border: 1px solid white;
background: #442;
color: white;
font-family: Calibri;
font-size: 35px;
padding: 0px 100px;
}
/* more unimportant aesthetic stuff */
section:nth-of-type(2n) {
background: #553;
}
<section>
I'm a section. Scroll down.
</section>
<section>
I'm another section. Keep scrolling.
</section>
<section>
The next section will expand to accommodate its contents.
</section>
<section>
<!-- a bunch of dots -->
.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.
<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.
<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.
<br><br>.<br><br>
</section>
<section>
If only it was this easy.
</section>
由于 IE11 的 min-height 错误,我使用了众所周知的解决方法,即将我的 flex 容器包装在另一个 flex 容器中。但是现在我的部分不再扩展以容纳它们的内容(如果您愿意,可以使用 fiddle)。
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
/* comment the following out to see the desired outcome */
display: flex;
flex-direction: column;
}
section {
/* relevant stuff */
display: flex;
justify-content: center;
align-items: center;
min-height: 100%;
/* unimportant aesthetic stuff */
border: 1px solid white;
background: #442;
color: white;
font-family: Calibri;
font-size: 35px;
padding: 0px 100px;
}
/* more unimportant aesthetic stuff */
section:nth-of-type(2n) {
background: #553;
}
body {
background: black;
}
<section>
I'm a section. Scroll down.
</section>
<section>
I'm another section. Keep scrolling.
</section>
<section>
How did these dots get on me?
</section>
<section>
<!-- a bunch of dots -->
.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.
<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.
<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.<br><br>.
<br><br>.<br><br>
</section>
<section>
The sections aren't expanding!
</section>
什么是适用于 IE11 的干净解决方案?它应该给我与第一个代码 sn-p 相同的结果,但在 IE11 中。
【问题讨论】:
-
查看jsfiddle.net/6p68f8yz/1,这是您要找的吗?
-
您应该添加一个显示预期(和实际)行为的图形。为什么是实际的?因为您看到的可能是由将来无法重现的错误引起的,所以您的问题将毫无用处。