137

私は#headerdiv100% widthを持っており、その div 内には順序付けられていないリストがあります。順序なしリストに適用margin: 0 autoしましたが、ヘッダー div 内で中央に配置されません。

理由を教えてください。親divの幅を定義すれば、順序付けられていないリストはmargin: 0 auto. 私は何が欠けていますか?

これが私のコードです:

<style>

* {
    margin: 0;
    padding: 0;
}

#header {
    width: 100%;    
    background-color: #333;
    min-height: 160px;
    font-family:Arial, Helvetica, sans-serif;
}

#sitename {
    font-size: 50px;
    width: 620px;
    margin:0 auto;
    padding-top: 35px;
    color:#999;
}

#header ul {
    float: right;
    margin: 0 auto;
}

#header ul li {
    float: left;
    padding-right: 20px;
    list-style-type: none;
    font-size: 20px;
}

</style>

</head>

<body>

<div id="header">
    <h1 id="sitename">Photography Auction Site</h1>

    <ul>
        <li>List of Photos</li>
        <li>Image Gallery</li>
        <li>Contact Us</li>
    </ul>
</div>

</body>
</html>
4

5 に答える 5

146

親要素ではなく、中央に配置する要素の幅を定義する必要があります。

#header ul {
    margin: 0 auto;
    width: 90%;
}

編集:わかりました、私は今テストページを見てきました、そしてこれがあなたがそれを望む方法です:

#header ul {
    list-style:none;
    margin:0 auto;
    width:90%;
}

/* Remove the float: left; property, it interferes with display: inline and 
 * causes problems. (float: left; makes the element implicitly a block-level
 * element. It is still good to use display: inline on it to overcome a bug
 * in IE6 and below that doubles horizontal margins for floated elements)
 * The styles below is the full style for the list-items. 
 */
#header ul li {
    color:#CCCCCC;
    display:inline;
    font-size:20px;
    padding-right:20px;
}
于 2009-06-08T06:29:51.537 に答える
14

なぜだめですか?

#header {
    text-align: center;
}

#header ul {
    display: inline;
}
于 2010-08-24T05:41:46.543 に答える