如何使Flexbox项目高度相等和/或从同一点开始

时间:2019-04-10 02:51:06

标签: html css css3 flexbox alignment

我正在创建Flexbox卡以显示三个元素。标题,<hr>和段落。标题文本的长度各不相同,因此高度也有所不同。

我希望所有标题都具有相同的高度,以便每张卡都是统一的。如果无法做到这一点,我希望在每张卡上将段落水平对齐。

我尝试为标题提供flex: 1属性以将段落下推,但它会占用过多空间。

这里是codepen和演示:

  body {
  max-width: 1180px;
  margin: 0 auto;
  }
  h2,
  p {
  margin: 0;
  padding: 0;
  }
  h2 {
  font-size: 18px;
  }
  /*******************************************************/
  .cards {
    display: flex;
  }
  .card {
    border: 1px solid;
    display: flex;
    flex-direction: column;
    margin: 0 0 25px 0;
    width: 50%;
  }
  .card-title {
    text-transform: uppercase;
    text-align: center;
    margin: 0 0 25px 0;
  }
  .card-paragraph {
    text-align: center;
  }
  hr {
    height: 3px;
    width: 100%;
    background: #333;
  }
  /*******************************************************/
<body>

  <div class="cards">
    <div class="card">
      <div class="card-title">
        Title one IS LONGER THAN TITLE TWO
      </div>
      <hr>
      <div class="card-paragraph">  
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text.
      </div>
    </div>
    <div class="card">
      <div class="card-title">
        TITLE TWO
      </div>
      <hr>
      <div class="card-paragraph">  
        Lorem Ipsum is simply dummy text of the printing and typesetting industry.
      </div>
    </div>
  </div>
  
</body>

我想要的

enter image description here

正在发生的事情

enter image description here

3 个答案:

答案 0 :(得分:2)

我建议使用CSS Grid这样的东西:

body {
  max-width: 1180px;
  margin: 0 auto;
}

h2,
p {
  margin: 0;
  padding: 0;
}

h2 {
  font-size: 18px;
}


/*******************************************************/

.cards {
  display: grid;
  grid-auto-columns: auto;
  grid-auto-rows: auto;
}

.card {
  border: 1px solid;
  grid-row: 1;
  display: grid;
  grid-auto-rows: 1fr;
}

.card-title {
  text-transform: uppercase;
  text-align: center;
  margin: 0 0 25px 0;
}

.card-paragraph {
  text-align: center;
}

hr {
  height: 3px;
  width: 100%;
  background: #333;
}


/*******************************************************/
<body>

  <div class="cards">
    <div class="card">
      <div class="card-title">
        Title one IS LONGER THAN TITLE TWO Title one IS LONGER THAN TITLE TWO Title one IS LONGER THAN TITLE TWO
      </div>
      <hr>
      <div class="card-paragraph">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text.
      </div>
    </div>
    <div class="card">
      <div class="card-title">
        TITLE TWO
      </div>
      <hr>
      <div class="card-paragraph">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry.
      </div>
    </div>
  </div>

</body>

答案 1 :(得分:2)

可能无法对齐相邻的flexbox 容器-CSS网格可能是一个不错的选择:

  • display: contents上使用card或完全移除包装器

  • 使用2列网格,并使用card-title

  • grid-row: 1置于第一行
  • 使用card-paragraphgrid-row: 3放置到第三行,并使用hrgrid-row: 2放置在中间。

请参见下面的演示

body {
  max-width: 1180px;
  margin: 0 auto;
}

h2,
p {
  margin: 0;
  padding: 0;
}

h2 {
  font-size: 18px;
}

.cards {
  /* display: flex; */
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.card {
  /* border: 1px solid;
  display: flex;
  flex-direction: column;
  margin: 0 0 25px 0;
  width: 50%;*/
  display: contents; /* added */
}

.card-title {
  text-transform: uppercase;
  text-align: center;
  /* margin: 0 0 25px 0;*/
  grid-row: 1; /* added */
  border: 1px solid;  /* added */
}

.card-paragraph {
  text-align: center;
  grid-row: 3; /* added */
  border: 1px solid;  /* added */
}

hr {
  height: 3px;
  /* width: 100%; */
  background: #333;
  margin: 0; /* added */
  grid-row: 2; /* added */
}
<div class="cards">
    <div class="card">
      <div class="card-title">
        Title one IS LONGER THAN TITLE TWO
      </div>
      <hr>
      <div class="card-paragraph">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text.
      </div>
    </div>
    <div class="card">
      <div class="card-title">
        TITLE TWO
      </div>
      <hr>
      <div class="card-paragraph">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry.
      </div>
    </div>
  </div>

答案 2 :(得分:1)

只有通过设置标题的高度才能使用flexbox: .card-title { ... height: 50px; } Codepen