Flexbox列:垂直对齐最后一个元素

时间:2015-05-15 16:11:44

标签: css flexbox

我在三个flexbox列中有不等量的内容,但我希望每列底部显示的按钮排成一行。我怎么能这样做?

代码I' m Demo



.container {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  position: relative;
  margin: 0 auto;
  padding: 25px 0;
  width: 500px;
  border: 1px solid #000;
}
.col {
  width: 33.33%;
  text-align: center;
}
.col:first-child:after,
.col:last-child:before {
  content: "";
  position: absolute;
  top: 10%;
  left: 33.33%;
  width: 1px;
  height: 80%;
  background-color: #000;
}
.col:last-child:before {
  left: 66.67%;
}

<div class="container">
  <div class="col">
    <p>Foo</p>
    <button>Button</button>
  </div>
  <div class="col">
    <p>Foo</p>
    <p>Bar</p>
    <button>Button</button>
  </div>
  <div class="col">
    <p>Foo</p>
    <p>Bar</p>
    <p>Baz</p>
    <button>Button</button>
  </div>
</div>
&#13;
&#13;
&#13;

我现在得到的是什么

Buttons that are not aligned

我想得到什么

Buttons that are aligned

1 个答案:

答案 0 :(得分:3)

添加

.col {
    display: flex;          /* Magic begins */
    flex-direction: column; /* Column layout */
    align-items: center;    /* Center contents horizontally */
}
button {
    margin-top: auto;       /* Push to the bottom */
}

.container {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  position: relative;
  margin: 0 auto;
  padding: 25px 0;
  width: 500px;
  border: 1px solid #000;
}
.col {
  width: 33.33%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.col:first-child:after,
.col:last-child:before {
  content: "";
  position: absolute;
  top: 10%;
  left: 33.33%;
  width: 1px;
  height: 80%;
  background-color: #000;
}
.col:last-child:before {
  left: 66.67%;
}
button {
  margin-top: auto;
}
<div class="container">
  <div class="col">
    <p>Foo</p>
    <button>Button</button>
  </div>
  <div class="col">
    <p>Foo</p>
    <p>Bar</p>
    <button>Button</button>
  </div>
  <div class="col">
    <p>Foo</p>
    <p>Bar</p>
    <p>Baz</p>
    <button>Button</button>
  </div>
</div>