如何使用FlexBox均匀分隔列?

时间:2019-01-10 22:09:52

标签: html css css3 flexbox responsive

我们有一组结果,应在三列中显示。使用Flexbox,我们可以获得想要完成的大部分任务(即,所有人都具有相同的高度,在顶部正确对齐(请参见flex__top,在底部正确对齐(请参见{{ 1}})。

但是,我们遇到了一个问题,当flex__bottom元素之一的高度不同于其他元素的高度时,对齐方式将关闭。

我们如何在flex__bottom节开始于相同位置的同时对齐所有列以具有相同的高度?

目标

  1. 确保所有列的高度相同
  2. 对于flex__bottom中的每个项目,都应有一个flex__bottom与所有其他项目对齐。

问题

current issue flexbox

所需的输出 desired output flexbox

这里提供了Codepen,以防难以在内联代码段中进行复制

border-top
/* Columns */

@media screen and (min-width: 320px) {
  .col-m-12 {
    width: 100%;
    padding: 16px;
    display: flex;
    justify-content: center;
    align-items: stretch;
  }
}

@media screen and (min-width: 768px) {
  .col-t-4 {
    width: calc(33% - 48px);
  }
}


/* Flex */

.flex__wrapper {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: stretch;
  flex-direction: row;
  flex: 0 1 auto;
}

.flex__link {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  flex-direction: column;
  background-color: #eee;
}

.flex__top {
  padding: 16px;
}

.flex__bottom {
  border-top: 1px solid;
}


/* Image */

img {
  height: auto;
  display: block;
  width: 100%;
}


/* Lists */

ul {
  list-style: none;
}

ul>li {
  display: inline-flex;
  border: 1px solid;
  margin: 4px;
  padding: 4px;
}


/* Link */

a {
  text-decoration: none;
  color: #000;
}

2 个答案:

答案 0 :(得分:0)

如果您可以限制项目标题和说明中的内容量,则可以为.flex-top添加最小高度。

/* Columns */

@media screen and (min-width: 320px) {
  .col-m-12 {
    width: 100%;
    padding: 16px;
    display: flex;
    justify-content: center;
    align-items: stretch;
  }
}

@media screen and (min-width: 768px) {
  .col-t-4 {
    width: calc(33% - 48px);
  }
}


/* Flex */

.flex__wrapper {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: stretch;
  flex-direction: row;
  flex: 0 1 auto;
}

.flex__link {
  display: flex;
  flex-direction: column;
  background-color: #eee;
}

.flex__top {
  padding: 0 16px;
  min-height: 350px;
}

.flex__bottom {
  border-top: 1px solid;
}


/* Image */

img {
  height: auto;
  display: block;
  width: 100%;
}


/* Lists */

ul {
  list-style: none;
}

ul>li {
  display: inline-flex;
  border: 1px solid;
  margin: 4px;
  padding: 4px;
}


/* Link */

a {
  text-decoration: none;
  color: #000;
}
<div class="flex__wrapper">
  <div class="col-m-12 col-t-4">
    <a class="flex__link" href="#">
      <div class="flex__top">
        <img src="https://placeimg.com/640/480/arch" class="flex__img">
        <h2 class="flex__h2">Check back tomorrow;</h2>
        <p>I will see if the book has arrived.</p>
      </div>
      <div class="flex__bottom">
        <ul>
          <li>Learn More</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="col-m-12 col-t-4">
    <a class="flex__link" href="#">
      <div class="flex__top">
        <img src="https://placeimg.com/640/480/nature" class="flex__img">
        <h2 class="flex__h2">I am happy</h2>
        <p>I am never at home on Sundays.</p>
      </div>
      <div class="flex__bottom">
        <ul>
          <li>Learn More</li>
          <li>Progressively matrix</li>
          <li>Uniquely build extensible</li>
          <li>seamless customer</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="col-m-12 col-t-4">
    <a class="flex__link" href="#">
      <div class="flex__top">
        <img src="https://placeimg.com/640/480/people" class="flex__img">
        <h2 class="flex__h2">Malls are great places to shop</h2>
        <p>I can find everything I need under one roof.</p>
      </div>
      <div class="flex__bottom">
        <ul>
          <li>maximizing</li>
          <li>Monotonectally whiteboard</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="col-m-12 col-t-4">
    <a class="flex__link" href="#">
      <div class="flex__top">
        <img src="https://placeimg.com/640/480/tech" class="flex__img">
        <h2 class="flex__h2">I want to buy</h2>
        <p>but know it won’t suit me.</p>
      </div>
      <div class="flex__bottom">
        <ul>
          <li>Learn More</li>
          <li>task error-free</li>
          <li>Energistically</li>
          <li>practices</li>
          <li>ethical methodologies</li>
          <li>task error-free</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="col-m-12 col-t-4">
    <a class="flex__link" href="#">
      <div class="flex__top">
        <img src="https://placeimg.com/640/480/animals" class="flex__img">
        <h2 class="flex__h2">Last Friday in three week</h2>
        <p>Blue worm shake hands with a legless lizard.</p>
      </div>
      <div class="flex__bottom">
        <ul>
          <li>Learn More</li>
          <li>incubate</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="col-m-12 col-t-4">
    <a class="flex__link" href="#">
      <div class="flex__top">
        <img src="https://placeimg.com/640/480/animals/grayscale" class="flex__img">
        <h2 class="flex__h2">She advised</h2>
        <p>him to come back at once.</p>
      </div>
      <div class="flex__bottom">
        <ul>
          <li>Learn More</li>
          <li>Seamlessly</li>
          <li>build excellent</li>
        </ul>
      </div>
    </a>
  </div>
</div>

但是,对于这种布局,网格也许是一个更好的选择

@media screen and (min-width: 640px) {
  .grid__wrapper {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 10px;
  }
}

.grid__item {
  background: #ccc;
  padding: 10px;
  margin-bottom: 10px;
}

@media screen and (min-width: 640px) {
  .grid__item {
    margin-bottom: 0;
  }
}

@media screen and (min-width: 640px) {
  .grid__link {
    height: 100%;
    display: grid;
    grid-auto-rows: 1fr;
  }
}

.item__bottom {
  border-top: 1px solid;
}

img {
  height: auto;
  display: block;
  width: 100%;
}

ul {
  list-style: none;
}

ul>li {
  display: inline-flex;
  border: 1px solid;
  margin: 4px;
  padding: 4px;
}

a {
  text-decoration: none;
  color: #000;
}
<div class="grid__wrapper">
  <div class="grid__item">
    <a class="grid__link" href="#">
      <div>
        <img src="https://placeimg.com/640/480/arch" class="flex__img">
        <h2>Check back tomorrow;</h2>
        <p>I will see if the book has arrived.</p>
      </div>
      <div class="item__bottom">
        <ul>
          <li>Learn More</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="grid__item">
    <a class="grid__link" href="#">
      <div>
        <img src="https://placeimg.com/640/480/nature" class="flex__img">
        <h2>I am happy</h2>
        <p>I am never at home on Sundays.</p>
      </div>
      <div class="item__bottom">
        <ul>
          <li>Learn More</li>
          <li>Progressively matrix</li>
          <li>Uniquely build extensible</li>
          <li>seamless customer</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="grid__item">
    <a class="grid__link" href="#">
      <div>
        <img src="https://placeimg.com/640/480/people" class="flex__img">
        <h2>Malls are great places to shop</h2>
        <p>I can find everything I need under one roof.</p>
      </div>
      <div class="item__bottom">
        <ul>
          <li>maximizing</li>
          <li>Monotonectally whiteboard</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="grid__item">
    <a class="grid__link" href="#">
      <div>
        <img src="https://placeimg.com/640/480/tech" class="flex__img">
        <h2>I want to buy</h2>
        <p>but know it won’t suit me.</p>
      </div>
      <div class="item__bottom">
        <ul>
          <li>Learn More</li>
          <li>task error-free</li>
          <li>Energistically</li>
          <li>practices</li>
          <li>ethical methodologies</li>
          <li>task error-free</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="grid__item">
    <a class="grid__link" href="#">
      <div>
        <img src="https://placeimg.com/640/480/animals" class="flex__img">
        <h2>Last Friday in three week</h2>
        <p>Blue worm shake hands with a legless lizard. Blue worm shake hands with a legless lizard. Blue worm shake hands with a legless lizard. Blue worm shake hands with a legless lizard. </p>
      </div>
      <div class="item__bottom">
        <ul>
          <li>Learn More</li>
          <li>incubate</li>
        </ul>
      </div>
    </a>
  </div>
  <div class="grid__item">
    <a class="grid__link" href="#">
      <div>
        <img src="https://placeimg.com/640/480/animals/grayscale" class="flex__img">
        <h2>She advised</h2>
        <p>him to come back at once.</p>
      </div>
      <div class="item__bottom">
        <ul>
          <li>Learn More</li>
          <li>Seamlessly</li>
          <li>build excellent</li>
        </ul>
      </div>
    </a>
  </div>
</div>

答案 1 :(得分:0)

要获得预期结果,请为flex__bottom设置高度

问题:未指定flex__bottom高度,子元素也不均匀,默认高度:auto将根据flex__bottom元素调整高度

.flex__bottom {
    border-top: 1px solid;
    height: 100px;
}

https://codepen.io/nagasai/pen/LMgxLg