无法居中导航栏

时间:2014-05-02 07:34:42

标签: html css navigation center

我无法将链接置于页面中间。我试过text-align:center;在我的样式表中几乎每个地方都无济于事。 http://www.ddofans.org/

这里是相关的代码条:

HTML:

<div id='nav'>
<ul>
<li class='active'><a href='index.html'><span>Home</span></a></li>
<li><a href='#'><span>Blog</span></a></li>
<li><a href='#'><span>News</span></a></li>
<li class='last'><a href='#'><span>Other</span></a></li>
</ul>
</div>

CSS:

#nav {
background: #035ea3;
width: auto;
text-align: middle;
}
#nav ul {

list-style: none;
margin: 0;
padding: 0;
line-height: 1;
display: block;
zoom: 1; 
}
#nav ul:after {
content: ' ';
display: block;
font-size: 0;
height: 0;
clear: both;
visibility: hidden;
}
#nav ul li {
float: left;
display: block;
padding: 0;  
}
#nav ul li a {
color: #f5f5f5;
text-decoration: none;  
display: block;
padding: 15px 25px;
font-family: 'Open Sans', sans-serif;
font-weight: 700;
text-transform: uppercase;
font-size: 14px;
position: relative;
-webkit-transition: color .25s;
-moz-transition: color .25s;
-ms-transition: color .25s;
-o-transition: color .25s;
transition: color .25s;
}
#nav ul li a:hover {
color: #000000;
}
#nav ul li a:hover:before {
width: 100%;
}
#nav ul li a:after {
content: '';
display: block;
position: absolute;
right: -3px;
top: 19px;
height: 6px;
width: 6px;
background: #f5f5f5;
opacity: .5;
}
#nav ul li a:before {
content: '';
display: block;
position: absolute;
left: 0;
bottom: 0;
height: 3px;
width: 0;
background: #000000;
-webkit-transition: width .25s;
-moz-transition: width .25s;
-ms-transition: width .25s;
-o-transition: width .25s;
transition: width .25s;
}
#nav ul li.last > a:after,
#nav ul li:last-child > a:after {
display: none;
}
#nav ul li.active a {
color: #000000;
}
#nav ul li.active a:before {
width: 100%;
}

4 个答案:

答案 0 :(得分:0)

display: inline-block;提交给#nav ul&amp; text-align:center#nav 您目前在text-align:middle使用#nav这是错误的。

答案 1 :(得分:0)

您需要将导航包装在容器中,然后通过指定边距使导航栏居中。

你的导航风格应该是:

#nav {
background: #035EA3;
width: 500px;
margin: 0 auto;
}

你的html看起来像这样:

<div style="width: 100%;background-color: #035EA3;">
<div id="nav">

<ul>
   <li class="active"><a href="index.html"><span>Home</span></a></li>
   <li><a href="#"><span>Blog</span></a></li>
   <li><a href="#"><span>News</span></a></li>
   <li class="last"><a href="#"><span>Other</span></a></li>
</ul>
</div>
</div>

答案 2 :(得分:0)

试试这个:

#nav {
   background: #035ea3;
   width: auto;
   text-align: center;
}

#nav ul {
   list-style: none;
   margin: 0;
   padding: 0;
   line-height: 1;
   display: inline-block;
   zoom: 1; 
 }

答案 3 :(得分:0)

Fiddle

text-align: middle;更改为#nav&amp;

text-align: center; {p> display: block;display: inline-block;的{​​{1}}