假设我有以下CSS和HTML代码:

#标题{ 身高:150 px; } < div id = "头" > <标题>标题< / h1 > 标题内容(一行或多行) < / div >

标题部分是固定的高度,但标题内容可能会改变。

我想要标题的内容垂直对齐到标题部分的底部,所以最后一行文本“粘”到标题部分的底部。

所以如果只有一行文本,它会像这样:

-----------------------------
| Header title
|
|
|
| header content (resulting in one line)
-----------------------------

如果有三行:

-----------------------------
| Header title
|
| header content (which is so
| much stuff that it perfectly
| spans over three lines)
-----------------------------

如何在CSS中做到这一点?


当前回答

我发现这个解决方案基于一个默认的引导启动模板

/* HTML */

<div class="content_wrapper">
  <div class="content_floating">
    <h2>HIS This is the header<br>
      In Two Rows</h2>
    <p>This is a description at the bottom too</p> 
  </div>
</div>

/* css */

.content_wrapper{
      display: table;
      width: 100%;
      height: 100%; /* For at least Firefox */
      min-height: 100%;
    }

.content_floating{
  display: table-cell;
  vertical-align: bottom;
  padding-bottom:80px;

}

其他回答

display: flex;
align-items: flex-end;

我遇到过这个问题几次,有很好的解决方案,但也有不太好的。因此,您可以使用flexbox、网格系统或显示表以不同的方式实现这一点。我更喜欢flex和margin-bottom: auto的组合。以下是我个人收集的文本底部可能性:

1. Flex / margin-top: auto;

.parent { 最小高度:200 px; 背景:绿色; 显示:flex; } .child { margin-top:汽车; 背景:红色; 填充:5 px; 颜色:白色; } < div class = "父" > <div class="child">底部文本</div> < / div >

2. Flex / align-self: Flex -end

.parent { 显示:flex; 最小高度:200 px; 背景:绿色; } .child { align-self: flex-end; 背景:红色; 填充:5 px; 颜色:白色; } < div class = "父" > <div class="child">底部文本</div> < / div >

3.Flex / align-items: Flex -end;

.parent { 最小高度:200 px; 背景:绿色; 显示:flex; 对齐项目:flex-end; } .child { 填充:5 px; 背景:红色; 颜色:白色; } < div class = "父" > <div class="child">底部文本</div> < / div >

4. Grid / align-self:结束;

.parent { 最小高度:200 px; 背景:绿色; 显示:网格; } .child { align-self:结束; 背景:红色; 填充:5 px; 颜色:白色; } < div class = "父" > <div class="child">底部文本</div> < / div >

5. 表/垂直对齐:底部;

就个人而言,我不喜欢这种处理表格的方法。

.parent { 最小高度:200 px; 背景:绿色; 显示:表; 宽度:100%; } .child { 显示:表格单元; vertical-align:底部; 背景:红色; 填充:5 px; 颜色:白色; } < div class = "父" > <div class="child">底部文本</div> < / div >

用垫片

6. Flex;/ flex: 1;

.parent { 最小高度:200 px; 背景:绿色; 显示:flex; flex-flow:列; } .spacer { flex: 1; } .child { 填充:5 px; 背景:红色; 颜色:白色; } < div class = "父" > < div class = "隔离" > < / div > <div class="child">底部文本</div> < / div >

7. Flex / Flex -grow: 1;

.parent { 最小高度:200 px; 背景:绿色; 显示:flex; flex-direction:列; } .spacer { flex-grow: 1; } .child { 填充:5 px; 背景:红色; 颜色:白色; } < div class = "父" > < div class = "隔离" > < / div > <div class="child">底部文本</div> < / div >

8. Inline-block / PseudoClass::before

.parent { 最小高度:200 px; 背景:绿色; } {前.child:: 显示:inline-block; 内容:”; 高度:100%; vertical-align:底部; } .child { 身高:200 px; 填充:5 px; 背景:红色; 颜色:白色; } < div class = "父" > <div class="child">底部文本</div> < / div >

❤️我个人比较喜欢的版本是:, 2. 和3。

对前面提到的其他flex-box解决方案的补充:

你可以在第一个div上使用flex-grow: 1 .这样,你的第二个div将与底部对齐,而第一个div将覆盖所有剩余空间。

在父div上,必须使用display: flex和flex-direction: column。

/* parent-wrapper div */
.container {
  display: flex;
  flex-direction: column;
}

/* first-upper div */
.main {
  flex-grow: 1;
}

检查提琴:https://jsfiddle.net/1yj3ve05/

试一试:

div.myclass { margin-top: 100%; }

尝试更改%来修复它。例如:120%或90%等等。

使用CSS定位:

/* Creates a new stacking context on the header */
#header {
  position: relative;
}

/* Positions header-content at the bottom of header's context */
#header-content {
  position: absolute;
  bottom: 0;
}

正如cletus所指出的,您需要确定头内容来完成这项工作。

<span id="header-content">some header content</span>

<div style="height:100%; position:relative;">
    <div style="height:10%; position:absolute; bottom:0px;">bottom</div>
</div>