List Items Middle Vertically Aligned

假装没事ソ 提交于 2019-12-13 12:24:46

问题


I have the following HTML and CSS that creates a list, where the list items have a min-height set. I would like for the contents of the list items to be vertically aligned in the middle instead of the top if there is not enough content to fill the entire height. How the heck do I pull this off?

<html>
    <head>
        <style type="text/css">
            ul {
                width : 300px;
                list-style-type: none;
            }

            li {
                min-height : 45px;
                vertical-align : middle;
                border-bottom : 1px black solid;
            }
        </style>
    <head>
    <body>
        <ul>    
            <li>Testing testing 1234</li>
            <li>Testing testing 1234 Testing testing 1234</li>
            <li>Testing testing 1234 Testing testing 1234 Testing testing 1234</li>
            <li>Testing testing 1234 Testing testing 1234 Testing testing 1234 Testing testing 1234</li>
            <li>Testing testing 1234 Testing testing 1234 Testing testing 1234 Testing testing 1234 Testing testing 1234</li>
       </ul>
 </body>

Which gives me the following:


回答1:


If you add a <div> inside the <li> and make the following changes to the CSS, it seems to work:

li {
    min-height : 45px;
    border-bottom : 1px black solid;
}
li div {
    height:45px;
    display:table-cell;
    vertical-align : middle;
}

See it live here-- works in IE8 and FF4: http://jsfiddle.net/RrVBh/




回答2:


Ahh, good ol' vertical align. The easiest way is to set line-height: 45px; but this only works when you have one line of content and will force any additional lines to overflow.

Otherwise, you could use display:table-cell; vertical-align: middle; as demonstrated in the above link.



来源:https://stackoverflow.com/questions/6219253/list-items-middle-vertically-aligned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!