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

Flexbox-卡片中的内容要均匀分布

  •  0
  • n1234  · 技术社区  · 7 年前

    这也许是一个很简单的答案,但我很沮丧。我有一个并排有两个项目的flex容器;其中一个是图像,另一个是内容。在内容中,它在水平和垂直方向居中,但内容堆叠得非常紧密(标题、文本、按钮)。有没有办法让它们均匀地分布在卡片的高度上?

    #container {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
    
    #container-image>img {
      display: block;
      width: 100%;
    }
    
    #container-image {
      flex: 1;
    }
    
    #container-content {
      flex: 2;
      align-self: center;
      text-align: center;
    }
    
    .button {
      background-color: blue;
      color: white;
      padding: 6px 30px;
      text-align: center;
      display: inline-block;
      font-size: 16px;
      border: 1px solid #34495e;
    }
    HTML:
    
    <section id="container">
      <div id="container-image">
        <img src="https://www.placecage.com/c/1500/2000" alt="" />
      </div>
    
      <div id="container-content">
        <h3>title</h3>
        <p>some text about this card</p>
        <a class="button">more...</a>
      </div>
    </section>
    1 回复  |  直到 7 年前
        1
  •  1
  •   Temani Afif    7 年前

    考虑使用列方向为文本设置另一个flexbox容器,然后您可以轻松地按需对其进行分隔:

    #container {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
    
    #container-image>img {
      display: block;
      width: 100%;
    }
    
    #container-image {
      flex: 1;
    }
    
    #container-content {
      flex: 2;
      text-align: center;
      display:inline-flex;
      flex-direction:column;
      justify-content:space-evenly; /*you can also use space-between OR space-around*/
    }
    
    .button {
      background-color: blue;
      color: white;
      padding: 6px 30px;
      text-align: center;
      display: inline-block;
      align-self:center;
      font-size: 16px;
      border: 1px solid #34495e;
    }
    <section id="container">
      <div id="container-image">
        <img src="https://www.placecage.com/c/1500/2000" alt="" />
      </div>
    
      <div id="container-content">
        <h3>title</h3>
        <p>some text about this card</p>
        <a class="button">more...</a>
      </div>
    </section>