简体   繁体   English

DIV内部垂直对齐底部UL

[英]Vertical align bottom UL inside DIV

I have a div with height:40px; 我有一个height:40px;div height:40px; and an ul with line-height:28px; 和一个line-height:28px;ul line-height:28px; . I would like to place the ul to the bottom, inside the div. 我想将ul放在div的底部。 I tried vertical-align:bottom; 我尝试了vertical-align:bottom; , but it doesn't help. ,但无济于事。

My other idea is the top margin, but if it's possible with vertical-align, I'll choose that. 我的另一个想法是上边距,但是如果可以使用垂直对齐,我会选择它。

My demo: http://jsfiddle.net/YpEd7/ 我的演示: http : //jsfiddle.net/YpEd7/

Add line height to the container. 将行高添加到容器。 Once you have line-height of 40px on the container, the vertical align bottom will align it to the bottom since your container is also 40px tall. 一旦容器上的行高为40px,由于容器的高度也为40px,因此垂直对齐底部会将其与底部对齐。 It wasn't working before since the line-height of the container was less than 40px so the ul did align to the bottom of the default line-height 由于容器的行高小于40px,因此ul一直与默认行高的底部对齐,因此之前没有任何作用

http://jsfiddle.net/YpEd7/2/ http://jsfiddle.net/YpEd7/2/

#container {
    background:gray;
    height:40px;
    line-height: 40px;
}

The same i posted in comment above^ 我在上面的评论中张贴的相同^

#container {
    background:gray;
    height:40px;
    position: relative;
}
ul {
    margin:0;
    padding:0;
    list-style:none;
    position: absolute;
    bottom: 0px;
}

http://jsfiddle.net/25VV5/2/ http://jsfiddle.net/25VV5/2/

You need to mix vertical-align to line-height in order to have this happen: DEMO 您需要将vertical-align与line-height混合才能发生这种情况: DEMO

#container {
    background:gray;
    height:40px;
    line-height:40px;/* equal to height of container */
}

ul {
    margin:0;
    padding:0;
    list-style:none;
    vertical-align:bottom;/* fine since i'm inline-block */
    display:inline-block;
    line-height:1em;
}

ul li {
    line-height:28px;
    background:red;
    color:#fff;
    margin:0 20px 0 0;
    float:left;
}

Please stop perpetuating the line-height hack for vertical alignment. 请停止永久保留行高标记以进行垂直对齐。 Vertical alignment only works when there is a sibling to reference. 垂直对齐仅在有同级参考时有效。

If you only have one element that you want vertically aligned, you must first wrap it in an element with a defined height (can be %). 如果只有一个要垂直对齐的元素,则必须首先将其包装在具有已定义高度(可以为%)的元素中。 Then you place the following class onto the container element. 然后,将以下类放置到容器元素上。

CSS CSS

.VAlignHelper:after {
    content:'';
    font-size:0;
    height:100%;
    vertical-align:middle;
    display:inline-block;
}

HTML HTML

<div id="container" class="VAlignHelper">
    <ul>
        <li>Menu 1</li>
        <li>Menu 2</li>
        <li>Menu 3</li>
    </ul>
</div>

Demo 演示

http://jsfiddle.net/jmdrury/YpEd7/3/ http://jsfiddle.net/jmdrury/YpEd7/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM