底部垂直对齐的 HTML 表格和一个向右浮动的元素

标签 html css css-float html-table vertical-alignment

我知道这个问题已经被问死了,但是我找不到类似的案例。我有一个 HTML 表格,“th”行的文本通过 vertical-align: bottom 沿底部对齐。现在,其中一个标题还包含一个按钮,我想将其 float 到右侧和底部。所以当然,我将该按钮包装在带有“float:right”的“span”中。但是该列中标题的文本不再触及容器的底部。

这里的例子: http://jsfiddle.net/accelerate/3sqyw/

HTML:

<table>
    <th>Col 1</th>
    <th>Col 2<span class="right-float"><button>Button</button></span></th>
    <th>Col 3</th>
</table>

CSS:

.right-float {
    display: inline-block;
    float: right;
}

table, th {
    border: 1px solid #aaaaaa;
}

table th {
    text-align: left;
    vertical-align: bottom;
    width: 120px;
    height: 40px;
    padding: 0 10px;
}

如果我删除“float: right”,“Col 2”将正确对齐到底部,但当然,按钮不再右对齐。那么我需要做什么来确保 Col 2 沿着底部与其余列对齐?

最佳答案

使用定位而不是 float 。

.right-float {
    position:absolute;
    bottom:0;
    right:0;
}

并在表头上设置相对定位( position:relative; 上的 <th> )。

jsFiddle example

关于底部垂直对齐的 HTML 表格和一个向右浮动的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20533848/

相关文章:

html - 中心分区;效果不佳

JavaScript Canvas 下载

css - 你如何解决 IE 不支持 :after?

html - 使用 float : right 后列表的最后一个元素被下推

html - 需要版权才能留在底部

html - 包含内容的堆栈菜单(大屏幕上的内容位于右侧)

HTML/CSS - 横向和纵​​向选项

css-float - 为什么 `inline-block` 元素会自动清除他们的 child ?

css - Wordpress - 无法更新 ie7 和 8 的样式表

css - 页面溢出到正文右侧