将div元素与容器底部对齐

时间:2016-09-21 19:51:50

标签: html css css3 flexbox

我试图在容器内部对齐三个div中的一个,但我遇到了一些问题。

正如您将看到的,如果我尝试在bottom-aligned div上使用绝对定位,则高度成为问题。

我可以设置一个特定的高度,但cta-item div中的内容可以从非常小到非常大。

我正在尝试为所有用例创建解决方案。

我知道我可以使用jQuery来获取最大cta-item div的高度并相应地进行调整,但我尽量避免使用jQuery / JS。

我用我现在拥有的东西创造了一个小提琴here

提前谢谢!

.dynamic-cta-ctr {
  display: flex;
}
.dynamic-cta-ctr .cta-item {
  flex: 1;
  text-align: center;
  padding: 0 30px;
  position: relative;
  border-right: 1px solid #555;
}
.dynamic-cta-ctr .cta-item:last-child {
  border-right: none;
}
.dynamic-cta-ctr .cta-item .bottom-aligned {
  /* position: absolute;
  bottom: 0; */
}
<div class="dynamic-cta-ctr">
  <div class="cta-item">
    <div class="heading-text"><h3>Some content</h3></div>
    <div class="cta-field-content">Some content that sort of hangs around</div>
    <div class="bottom-aligned"><h5>Bottom aligned content</h5></div>
  </div>
  <div class="cta-item">
    <div class="heading-text"><h3>You can have your cake and eat it too!</h3></div>
    <div class="cta-field-content">Some content that sort of hangs around</div>
    <div class="bottom-aligned"><h5>Bottom aligned content</h5></div>
  </div>
  <div class="cta-item">
    <div class="heading-text"><h3>More people will come if you say we have punch and pie</h3></div>
    <div class="cta-field-content">Some content that sort of hangs around</div>
    <div class="bottom-aligned"><h5>Bottom aligned content</h5></div>
  </div>
</div>

1 个答案:

答案 0 :(得分:7)

将此添加到您的代码中:

.cta-item {
   display: flex;
   flex-direction: column;
}

.bottom-aligned {
    margin-top: auto;
}

revised fiddle

此处有更多详情:Methods for Aligning Flex Items