如何将弹性项目居中?

时间:2018-03-22 10:03:24

标签: html css css3 flexbox centering

我想要左边的ABCD,DATE应该在中心。怎么做到这一点?



.flex-items {
  display: flex;
  justify-content: flex-start;
  padding-left: 17px;
}

.abcd1 {}

.date1 {
  /*this item should be in the center.*/
}

<div class="flex-items">
  <div class="abcd1">ABCD</div>
  <div class="date1">DATE</div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

试试此代码

&#13;
&#13;
.flex-items {

  display: flex;
  justify-content: flex-start;
  padding-left: 17px;
}

.abcd1 {

}

.date1 {
    margin-left: auto;
    margin-right: auto;
}
&#13;
<div class="flex-items">

<div class="abcd1">ABCD</div>
<div class="date1">DATE</div>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

见下文。希望这会有所帮助。

&#13;
&#13;
.flex-items {
  display: flex;
  padding-left: 17px;
  text-align: center;
}

.date1 {
  flex: 1;
}
&#13;
<div class="flex-items">
  <div class="abcd1">ABCD</div>
  <div class="date1">DATE</div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

文字对齐:中心在提供宽度时有效。 这也有效:

&#13;
&#13;
.flex-items {
     display: flex;
     justify-content: flex-start;
     padding-left: 17px;
}
.abcd1 {
          
}     
.date1 {
     /*this item should be in the center.*/
     text-align: center;
     width: 100%;
}
&#13;
    <div class="flex-items">
    <div class="abcd1">ABCD</div>
    <div class="date1">DATE</div>
</div>
&#13;
&#13;
&#13;