【问题标题】:flex-wrap causing next line to have too big of a gap [duplicate]flex-wrap导致下一行有太大的差距[重复]
【发布时间】:2016-04-14 12:59:33
【问题描述】:

我有一个用于将两行文本垂直居中的弹性框。两行文字相距太远。这是我正在谈论的图像:

这是我的代码:

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>

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

【问题讨论】:

  • Ahhhhhhhh 关闭一些标签! :-)

标签: html css flexbox


【解决方案1】:

使用flex-direction: column 并删除h1h3 的边距

.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;
}
<div class="vertical-center">
   <h1 class="text-center white flexbox-text">THIS IS TITLE</h1>
   <h3 class="text-center white flexbox-text">SUBTITLE</h3>
</div>

【讨论】:

    【解决方案2】:

    编辑:否决票?!我正在回答这个问题...

    对我来说似乎很简单... https://jsfiddle.net/krL755h8/ 可能是行高、内边距或边距或 flexbox 的错误使用导致问题。

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

    标题

    字幕

    【讨论】:

    • 干杯。不知道谁投了反对票。我投票赞成,哈哈
    【解决方案3】:

    在 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,导致行分布均匀在 flex 容器中。

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 2019-10-18
      • 2018-09-16
      • 1970-01-01
      • 2021-09-20
      • 2016-07-25
      • 1970-01-01
      • 2020-03-16
      相关资源
      最近更新 更多