问题居中固定导航栏
Trouble centering fixed navigation bar
基本上,我希望在我的网站上有一个固定的导航栏,它会在您向下滚动时随您移动。到目前为止一切都很好,但我真的很难将栏居中。
HTML
<header id="header">
<nav id="nav">
<ul>
<li><a href="#slide1">Front</a></li>
<li><a href="#slide2">Mastering</a></li>
<li><a href="#slide3">Samples</a></li>
<li><a href="#slide4">Contact</a></li>
</ul>
</nav>
</header>
CSS
#header {
z-index: 1;
width: 100%;
height: 80px;
position: fixed;
background-color:rgba(0,0,0,0.8);
display: block;
}
#nav {
z-index: 1;
width:100%;
margin-top: 20px;
display: block;
position: fixed;
text-align:center;
}
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 50%;
/* To help to identify the location */
height:50px;
background-color:green;
}
#nav li{
margin-top: 9px;
float: left;
padding-left: 21px;
display:block;
}
JSFiddle
https://jsfiddle.net/kjr1591d/1/
很想得到一些帮助。提前谢谢你:)
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 80%;
/* To help to identify the location */
height:50px;
background-color:green;
}
我想你在找这样的东西-
#header {
z-index: 1;
width: 100%;
height: 80px;
position: fixed;
background-color:rgba(0,0,0,0.8);
display: block;
}
#nav {
z-index: 1;
width:100%;
margin: 15px 0;
display: block;
position: fixed;
text-align:center;
}
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 50%;
/* To help to identify the location */
height:50px;
background-color:green;
text-align:center;
padding:0
}
#nav li{
margin:15px 0 0;
/*float: left;*/
padding:0 10px;
display:inline-block;
}
<header id="header">
<nav id="nav">
<ul>
<li><a href="#slide1">Front</a></li>
<li><a href="#slide2">Mastering</a></li>
<li><a href="#slide3">Samples</a></li>
<li><a href="#slide4">Contact</a></li>
</ul>
</nav>
</header>
将 ul
和 li
更改为显示为 inline-block
,并为 li
分配大小相等的左右边距。
基本上,我希望在我的网站上有一个固定的导航栏,它会在您向下滚动时随您移动。到目前为止一切都很好,但我真的很难将栏居中。
HTML
<header id="header">
<nav id="nav">
<ul>
<li><a href="#slide1">Front</a></li>
<li><a href="#slide2">Mastering</a></li>
<li><a href="#slide3">Samples</a></li>
<li><a href="#slide4">Contact</a></li>
</ul>
</nav>
</header>
CSS
#header {
z-index: 1;
width: 100%;
height: 80px;
position: fixed;
background-color:rgba(0,0,0,0.8);
display: block;
}
#nav {
z-index: 1;
width:100%;
margin-top: 20px;
display: block;
position: fixed;
text-align:center;
}
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 50%;
/* To help to identify the location */
height:50px;
background-color:green;
}
#nav li{
margin-top: 9px;
float: left;
padding-left: 21px;
display:block;
}
JSFiddle https://jsfiddle.net/kjr1591d/1/
很想得到一些帮助。提前谢谢你:)
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 80%;
/* To help to identify the location */
height:50px;
background-color:green;
}
我想你在找这样的东西-
#header {
z-index: 1;
width: 100%;
height: 80px;
position: fixed;
background-color:rgba(0,0,0,0.8);
display: block;
}
#nav {
z-index: 1;
width:100%;
margin: 15px 0;
display: block;
position: fixed;
text-align:center;
}
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 50%;
/* To help to identify the location */
height:50px;
background-color:green;
text-align:center;
padding:0
}
#nav li{
margin:15px 0 0;
/*float: left;*/
padding:0 10px;
display:inline-block;
}
<header id="header">
<nav id="nav">
<ul>
<li><a href="#slide1">Front</a></li>
<li><a href="#slide2">Mastering</a></li>
<li><a href="#slide3">Samples</a></li>
<li><a href="#slide4">Contact</a></li>
</ul>
</nav>
</header>
将 ul
和 li
更改为显示为 inline-block
,并为 li
分配大小相等的左右边距。