vertical-align: bottom
不起作用由于父元素的高度大于标签的计算高度,使用 vertical-align: bottom
无法 将行内元素移动到父元素的底部。
因为在行内流中,vertical-align
根据其父元素的基线决定元素的位置;而在标签上使用该属性不会改变其父元素的基线位置。
内联级别元素(inline
、inline-block
)默认情况下坐落在其基线上。如果它们有不同的高度,则最高的元素将确定其他元素的位置。
即在行内流中,最高的元素将影响/移动父元素的基线:
因此,在父元素有明确的 height
属性的情况下,如果我们可以有一个内联子元素与父元素完全相同的高度(full-height child),它会影响内联流并向下移动基线:
vertical-align: bottom;
来 垂直对齐它们。
baseline
将框的基线与父框的基线对齐。如果框没有基线,则将底边缘与父框的基线对齐。
bottom
将对齐子树的底部与行框的底部对齐。
因此,您可以在父元素中创建一个全高元素(个人而言,我更喜欢使用伪元素)来将标签对齐到底部。
#contain-word-div:after {
content: "";
display: inline-block;
height: 100%; /* Let it be as height as the parent */
vertical-align: bottom; /* Align the element at the bottom */
}
#contain-word-lab {
display: inline-block;
vertical-align: bottom; /* Align the element at the bottom */
}
#contain-word-lab {
position:absolute;
bottom:5px;
}
将position:relative
应用于父div,并将您的标签设置为position:absolute
。
#contain-word-div {
height: 100px;
position:relative;
}
#contain-word-lab {
position:absolute;
bottom:5px;
}
vertical-align
在容器为表格/类似表格元素(如table
、tr
、td
、th
)或内联文本元素(如span
)时效果最佳。然而,因为使用 table
元素进行布局不是一个好主意,我们可以使用 display:table;
和 display:table-cell;
CSS 属性使其他元素像它们一样工作。
尝试应用以下 CSS:
#contain-word-div {
height: 100px;
border: 1px solid black; /* added just for visualising position */
display:table;
}
#contain-word-lab {
display:table-cell;
vertical-align: bottom;
padding-bottom: 5px; /* use padding to give the label more height rather than trying to push the "cell" upwards */
}
{{链接1:DEMO}}
table
列的显示类型上进行更改可能会导致问题。 - Hashem Qolamidiv
元素中。这是一个带有其他div
的示例,没有任何问题。http://www.bootply.com/gmpFwz4lIn - haxxxton#contain-word-lab {
vertical-align='bottom';/***Remove This***/
bottom:2px;
position:absolute;
}
<html>
元素建立的初始包含块相对定位。即在页面底部。 - Hashem Qolamivertical-align
只适用于表格单元格。如果您想将一个元素定位在其父元素的底部,您需要给它添加 position: absolute;
和 bottom: 0;
。
“bottom”无论如何都不会起作用 :) 尝试只使用bottom
,不要加上引号。
此外,您的标签也需要一个高度。现在它使用的是自动高度,正好是字体大小。我建议在您的标签中添加line-height: 100px;
。
" "
? - Kheema Pandey