flex-wrap导致下一行有太大的差距

时间:2016-01-10 01:02:07

标签: html css css3 flexbox

我有这个flexbox,用于垂直居中两行文字。两行文字相距太远。这是我正在谈论的图像:

enter image description here

这是我的代码:

CSS

.vertical-center {
  min-height: 100%;  /* Fallback for vh unit */
  min-height: 100vh; /* You might also want to use
                        'height' property instead.

                        Note that for percentage values of
                        'height' or 'min-height' properties,
                        the 'height' of the parent element
                        should be specified explicitly.

                        In this case the parent of '.vertical-center'
                        is the <body> element */

  /* Make it a flex container */
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex; 

  /* Align the bootstrap's container vertically */
    -webkit-box-align : center;
  -webkit-align-items : center;
       -moz-box-align : center;
       -ms-flex-align : center;
          align-items : center;

  /* In legacy web browsers such as Firefox 9
     we need to specify the width of the flex container */
  width: 100%;

  /* Also 'margin: 0 auto' doesn't have any effect on flex items in such web browsers
     hence the bootstrap's container won't be aligned to the center anymore.

     Therefore, we should use the following declarations to get it centered again */
         -webkit-box-pack : center;
            -moz-box-pack : center;
            -ms-flex-pack : center;
  -webkit-justify-content : center;
          justify-content : center;

          flex-wrap: wrap;

}

p.white, h1.white, h2.white, h3.white, h4.white, h5.white, h6.white {
    color: white;
}

.flexbox-text {
    margin: -50px, 0px, -50px, 0px;
}

HTML

<div class="vertical-center">
      <h1 class="text-center white flexbox-text">{{mottoText}}</h1>
      <h3 class="text-center white flexbox-text">{{mottoSubText}}</h3>
    </div>

忽略角度两种语法。如何让两行文本更接近?

3 个答案:

答案 0 :(得分:3)

使用hosts并从flex-direction: columnh1

中删除边距

&#13;
&#13;
h3
&#13;
.vertical-center {
  display: flex;
  flex-direction: column;
  height: 100vh;
  align-items: center;
  justify-content: center;
}

p.white, h1.white, h2.white, h3.white, h4.white, h5.white, h6.white {
    color: black;
    margin: 0;
}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

编辑:downvote ?!我正在回答这个问题...

对我来说似乎很简单...... https://jsfiddle.net/krL755h8/ 可能是线高,填充或边距或不正确使用flexbox导致问题。

html {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}

标题

   

副标题

答案 2 :(得分:2)

在Flex容器的横轴上放置单行时,可以使用align-items

但是当容器有多行时 - 例如当flex项目换行时 - align-content属性变为转换行所必需的。

  

8.4. Packing Flex Lines: the align-content property

     

align-content属性对齐Flex容器的行   当横轴有额外的空间时,flex容器,   类似于justify-content如何对齐单个项目   主轴。注意,此属性对单线flex没有影响   容器

网页上“两行文字相距太远”的原因是因为align-content的初始值为stretch,导致行在柔性容器中均匀分布。

针对不同的路线,在align-content上尝试不同的值。例如,align-content: center将两条线放在容器的中心。

.vertical-center {
    min-height: 100%;
    min-height: 100vh;
    display: flex;
    align-items: center;
    width: 100%;
    justify-content: center;
    flex-wrap: wrap;
    align-content: center; /* NEW */
}

p.white,
h1.white,
h2.white,
h3.white,
h4.white,
h5.white,
h6.white {
    color: black;
}

.flexbox-text {
    margin: -50px, 0px, -50px, 0px;
}
<div class="vertical-center">
    <h1 class="text-center white flexbox-text">MINIMISE WASTE. SUSTAINABLE LIVING. SUPPORT COMMUNITY</h1>
    <h3 class="text-center white flexbox-text">SHARE FOOD WITH OTHERS IN YOUR AREA</h3>
</div>