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


当前回答

2015的解决方案

<div style='width:200px; height:60px; border:1px solid red;'>

    <table width=100% height=100% cellspacing=0 cellpadding=0 border=0>
        <tr><td valign=bottom>{$This_text_at_bottom}</td></tr>
    </table>

</div>

http://codepen.io/anon/pen/qERMdx

你的欢迎

其他回答

似乎起作用了:

#content {
    /* or just insert a number with "px" if you're fighting CSS without lesscss.org :) */
    vertical-align: -@header_height + @content_height;

    /* only need it if your content is <div>,
     * if it is inline (e.g., <a>) will work without it */
    display: inline-block;
}

使用较少使解决CSS谜题更像编码而不是像…我就是喜欢CSS。这是一个真正的乐趣,当你可以改变整个布局(而不破坏它:)只是改变一个参数。

* { 保证金: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的高度及其子内容。这样你才能实现你的目标。尝试这个代码片段来解决这个问题。

非常感谢你的兴趣。

这是一种灵活的方法。当然,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;
}

试一试:

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

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

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

/* 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;

}