【问题标题】:justify-content to center using Flex Box使用 Flex Box 使内容居中
【发布时间】:2019-03-12 09:37:39
【问题描述】:

我正在尝试将我的 div 对齐到页面的中心,但我不知道如何使用 justify 选项来做到这一点。

这就是现在的样子:

html:

<div class="flex-container">

  <div class="box" *ngFor='let movie of moviesArray;let i=index'>
    ....
  </div>

</div>

css:

.flex-container{
    display: flex;
    background-color: white;
    flex-flow: column;
    flex-wrap: wrap;
    margin:0 auto;

}

.box{
    width:80%;
    flex: 0 0 100px;
    background-color:#f4f4f4;
    margin:10px 0;
    padding:20px;

}



....

感谢任何帮助:)

【问题讨论】:

    标签: html css flexbox centering


    【解决方案1】:

    当您在flex-direction: column 中时,主轴切换到垂直对齐,justify-content 向上/向下工作,而不是向左/向右工作。

    flex-direction: column 中使用align-items 水平居中。

    更多详情:In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

    【讨论】:

      【解决方案2】:

      .flex-container{
          display: flex;
          background-color: white;
          flex-flow: column;
          flex-wrap: wrap;
          margin:0 auto;
      
      }
      
      .box{
          display: flex;
          align-items: center;
          justify-content: center;
          width:80%;
          flex: 0 0 100px;
          background-color:#f4f4f4;
          margin:10px 0;
          padding:20px;
      
      }
      <div class="flex-container">
      
        <div class="box" *ngFor='let movie of moviesArray;let i=index'>
          ..................
        </div>
      
      </div>

      【讨论】:

      • 添加要显示的框类:flex ,justify-content 用于水平居中,align-items 用于垂直居中
      猜你喜欢
      • 2022-11-11
      • 2017-02-16
      • 1970-01-01
      • 2019-03-18
      • 2016-07-21
      • 2010-12-19
      • 2019-12-22
      • 2016-07-23
      • 2019-05-01
      相关资源
      最近更新 更多