CSS Flexbox-按卡描述对齐

时间:2018-12-26 14:21:21

标签: css flexbox

我的卡片布局简单,我想按卡片说明进行对齐。 有人可以帮我吗? :)

enter image description here

这是一支钢笔:CODEPEN

这是我的HTML结构:

 <div class="card-container">
              <div class="card">
                 <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
                 <h4 class="card-title">Just a title</h4>
                 <p class="card-desc">A simple description</p>
              </div>

              <div class="card">
                 <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
                 <h4 class="card-title">Just a title</h4>
                 <p class="card-desc">A simple description</p>
              </div>

              <div class="card">
                 <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
                 <h4 class="card-title">A  veeeeery very very very large title</h4>
                 <p class="card-desc">omg, that description have broken :(</p>
              </div>

              <div class="card">
                 <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
                 <h4 class="card-title">Just a title</h4>
                 <p class="card-desc">A simple description</p>
              </div>

              <div class="card">
                 <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
                 <h4 class="card-title">Just a title</h4>
                 <p class="card-desc">A simple description</p>
              </div>
 </div>

1 个答案:

答案 0 :(得分:2)

通过将.card元素更改为

display: flex;
flex-direction: column;

,然后在说明中添加margin-top: auto;。这将导致描述元素被推到卡的底部。由于卡片垂直拉伸,因此对齐在水平轴上进行。如果您将align-items上的.card-container更改为stretch以外的其他内容,则不会是这种情况。

.card {
  display: flex;
  flex-direction: column;
  text-align: center;
  flex: 50%;
  padding: 15px 0;
}

.card-img img {
  width: 60px;
  padding: 15px;
}

.card-title {
  font-weight: 700;
  text-transform: uppercase;
  margin-bottom: 10px;
  font-size: 1.2rem;
}

.card-desc {
  font-weight: 400;
  margin: 0;
  margin-top: auto;
  line-height: 1rem;
}

.card-container {
  background: rgba(255, 0, 0, 0.432);
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  flex: 100%;
}

@media screen and (min-width: 500px) {
  .card {
    flex: 1;
  }
  .card-img img {
    width: 50px;
  }
  .card-title {
    font-weight: 700;
    text-transform: uppercase;
    margin-bottom: 10px;
    font-size: 1.2rem;
  }
  .card-desc {
    font-weight: 400;
    line-height: 1rem;
  }
  .card-container {
    background: rgba(255, 0, 0, 0.432);
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    flex: 100%;
  }
}
<div class="card-container">
  <div class="card">
    <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
    <h4 class="card-title">Just a title</h4>
    <p class="card-desc">A simple description</p>
  </div>

  <div class="card">
    <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
    <h4 class="card-title">Just a title</h4>
    <p class="card-desc">A simple description</p>
  </div>

  <div class="card">
    <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
    <h4 class="card-title">A veeeeery very very very large title</h4>
    <p class="card-desc">omg, that description have broken :(</p>
  </div>

  <div class="card">
    <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
    <h4 class="card-title">Just a title</h4>
    <p class="card-desc">A simple description</p>
  </div>

  <div class="card">
    <div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
    <h4 class="card-title">Just a title</h4>
    <p class="card-desc">A simple description</p>
  </div>
</div>