如何获得导航链接在页面上平均分布并居中?

时间:2018-12-10 01:20:00

标签: html css button html-lists nav

我是一个初学者,我正在为我的一个学校网络项目编写一个网站。我正在尝试设置标题的样式,使其看起来像这样:

https://i.stack.imgur.com/Wz58F.png

但是导航和“注册图书馆卡”按钮给了我一些问题。我想将按钮移到顶部的图标下面,但是按钮一直向左移动。我尝试了 float:right ,但这似乎不起作用。这是我的HTML和CSS。 谢谢!

</head>
<body>
<header>

<img class="logo clearfix" src="images/logo.png" alt="Charlotte County Library System Logo"/>
<img class="icons right clearfix" src="images/search.png" alt="Search Icon"/>
<img class="icons right clearfix" src="images/myaccount.png" alt="My Account Icon"/>
<button class="signup">Sign Up for a Library Card &gt;</button>
</header>

<div class="nav">
<nav>
<ul>
<li><a href="index.html">HOME</a></li>
<li><a href="index.html">CATALOG</a></li>
<li><a href="index.html">FEES &amp; FINES</a></li>
<li><a href="index.html">LOCATIONS &amp; HOURS</a></li>
<li><a href="index.html">SERVICES</a></li>
</ul>
</nav>

</div>

</body>
</html>

Css:

* {
-webkit-box-sizing: border-box; 
-moz-box-sizing: border-box; 
 box-sizing: border-box; 
 margin: 0;
 padding: 0;
}

.clearfix:after{
content:"";
display: table;
clear: both;
}

header{
width: 1200px;
margin: 0 auto;
margin-bottom: 40px;
}


.logo{
float: left;
width: 400px;
margin-top: 30px;
}

.icons{
width: 45px;
padding: 10px;
}


.left{
float:left;
}
.right{
float: right;
}

button {
background-color: #3399CC;
border: none;
border-radius: 4px;
color: #fff;
padding: 8px;
width: 250px;
text-align: center;
text-decoration: none;
font-size: 14px;
margin-top: 70px;
font-family:'Poppins', sans-serif;
font-weight: 500;
cursor: pointer;
}

nav{
background-color: #13DEA9;
width: 100%;
}
nav ul{
padding: 15px;


}

nav ul li{
font-family: 'Poppins', sans-serif;
font-weight: 600;
text-align: center;
list-style: none;
display: inline;


}

nav ul li a{
text-decoration: none;
margin: 70px;
color: white;


}

1 个答案:

答案 0 :(得分:1)

只需使用flexbox。这是您的新CSS

nav {
    background-color: #13DEA9;
    width: 100%;
    height: 60px;
}

nav ul {
    height: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
}

nav ul li {
    font-family: 'Poppins', sans-serif;
    font-weight: 600;
    text-align: center;
    list-style: none;
}

nav ul li a {
    text-decoration: none;
    color: white;
}

这是一个JS小提琴:https://jsfiddle.net/jot6L15b/

我还向nav添加了height属性,您可以将其设置为所需的任何值,并且该属性也将保持垂直居中。