假设我有以下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
演示也有一个例子如何垂直中心对齐使用相同的技术。