簡體   English   中英

如何獲得導航鏈接在頁面上平均分布並居中?

[英]How do I get my nav links to spread evenly across and be centered in the page?

我是一個初學者,正在為我的一個學校網絡項目編寫一個網站。 我正在嘗試設置標題的樣式,使其看起來像這樣:

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

但是導航和“注冊借書證”按鈕給了我一些問題。 我想將按鈕移到頂部的圖標下面,但是按鈕一直向左移動。 我嘗試了float:對,但這似乎不起作用。 這是我的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;


}

只需使用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屬性,您可以將其設置為所需的任何值,並且它也將保持垂直居中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM