假设我有以下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中做到这一点?


当前回答

如果你有多个动态高度项,使用CSS显示table和table-cell的值:

HTML

<html>
<body>

  <div class="valign bottom">
    <div>

      <div>my bottom aligned div 1</div>
      <div>my bottom aligned div 2</div>
      <div>my bottom aligned div 3</div>

    </div>
  </div>

</body>
</html>

CSS

html,
body {
  width: 100%;
  height: 100%;
}
.valign {
  display: table;
  width: 100%;
  height: 100%;
}
.valign > div {
  display: table-cell;
  width: 100%;
  height: 100%;
}
.valign.bottom > div {
  vertical-align: bottom;
}

我在这里创建了一个JSBin演示:http://jsbin.com/INOnAkuF/2/edit

演示也有一个例子如何垂直中心对齐使用相同的技术。

其他回答

我遇到过这个问题几次,有很好的解决方案,但也有不太好的。因此,您可以使用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。

* { 保证金:0; } div { 宽度:300 px; 背景:cornflowerblue; 颜色:# fff; 身高:150 px; 显示:flex; justify-content:之间的空间; flex-direction:列; } < div > 标题< h4 > < / h4 > <p>这是一个段落</p> <!自16世纪以来,Ipsum一直是行业标准的假文本,当时一个不知名的打印机拿了一大堆字,把它打乱了 < / div >

只需简单地使用display:flex和flex-direction:column使子div按垂直顺序同步,然后应用justify-content:space-between来调整父div的高度及其子内容。这样你才能实现你的目标。尝试这个代码片段来解决这个问题。

非常感谢你的兴趣。

如果你可以设置内容的包装div的高度(#header-content,如other's reply所示),而不是整个#header,也许你也可以尝试这种方法:

HTML

<div id="header">
    <h1>some title</h1>
    <div id="header-content">
        <span>
            first line of header text<br>
            second line of header text<br>
            third, last line of header text
        </span>
    </div>
</div>

CSS

#header-content{
    height:100px;
}

#header-content::before{
  display:inline-block;
  content:'';
  height:100%;
  vertical-align:bottom;
}

#header-content span{
    display:inline-block;
}

在代码依赖上显示

将div移到底部的最佳解决方案如下所示。 基本上,你需要做的是将display flex和flex-direction设置为父类的列,并将'margin-top: auto'添加到它的子类,该子类需要被浮动到容器的底部 注意:我已经使用了bootstrap及其类。

.box-wrapper { 身高:400 px; 边框:1px实心#000; 保证金:20 px; 显示:flex;//仅用于表示。引导默认类已经添加 flex-direction:列; } .link-02 { margin-top:汽车; } <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet" /> <div class="box-wrapper d-flex flex-column col-4"> <div>意外blanditiis debitis</div> < div class = " news-box”> <img class="d-block" alt="non ipsam nihil" src="https://via.placeholder.com/150"> <p>连续痛苦的分娩和面对的分娩</p> < / div > <a href="https://oscar.com" target="_blank" class="link-02"> 这是移动到底部与最小的努力 < / > < / div >

这是一种灵活的方法。当然,IE8不支持,7年前用户就需要了。根据您需要支持的内容,其中一些可以取消。

不过,如果有一种方法来做到这一点,没有一个外部容器,这将是很好的,只是让文本在它自己的自我对齐。

#header {
    -webkit-box-align: end;
    -webkit-align-items: flex-end;
    -ms-flex-align: end;
    align-items: flex-end;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    height: 150px;
}