Center single- AND multi-line li text vertically

Raphael Müller picture Raphael Müller · Nov 4, 2010 · Viewed 25.9k times · Source

I have an unordered list with a background-image set. All list-items have the same height, the background-image is positioned left center.

The text of each item should be centered vertically to the li. This works well with single-line text (by setting the line-height according to the height of the li), but not with two lines of text.

I could add "line-height:normal" to the two-line item, but I want a solution that works for all items.

How can I do this?

Example:

li { 
    list-style-type:none; 
    padding-left:40px; 
    height:36px;
    line-height:36px; 
    background:url('tick.png') no-repeat 0 50%; 
}

Answer

fcalderan picture fcalderan · Nov 4, 2010

this is a most crossbrowser solution

    li {
    	width           : 200px;
        line-height     : 100px;
        height          : 100px;
    	border          : 1px blue solid;        
    }
    li span {
    	display				: -moz-inline-box;  /* FF2 or lower */
    	display				: inline-block;     /* FF3, Opera, Safari */
        line-height         : normal;
        vertical-align      : middle;    
    }
    
    li span		{ *display	: inline;} /* haslayout for IE6/7 */
<ul>
       <li><span>My text</span></li>
       <li><span>My longer text</span></li>
       <li><span>My text, but this time is really wide</span></li>
       <li><span>My text, some thoughts about how much it will expand in this item.</span></li>
    </ul>

I used star hack for brevity, you should avoid. Just use html5boilerplate solution, it uses conditional comments on body tag