HTML——Two ways to center the navigation bar of ul and li

Summarized the production method of the lower navigation bar: one is to use float design, set the height and width in advance, and then set the elements to be displayed as float::left to display in sequence.


First, the float method

interface html

<div class="topbar-container">
    <div class="topbar-wrap">
        <div class="logo">
            <a href="#"> <img src="img/mi.png" alt="mi"></a>
        </div>
        <div class="nav">
            <ul>
                <li><a href="#">小米商场</a></li>
                <li><a href="#">MIUI</a></li>
                <li><a href="#">米聊</a></li>
                <li><a href="#">游戏</a></li>
                <li><a href="#">多看阅读</a></li>
                <li><a href="#">云服务</a></li>
                <li><a href="#">金融</a></li>
                <li><a href="#">小米网移动版</a></li>
            </ul>
        </div>
    </div>
</div>


css code

.topbar-container{
    background-color: #222222;
}
.topbar-wrap{
    width:1200px;
    height:60px;
    margin:0 auto;
    overflow: hidden;
}
.logo{
    height:50px;
    width: 50px;
    float: left;
    margin:5px 5px;

}
.nav{
    float: left;
    height: 60px;;
}
.nav li{
    float: left;
    width: 100px;
    margin:0 10px;
    list-style: none;
}
.nav a{
    text-decoration: none;
    height: 100%;
    width: 100%;
    display: block;
    font-size: 18px;
    color: #dadada;
    text-align: center;
    line-height: 60px;

}
.nav a:hover{
    background: #333;
}


2. Inline-block is centered

The img on the left is set to a'bsolute so that the img does not occupy a position, so that the nav on the right is centered

html code

<div class="topbar-container2">
    <div class="topbar-wrap2">
        <div class="logo2">
            <a href="#"> <img src="img/mi.png" alt="mi"></a>
        </div>
        <div class="nav2">
            <ul>
                <li><a href="#">小米商场</a></li>
                <li><a href="#">MIUI</a></li>
                <li><a href="#">米聊</a></li>
                <li><a href="#">游戏</a></li>
                <li><a href="#">多看阅读</a></li>
                <li><a href="#">云服务</a></li>
                <li><a href="#">金融</a></li>
                <li><a href="#">小米网移动版</a></li>
            </ul>
        </div>
    </div>

</div>


css code

.topbar-container2{
    background-color: #222222;
}
.topbar-wrap2{
    height: 60px;
    width: 1200px;
    margin:0 auto;
    position: relative;
}
.logo2{
    position: absolute;
    height:50px;
    width: 50px;
    margin:5px 5px;
    left:-0px;
    top:0;


}
.nav2{
    height: 60px;
    width:1200px;

}
.nav2 ul{
    text-align: center;
    height:100%;
    width: 100%;
    list-style: none;
}
.nav2 li{
    display:inline-block;
}
.nav2 ul li  a{
    text-decoration: none;
    font-size: 18px;
    color: #dadada;
    text-align: center;
    line-height: 60px;
    margin:0 20px;
}


The point is that ul sets text-align: center, so that it will be centered




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324101563&siteId=291194637