使用flexbox将元素与底部对齐

sup*_*ize 316 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>
Run Code Online (Sandbox Code Playgroud)

我想要以下布局:

 -------------------
|heading 1          |
|heading 2          | 
|paragraph text     |
|can have many      |
|rows               |
|                   |
|                   |
|                   | 
|link-button        |
 -------------------
Run Code Online (Sandbox Code Playgroud)

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

Ori*_*iol 545

您可以使用自动边距

在通过justify-content和进行对齐之前align-self,任何正的自由空间都会分配到该维度中的自动边距.

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

p { margin-bottom: auto; } /* Push following elements to the bottom */
a { margin-top: auto; } /* Push it and following elements to the bottom */
Run Code Online (Sandbox Code Playgroud)

.content {
  height: 200px;
  border: 1px solid;
  display: flex;
  flex-direction: column;
}
h1, h2 {
  margin: 0;
}
a {
  margin-top: auto;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

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

p { flex-grow: 1; } /* Grow to fill available space */
Run Code Online (Sandbox Code Playgroud)

.content {
  height: 200px;
  border: 1px solid;
  display: flex;
  flex-direction: column;
}
h1, h2 {
  margin: 0;
}
p {
  flex-grow: 1;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • flex-grow为我工作.这就是我做的方式`html {height:100%; } body {min-height:100%; 显示:flex; 弯曲方向:柱; } #site-content {flex:1; }` (11认同)
  • 对我来说,这适用于 Chrome,但不适用于 iOS 10.2 上的 Safari。有人可以确认吗? (2认同)
  • 遗憾的是,这在ie或edge中不起作用.:( (2认同)
  • 还要注意一点,请记住要在其父元素上使用“ margin-top:auto;”将其高度推至底部的“ height:100%”。 (2认同)
  • “任何正的可用空间都会分配到该维度中的自动边距。” 像这样的小事情是如此优雅,对我来说这个解决方案是基于你的答案的两行代码。谢谢 :) (2认同)

Rou*_*rge 127

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

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

.container {
  display: flex;
}

.button {
  align-self: flex-end;
}
Run Code Online (Sandbox Code Playgroud)

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

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

检查这个JSFiddle

  • 如果你想在底部,请确保将flex-direction设置为column. (4认同)
  • 如果我不能提供高度怎么办? (3认同)

Tim*_*imo 73

解决方案align-self: flex-end;对我来说不起作用,但是如果你想使用flex,这个做了:

CSS

 -------------------
|heading 1          |
|heading 2          | 
|paragraph text     |
|                   |
|                   |
|                   | 
|link button        |
 -------------------
Run Code Online (Sandbox Code Playgroud)

HTML

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

.content .upper {
  justify-content: normal;
}

/* Just to show container boundaries */
.content .upper, .content .bottom, .content .upper > * {
  border: 1px solid #ccc;
}
Run Code Online (Sandbox Code Playgroud)

结果是:

<div class="content">
  <div class="upper">
	  <h1>heading 1</h1>
	  <h2>heading 2</h2>
	  <p>paragraph text</p>
  </div>

  <div class="bottom">
	  <a href="/" class="button">link button</a>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • content 是列容器,它容纳 2 个其他容器,带有 justify-content: space- Between ;您告诉 Flexbox 将容器彼此推开尽可能多的距离(在这种情况下受内容容器的高度限制) (2认同)

fer*_*rit 52

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

2. 为你想留在底部的元素设置样式: style="margin-top: auto;"

3. 完成!哇。那很简单。

例子:

在此处输入图片说明

<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>
Run Code Online (Sandbox Code Playgroud)

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


Sta*_*ler 14

如果可以修改您的 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>
Run Code Online (Sandbox Code Playgroud)
.content {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
Run Code Online (Sandbox Code Playgroud)


Tom*_*lie 8

将 display 设置为flex 时,您可以简单地使用该flex属性来标记哪些内容可以增长,哪些内容不能。

弹性属性

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

div.up {
  flex: 1;
}

div.down {
  flex: none;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 给自己注意:记住设置容器的高度。我很挣扎,在我的场景中我所缺少的就是设置高度:100% (2认同)

San*_*Shr 5

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

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

例子:

索引.html

<div class="parent">
  <p>Child</p>
</div>
Run Code Online (Sandbox Code Playgroud)

样式文件

.parent {
  background: gray;
  width: 10%;
  height: 100px;    
  position: relative;
}
p {
  position: absolute;
  bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)

代码笔在这里。