调整大小时无法居中 div

Posted

技术标签:

【中文标题】调整大小时无法居中 div【英文标题】:Cannot center div when resizing 【发布时间】:2018-12-13 02:08:29 【问题描述】:

我试图将这些卡片显示在中心,但是当我调整页面大小时,它们会偏离中心。如果可能的话,我想通过仅使用 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"    />
      <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"    />
      <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"    />
      <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"    />
      <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】:

我已经稍微简化了你的代码,并减小了大小,只是为了让它在这里显示得更好。

修复的本质是使用“inline-block”使列显示并在父容器上使用文本对齐中心

<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>

【讨论】:

感谢您的回复,这似乎工作正常 :) 嗨,Emma,如果它对你有用,请接受答案:)

以上是关于调整大小时无法居中 div的主要内容,如果未能解决你的问题,请参考以下文章

使用 next.js 调整窗口大小时无法居中我的图像

调整大小以适合 div 中的图像,并水平和垂直居中

调整浏览器大小时,如何使部分中的对象保持居中?

在容器调整大小时调整传单地图的大小

调整 div 大小时无法让 Google 地图高度自动调整大小

元素不会保持居中,尤其是在调整屏幕大小时