代码之家  ›  专栏  ›  技术社区  ›  Johnson

CSS flexbox-按卡对齐说明

  •  0
  • Johnson  · 技术社区  · 5 年前

    我有一个简单的卡片布局,我想按照卡片描述对齐它。 有人能帮我吗?:)

    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 回复  |  直到 5 年前
        1
  •  2
  •   chazsolo    5 年前

    通过改变 .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>