日期:2014-05-17  浏览次数:20640 次

如何只修改CSS让select垂直居中
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <style type="text/css">
        div{
            line-height: 50px;
            border: 1px solid #333;
            height: 50px;
        }
    </style>
</head>
<body>
    <div>
        <select>
            <option>1234</option>
        </select>
    </div>
</body>
</html>



------解决方案--------------------
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <style type="text/css">
        div{
            line-height: 50px;
            border: 1px solid #333;
            height: 50px;
            position:relative;
        }
        select{
            position:absolute;
            top:30%;
        }
    </style>
</head>
<body>
    <div>
        <select>
            <option>1234</option>
        </select>
    </div>
</body>
</html>

------解决方案--------------------
4楼的答案是你想要的吗?
------解决方案--------------------
IE8/FF3.6通过
CSS code

        div
        {
            line-height: 50px;
            border: 1px solid #333;
            height: 50px;
            text-align: center;/*DIV里元素居中*/
        }
        select
        {
            position:relative;
            top:50%;/*该元素顶部与父元素顶距离,若要更佳垂直居中效果,需要考虑元素本身高度,如在此例30%更好。*/
        }