使用flexbox将元素与底部对齐

时间:2015-06-23 10:56:14

标签: html css flexbox

我有一些div和一些孩子:

<div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some more or less text</p>
  <a href="/" class="button">Click me</a>
</div>

我想要以下布局:

 -------------------
|heading 1          |
|heading 2          | 
|paragraph text     |
|can have many      |
|rows               |
|                   |
|                   |
|                   | 
|link-button        |
 -------------------

无论p中有多少文字,我都希望.button始终位于底部,而不会将其从流程中取出。我听说这可以通过Flexbox实现,但我无法让它工作。

9 个答案:

答案 0 :(得分:467)

您可以使用auto margins

  

在通过justify-contentalign-self进行对齐之前,   任何正的自由空间都会分配到汽车边距   尺寸。

所以你可以使用其中一个(或两个):

p { margin-bottom: auto; } /* Push following elements to the bottom */
a { margin-top: auto; } /* Push it and following elements to the bottom */

&#13;
&#13;
.content {
  height: 200px;
  border: 1px solid;
  display: flex;
  flex-direction: column;
}
h1, h2 {
  margin: 0;
}
a {
  margin-top: auto;
}
&#13;
<div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some text more or less</p>
  <a href="/" class="button">Click me</a>
</div>
&#13;
&#13;
&#13;

或者,您可以在a增长之前填充元素以填充可用空间:

p { flex-grow: 1; } /* Grow to fill available space */

&#13;
&#13;
.content {
  height: 200px;
  border: 1px solid;
  display: flex;
  flex-direction: column;
}
h1, h2 {
  margin: 0;
}
p {
  flex-grow: 1;
}
&#13;
<div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some text more or less</p>
  <a href="/" class="button">Click me</a>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:116)

您可以使用display:flex将元素定位到底部,但我认为您不想在这种情况下使用flex,因为它会影响您的所有元素。

要使用flex将元素定位到底部,请尝试:

.container {
  display: flex;
}

.button {
  align-self: flex-end;
}

您最好的选择是将位置:绝对设置为按钮并将其设置为bottom: 0,或者您可以将按钮放在容器外部并使用否定transform: translateY(-100%)将其放入容器中,如下所示:

.content {
    height: 400px;
    background: #000;
    color: #fff;
}
.button {
    transform: translateY(-100%);
    display: inline-block;
}

选中此JSFiddle

答案 2 :(得分:60)

使用align-self: flex-end;的解决方案对我不起作用,但如果您想使用flex,则会执行此操作:

CSS

.content{
  display: flex;
  justify-content: space-between;
  flex-direction: column;
}

HTML

<div class="content">
  <div>
    <h1>heading 1</h1>
    <h2>heading 2</h2>
    <p>Some more or less text</p>
  </div>

  <div>
    <a href="/" class="button">Click me</a>
  </div>
</div>

结果:

 -------------------
|heading 1          |
|heading 2          | 
|paragraph text     |
|can have many      |
|rows               |
|                   |
|                   |
|                   | 
|link-button        |
 -------------------

答案 3 :(得分:23)

1。样式父元素:style="display:flex; flex-direction:column; flex:1;"

2。设置要保留在底部的元素的样式:style="margin-top: auto;"

3。做完了!哇。那很容易。

示例:

enter image description here

<section style="display:flex; flex-wrap:wrap;"> // For demo, not necessary
    <div style="display:flex; flex-direction:column; flex:1;"> // Parent element
        <button style="margin-top: auto;"> I </button> // Target element
    </div>

    ... 5 more identical divs, for demo ...

</section>

演示:https://codepen.io/ferittuncer/pen/YzygpWX

答案 4 :(得分:5)

将显示设置为 flex 时,只需使用flex属性来标记哪些内容可以增长,哪些内容不能增长。

Flex property

div.content {
 height: 300px;
 display: flex;
 flex-direction: column;
}

div.up {
  flex: 1;
}

div.down {
  flex: none;
}
<div class="content">
  <div class="up">
    <h1>heading 1</h1>
    <h2>heading 2</h2>
    <p>Some more or less text</p>
  </div>

  <div class="down">
    <a href="/" class="button">Click me</a>
  </div>
</div>

答案 5 :(得分:1)

我刚刚找到了解决方案。

对于那些对给定答案不满意的人可以尝试使用flexbox这种方法

CSS

    .myFlexContainer {
        display: flex;
        width: 100%;
    }

    .myFlexChild {
        width: 100%;
        display: flex;

        /* 
         * set this property if this is set to column by other css class 
         *  that is used by your target element 
         */
        flex-direction: row;

        /* 
         * necessary for our goal 
         */
        flex-wrap: wrap;
        height: 500px;
    }

    /* the element you want to put at the bottom */
    .myTargetElement {
        /*
         * will not work unless flex-wrap is set to wrap and 
         * flex-direction is set to row
         */
        align-self: flex-end;
    }

HTML

    <div class="myFlexContainer">
        <div class="myFlexChild">
            <p>this is just sample</p>
            <a class="myTargetElement" href="#">set this to bottom</a>
        </div>
    </div>

答案 6 :(得分:1)

如果您能够修改 HTML 结构,您可以将所有顶部元素包装在一个单独的 div 中,然后使用 justify-content: space-between

像这样:

<div class="content">
  <div>
    <h1>heading 1</h1>
    <h2>heading 2</h2>
    <p>Some more or less text</p>
  </div>
  <a href="/" class="button">Click me</a>
</div>
.content {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

答案 7 :(得分:-1)

尝试一下

.content {
      display: flex;
      flex-direction: column;
      height: 250px;
      width: 200px;
      border: solid;
      word-wrap: break-word;
    }

   .content h1 , .content h2 {
     margin-bottom: 0px;
    }

   .content p {
     flex: 1;
    }
   <div class="content">
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>Some more or less text</p>
  <a href="/" class="button">Click me</a>
</div>

答案 8 :(得分:-3)

不确定flexbox但你可以使用position属性。

设置父div position: relative 和子元素可能是<p><h1>等。设置position: absolutebottom: 0

示例:

的index.html

<div class="parent">
  <p>Child</p>
</div>

的style.css

.parent {
  background: gray;
  width: 10%;
  height: 100px;    
  position: relative;
}
p {
  position: absolute;
  bottom: 0;
}

Code pen here.