如何在div中对齐水平列表?

时间:2010-08-19 17:33:05

标签: css

我试图将我的横向< ul>居中在< div>内(我的例子中的黄色条纹)。标记在下面。我知道如果< li>然后我可以通过在< ul>上设置左右边距来实现它了。到“自动”,但我似乎没有找到摆脱“浮动”的方法,因为我需要我的< li>是块元素,以便我可以调整它们的大小。请帮忙! 谢谢 康斯坦丁


<html>
<head>
    <title></title>
    <style type="text/css">
        .container
        {
            background-color: yellow;
        }
        .container li
        {
            border: solid 1px grey;
            display: block;
            float: left;
            height: 100px;
            line-height: 100px;
            list-style-type: none;
            margin: 5px;
            text-align: center;
            width: 100px;
        }
    </style>
</head>
<body>
    <div class="container">
        <ul>
            <li><a href="#">x</a></li>
            <li><div>y</div></li>
        </ul>
        <div style="clear: both;">
        </div>
    </div>
</body>
</html>

<小时/> 代表OP发布演示:jsbin

3 个答案:

答案 0 :(得分:5)

是块级元素,因此占用容器的整个宽度... text-align用于对齐文本。你可以这样做:

.container ul{
    width:400px;
    margin:0px auto
}

答案 1 :(得分:1)

试试这个,适用于firefox和chrome

<html>
<head>
<title></title>
<style type="text/css">
    .container
    {
        background-color: yellow;
        text-align: center;
    }
    .container ul
    {
        display: inline-table;
        text-align: center;
    }
    .container li
    {
        border: solid 1px grey;
        display: block;
        float: left;
        height: 100px;
        line-height: 100px;
        list-style-type: none;
        margin: 5px;
        text-align: center;
        width: 100px;
    }
</style>
</head>
<body>
<div class="container">
    <ul>
        <li><a href="#">x</a></li>
        <li>
            <div>
                y</div>
        </li>
    </ul>
    <div style="clear: both;">
    </div>
</div>
</body>
</html>

答案 2 :(得分:0)

不确定如何回答你的问题,因为我甚至看不到FF 3.6.8中的黄色条纹

但看看这个http://www.cssplay.co.uk/boxes/ - 有很多选项,它可能会帮助你。

相关问题