Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

css - How to center text inside a li element inside an unordered list

This list is working great for me but the text within the <li> elements is not centering.

The <li>s must auto resize to their content.

#nav-menu {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    height: 30px;
    background-image: url(../img/menu_bg.png);
    background-repeat: repeat-x;
    border-bottom: dotted thin #666666;
    border-top: dotted thin #666666;
    text-align: center;
    width: 800px;
}

#nav-menu ul {
    list-style: none;
    padding: 0;
    margin: auto 0;
}

#nav-menu li {
    float: left;
    border-right: dotted thin #666666;
    list-style: none;
    padding: 0.5em 2em 0.5em 0.75em;
}

#nav-menu li a {
    line-height: 1.5em;
    color: #333333;
    text-decoration: none;
    font-size: 12px;
    font-weight: bold;
    display: block;
}
<div id="nav-menu">
    <ul>
        <li class="current_page_item"><a href="#" title="Home">Home</a>
        <li class="current_page_item"><a href="#" title="Home">Home</a>
        <li class="current_page_item"><a href="#" title="Home">Home</a>
        <li class="current_page_item"><a href="#" title="Home">zxczczxczHome</a>
    </ul>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While you're assigning unequal padding values to the left and right of the li (0.75em and 2em respectively) the text can't be centred since you're forcing it off-centre with the padding.

If you amend the padding to: padding: 0.5em 1em; (0.5em top and bottom, 1em left and right) then it can be centred.

#nav-menu {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    height: 30px;
    background-image: url(../img/menu_bg.png);
    background-repeat: repeat-x;
    border-bottom: dotted thin #666666;
    border-top: dotted thin #666666;
    text-align: center;
    width: 800px;
}

#nav-menu ul {
    list-style: none;
    padding: 0;
    margin: auto 0;
}

#nav-menu li {
    float: left;
    border-right: dotted thin #666666;
    list-style: none;
    padding: 0.5em 1em;
}

#nav-menu li a {
    line-height: 1.5em;
    color: #333333;
    text-decoration: none;
    font-size: 12px;
    font-weight: bold;
    display: block;
}
<div id="nav-menu">
    <ul>
        <li class="current_page_item"><a href="#" title="Home">Home</a></li>
        <li class="current_page_item"><a href="#" title="Home">Home</a></li>
        <li class="current_page_item"><a href="#" title="Home">Home</a></li>
        <li class="current_page_item"><a href="#" title="Home">zxczczxczHome</a></li>
    </ul>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...