垂直对齐:底部不工作

Ada*_*kis 30 html css vertical-alignment

我认为vertical-align应该与内联元素一起使用.但由于某些原因,灰色div中的所有内容都与顶部对齐,而不是与底部对齐.

<div style="position:absolute; top:130px; right: 80px; width: 230px; background-color:Gray; height:30px;" class="defaultText" id="pager">
    <span style="vertical-align:bottom;">Page Size:</span>
    <select style="vertical-align:bottom; font-size:8pt; margin-top: 0; margin-left:3px; height:16px; text-align:center;">
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="200">200</option>
        <option value="500">500</option>
        <option value="10000">*</option>
    </select>
    <div style="float:right;">
        <span style="vertical-align:bottom; color:Blue; cursor: pointer; margin-right: 10px;"><</span>
        <input style="vertical-align:bottom; height:12px; font-size:8pt; width: 20px;" type="text" data-bind="value: pageNum" />
        <span style="vertical-align:bottom;"> of </span>
        <span style="vertical-align:bottom;" data-bind="text: numPages"></span>
        <span style="vertical-align:bottom; color:Blue; cursor: pointer; margin-left: 5px;">></span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Tam*_*ake 16

除非您正在处理表格单元格,否则vertical-align会将元素与相邻元素(尤其是文本)对齐.因此,在灰色的div元素应该与一字排开彼此的div,而不是底部.请参阅http://phrogz.net/css/vertical-align/index.html上的示例.


小智 14

下面是一个示例,您可以使用以下代码完成此操作

演示: http ://jsfiddle.net/SbNKa/1/

#theContainer {
    height: 100px;
    width: 500px;
    position: relative;
    border: 1px solid #900;
}
.content-bottom {
    position: absolute;
    width: 498px;
    bottom: 0; /*This is the part that glues it to the bottom*/
    border: 1px solid #000;
}
<div id="theContainer">
    <div class="content-bottom">Content</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @DotNetWise:这是该家伙想要完成的答案,并非所有问题都是被问到的实际问题.希望有助于澄清事情. (4认同)

小智 11

这是使用Flex盒子的现代更新答案.

div {
  height: 100%; // containing div must fill the full height of the parent div
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
}
Run Code Online (Sandbox Code Playgroud)


Sir*_*lam 5

裂缝就在这里。我正在寻找答案(vertical-align)而不是替代答案(bottom: 0)。所以这就是解决方案。

vertical-align是相对于其容器而不是父(或wrapper)元素设置的。因此,只需给它一些行高,然后应用 vertical-align: bottom.

    div {
        background:yellow;
        margin:10px;
        line-height:100px;
    }
    div > * {
        vertical-align: bottom;
        line-height: normal;
    }

    a {
        background-color:#FFF;
        height:20px;
        display:inline-block;
        border:solid black 1px;
        padding:5px;
    }

    span {
        background:red;
        width: 50px;
    }
Run Code Online (Sandbox Code Playgroud)
    <div>
    <a>Some link</a>
    <span>Some text </span>
    </div>
Run Code Online (Sandbox Code Playgroud)