简体   繁体   English

垂直对齐Flexbox项,而不会导致项之间的幻影空白

[英]Vertically aligning Flexbox items, without causing Phantom Whitespace between items

The problem I'm facing here is that when using Flexbox's " flex-wrap: wrap ", it seems to cause extra white space beneath the div . 我在这里面临的问题是,在使用Flexbox的“ flex-wrap: wrap ”时,似乎会在div下方引起额外的空白。

My setup is like this (class names to clarify the use of the div): 我的设置是这样的(类名以阐明div的用法):

<div class="viewheight-background">

    <div class="header"></div>
    <div class="vertically-centered-text"></div>

</div>

My css is pretty straight forward: 我的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;
}

The vertically-centered-text is inside a div because I am dealing with a main heading and some subtext. 垂直居中的文本位于div内,因为我正在处理主标题和一些子文本。

If I take away " align-self: center ", the flex-items wrap just fine except for one thing. 如果我删除“ align-self: center ”,则flex-item除了一件事外就可以正常包装。 There seems to be some kind of phantom white-space which I can't identify. 似乎有些我无法识别的幻像空白。 This causes " align-self: center " to be lower than the center of the div it should be centered in. 这将导致“ align-self: center ”低于它应居中的div中心。

Any ideas? 有任何想法吗?

(If I'm being too vague, please let me know and i'll clarify) (如果我太含糊,请让我知道,我会澄清)

EDIT: 编辑:

Fiddle for reference. 提琴供参考。 Forgot to add html, body {height: 100%;} 忘记添加html, body {height: 100%;}

Finally found a decent solution. 终于找到了一个体面的解决方案。

It appears that: 看起来:

float, clear and vertical-align have no effect on a flex item. 浮动,清除和垂直对齐对弹性项目没有影响。

So I Found out the issue was with with your: 因此,我发现问题出在您的:

align-self: center;

on your second flex item. 在第二个弹性项目上。

So your css should be edited to: 因此,您的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;
}

As shown in this LIVE DEMO or run this snippet (in full page view): 如此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; } 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM