我在这里遇到的问题是,当使用Flexbox" flex-wrap: wrap
"时,它似乎会在div
下方产生额外的空白区域。
我的设置是这样的(类名来澄清div的用法):
<div class="viewheight-background">
<div class="header"></div>
<div class="vertically-centered-text"></div>
</div>
我的css很直接:
html, body{
height: 100%;
}
.viewheight-background {
min-height: 100%; /* To ensure a section of 100% screen height, could be done with 'vh' */
background-size: cover;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.vertically-centered-text {
align-self: center;
}
垂直居中的文本在div中,因为我正在处理主标题和一些子文本。
如果我带走&#34; align-self: center
&#34 ;, flex-items包装就好了除了一件事。似乎有某种虚幻的白色空间,我无法识别。这会导致&#34; align-self: center
&#34;低于它应该居中的div
的中心。
有什么想法吗?
(如果我太模糊,请告诉我,我会澄清)
编辑:
Fiddle以供参考。忘记添加html, body {height: 100%;}
答案 0 :(得分:0)
终于找到了一个不错的解决方案。
看来:
float,clear和vertical-align对弹性项目没有影响。
所以我发现问题出在你的身上:
align-self: center;
在你的第二个弹性项目上。
所以你的css应该编辑为:
.vertically-centered-text {
/*align-self: center;*/
background-color:blue;
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
text-align: center;
}
如此LIVE DEMO所示或运行此代码段(在整页视图中):
html,
body {
height: 100%;
}
.viewheight-background {
min-height: 100%;
/* To ensure a section of 100% screen height, could be done with 'vh' */
background-size: cover;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.vertically-centered-text {
/*align-self: center;*/
background-color: blue;
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
text-align: center;
}
.header {
background-color: red;
}
&#13;
<div class="viewheight-background">
<div class="header">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>
<div class="vertically-centered-text">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
</div>
&#13;