Flexbox对齐到底部

6
我有以下布局 http://jsbin.com/joyetaqase/1/edit?html,css,output
<div class="row">

    <div class="col">
      <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
      <p>my footer</p>
    </div>

     <div class="col">
      <h2>Lorem Ipsum.</h2>
      <p>my footer</p>
    </div>

  </div>

使用flexbox,我正在尝试使.col div具有相同的高度和宽度。
我的问题是:如何将<p>放在框的底部?
布局应如下所示:

enter image description here

我知道我可以将<p>绝对定位并使用bottom:0;,但我想用flexbox实现这个效果,这可行吗?
有人能解释一下吗?
3个回答

3
你可以按照以下方式操作。给.col添加display: flex;flex-direction: column;属性,给h2添加flex: 1 0 auto;属性。

.row {
  width: 500px;
  font-family: monospace;
  display:flex;
}

.col {
  width: 50%;
  border: 1px solid #000;
  padding: 10px;
  box-sizing: border-box;
  display: flex;
    flex-direction: column;
}

h2, p {
  font-weight: normal;
  margin: 0;
}

h2 {
    flex: 1 0 auto;
}
 <div class="row">
    
    
    <div class="col">
      <h2>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2>
      <p>my footer</p>
    </div>
    
     <div class="col">
      <h2>Lorem Ipsum.</h2>
      <p>my footer</p>
    </div>
    
    
  </div>

Updated JSBin


2
@Hiero,很高兴能帮到你。别忘了在方便的时候接受答案。 - ketan

1
使 .col 成为嵌套的、列方向的 flex 容器。
.col {
  width: 50%;
  border: 1px solid #000;
  padding: 10px;
  box-sizing: border-box;

  /* NEW */
  display: flex;
  flex-direction: column;          /* stack <h2> and <p> vertically */
  justify-content: space-between;  /* align <h2> at top and <p> at bottom */
}

0

给父元素(.col)设置相对定位,然后使用属性 bottom:0px; 给页脚设置绝对定位。

JSBin

.col {
  width: 50%;
  border: 1px solid #000;
  padding: 10px;
  box-sizing: border-box;
  position:relative;
}

p{
  position:absolute;
  bottom:0px;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接