简体   繁体   中英

How to vertically align image middle in <li>?

Hell all: the below is the html code, I want to vertically align every image in li , no idea after googling a lot. I can use jquery to set it margin-top according to images' height, but it's inconvenient solution.

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB2312"/>
    <style type="text/css">
        .psdthumb2 ul {
            list-style: none;

        }
        li.qq2{
            height: 400px;
            width: 400px;
            background-color: red;
        }
    </style>
</head>

<body>
<div class="psdthumb2">
    <ul>
        <li>111111</li>
        <li class="qq2">
            <img src="http://mat1.qq.com/www/images/allskin/wmlogo.gif">
        </li>
    </ul>
    <ul>
        <li>222222</li>
        <li class="qq2">
            <img src="http://img11.360buyimg.com/n4/3445/522086b7-dc0a-432c-b027-7b2a80c79f29.jpg">
        </li>
    </ul>
</div>
</body>
</html>

thanks in advance.

li.qq2{
   display : table-cell;
   vertical-align : middle;
   text-align : center;
}

This should work.

Tested in Chrome, Safari, FF and IE 9.

Here's a working example:

CSS

.psdthumb2 ul 
{
    list-style: none;
}
li.qq2
{
    height: 400px;
    width: 400px;
    background-color: red;
    display: table-cell;
    vertical-align: middle;
}​

HTML

<div class="psdthumb2">
    <ul>
        <li>111111</li>
        <li class="qq2">
            <img src="http://mat1.qq.com/www/images/allskin/wmlogo.gif">
        </li>
    </ul>
    <ul>
        <li>222222</li>
        <li class="qq2">
            <img src="http://img11.360buyimg.com/n4/3445/522086b7-dc0a-432c-b027-7b2a80c79f29.jpg">
        </li>
    </ul>
</div>

Basically you just need to add display: table-cell; vertical-align: middle; display: table-cell; vertical-align: middle; to your css class.

Hope it helps!

.qq2 img{vertical-align:middle;}

应该管用。

see the demo with your updated code:- http://jsbin.com/ozixop/edit#javascript,html,live

you just update your css and add few properties for vertical middle as mentioned below:-

    li.qq2{
background-color: red;
display: table-cell;
height: 400px;
text-align: center;
vertical-align: middle;
width: 400px;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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