调整大小时无法居中div

3

我正在尝试将这些卡片居中显示,但是当我调整页面大小时,它们会偏离中心。如果可能的话,我希望只使用html和css来实现此目标。我想将卡片显示为每行4个。 有人能帮忙吗?谢谢

<style>
  .review-card {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    max-width: 200px;
    text-align: center;
    font-family: arial;
    padding: 5px;
    float: left;
    margin-left: 5%`enter code here`;
  }
</style>
<h1 style="text-align: center;">cards</h1>
<div class="row">
  <div class="column">
    <div class="review-card">

      <img class="alignnone size-medium wp-image-271388" src="https://expertpensions.co.uk/wp-content/uploads/2018/07/blank-profile-picture-973460_640-300x300.png" alt="image" width="300" height="300" />
      <h2>John</h2>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry'sstandard dummy text ever since the 1500s
    </div>
  </div>
  <div class="column">
    <div class="review-card">
      <img class="alignnone size-medium wp-image-271388" src="https://expertpensions.co.uk/wp-content/uploads/2018/07/blank-profile-picture-973460_640-300x300.png" alt="image" width="300" height="300" />
      <h2>Gayle</h2>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
    </div>
  </div>
  <div class="column">
    <div class="review-card">
      <img class="alignnone size-medium wp-image-271388" src="https://expertpensions.co.uk/wp-content/uploads/2018/07/blank-profile-picture-973460_640-300x300.png" alt="image" width="300" height="300" />
      <h2>Caroline</h2>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text
    </div>
  </div>
  <div class="column">
    <div class="review-card">
      <img class="alignnone size-medium wp-image-271388" src="https://expertpensions.co.uk/wp-content/uploads/2018/07/blank-profile-picture-973460_640-300x300.png" alt="image" width="300" height="300" />
      <h2>Linda</h2>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
    </div>
  </div>
</div>

1个回答

0

我稍微简化了你的代码,并减小了尺寸,只是为了在这里更好地显示。

修复的关键是使用 "inline-block" 来显示列,并在父容器上使用 text-align center。

<style>
  .row {
    background: pink;
    text-align: center;
  }
  
  .column {
    display: inline-block;
  }
  
  .review-card {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    max-width: 200px;
    text-align: center;
    font-family: arial;
    font-size: 12px;
    padding: 5px;
    background: white;
    margin-left: 1%;
  }
</style>
<h1 style="text-align: center;">cards</h1>
<div class="row">
  <div class="column">
    <div class="review-card">
      <img src="https://placehold.it/100x100?text=Temp+Image(100x100)">
      <h2>John</h2>
    </div>
  </div>
  <div class="column">
    <div class="review-card">
      <img src="https://placehold.it/100x100?text=Temp+Image(100x100)">
      <h2>Gayle</h2>
    </div>
  </div>
  <div class="column">
    <div class="review-card">
      <img src="https://placehold.it/100x100?text=Temp+Image(100x100)">
      <h2>Caroline</h2>
    </div>
  </div>
  <div class="column">
    <div class="review-card">
      <img src="https://placehold.it/100x100?text=Temp+Image(100x100)">
      <h2>Linda</h2>
    </div>
  </div>
</div>


谢谢您的回复,这似乎运行得很好 :) - undefined
嗨Emma,请在它对你有用的情况下接受答案 :) - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接