Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Items Middle Vertically Aligned

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:

enter image description here

like image 946
Steve Avatar asked Jun 02 '11 19:06

Steve


People also ask

Is middle a vertical alignment?

To get them centered along a line, you'd use vertical-align: middle; . Although note that it centers the text according to its tallest ascender and deepest descender. Each element lines up according to the line you've set, which doesn't change from element to element.

How do you center an object vertically?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.


2 Answers

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.

like image 30
scurker Avatar answered Oct 29 '22 15:10

scurker


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/

like image 76
dragfyre Avatar answered Oct 29 '22 15:10

dragfyre