页面容器未与页面居中对齐居中问题

时间:2019-11-30 18:02:54

标签: html css flexbox

我试图使页面容器居中,同时保持内容的左对齐。

我最初是在flexbox上尝试此操作,但是遇到了问题,所以有些复杂。

主包装容器居中,但内部包装容器未居中

flexbox是否可能?为什么在我告诉容器居中对齐的情况下,容器为何不居中?

链接到笔:https://codepen.io/jacksonrose/pen/RwNbMMm

<div class="center">Logo</div>

<div class="pageWrapper">
  <div class="country">USA</div>
  <div class="stockistItems">
    <div class="item">
      <div class="state">Montana</div>
      <span>766 Brackbill Road Gap PA<br>17527-9457 <br>+1 800 309 1500</span>
    </div>
    <div class="item">
      <div class="state">California</div>
      <span>766 Brackbill Road Gap PA<br>17527-9457 <br>+1 800 309 1500</span>
    </div>
    <div class="item">
      <div class="state">Arizona</div>
      <span>766 Brackbill Road Gap PA<br>17527-9457 <br>+1 800 309 1500</span>
    </div>
  </div>
</div>

body {
  margin: 0px;
  padding: 0px;
}
.center {
  text-align: center;
}
.pageWrapper {
    max-width: 800px;
    display: block;
    margin: 0 auto;
    padding-top: 100px;
  .country {
    font-size: 16px;
  }
  .stockistItems {
    display: flex;
    border-bottom: 1px solid #eaeaea;
    padding: 25px 0px;
    margin-bottom: 20px;
    justify-content: center;
    .item {
      display: flex;
      flex-direction: column;
      width: 33%;
    }
  }
}

主包装容器居中,但内部包装容器未居中

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

width:33%中删除.item,并给.stockListItems一个justify-content: space-between。这样会将.item div均匀拉伸到.pageWrapper容器的末尾。然后给.pageWrapper一个width: 90%

body {
  margin: 0px;
  padding: 0px;
}

.center {
  text-align: center;
}

.pageWrapper {
  max-width: 800px;
  display: block;
  margin: 0 auto;
  padding-top: 100px;
  width: 90%;
}

.pageWrapper .country {
  font-size: 16px;
}

.pageWrapper .stockistItems {
  display: flex;
  border-bottom: 1px solid #eaeaea;
  padding: 25px 0px;
  margin-bottom: 20px;
  /*justify-content: center;*/
  justify-content: space-between;
}

.pageWrapper .stockistItems .item {
  display: flex;
  flex-direction: column;
  /*width: 33%;*/
}
<div class="center">Logo</div>

<div class="pageWrapper">
  <div class="country">USA</div>
  <div class="stockistItems">
    <div class="item">
      <div class="state">Montana</div>
      <span>766 Brackbill Road Gap PA<br>17527-9457 <br>+1 800 309 1500</span>
    </div>
    <div class="item">
      <div class="state">California</div>
      <span>766 Brackbill Road Gap PA<br>17527-9457 <br>+1 800 309 1500</span>
    </div>
    <div class="item">
      <div class="state">Arizona</div>
      <span>766 Brackbill Road Gap PA<br>17527-9457 <br>+1 800 309 1500</span>
    </div>
  </div>
</div>