如何让我的导航链接在页面中均匀分布并居中?
How do I get my nav links to spread evenly across and be centered in the page?
我是初学者,正在为学校的一个网络项目编写网站。我正在尝试设置页眉样式,使其看起来像这样:
https://i.stack.imgur.com/Wz58F.png
但是导航和 "sign up for a library card" 按钮给我带来了一些问题。我想将按钮移到它位于顶部图标下方的位置,但按钮一直向左移动。我试过 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 ></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 & FINES</a></li>
<li><a href="index.html">LOCATIONS & 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 Fiddle: https://jsfiddle.net/jot6L15b/
另外,我在 nav
中添加了一个高度属性,您可以将其设置为任何您想要的值,它也将保持垂直居中。
我是初学者,正在为学校的一个网络项目编写网站。我正在尝试设置页眉样式,使其看起来像这样:
https://i.stack.imgur.com/Wz58F.png
但是导航和 "sign up for a library card" 按钮给我带来了一些问题。我想将按钮移到它位于顶部图标下方的位置,但按钮一直向左移动。我试过 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 ></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 & FINES</a></li>
<li><a href="index.html">LOCATIONS & 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 Fiddle: https://jsfiddle.net/jot6L15b/
另外,我在 nav
中添加了一个高度属性,您可以将其设置为任何您想要的值,它也将保持垂直居中。