【问题标题】:Having trouble centering my navigation bar无法将我的导航栏居中
【发布时间】:2014-06-18 20:07:40
【问题描述】:

我的链接无法在页面中间居中。我试过 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%;
}

【问题讨论】:

  • text-align:middle 应该是 text-align:center 而不是浮动你的 li,而是让它们 display:inline-block

标签: html css navigation center


【解决方案1】:

试试这个:

#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; 
 }

【讨论】:

    【解决方案2】:

    display: inline-block;#nav ultext-align:center#nav 您当前在#nav 中使用text-align:middle,这是错误的。

    【讨论】:

      【解决方案3】:

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

      你的导航风格应该是:

      #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>
      

      【讨论】:

        【解决方案4】:

        Fiddle

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

        display: block;display: inline-block;#nav ul

        【讨论】:

          猜你喜欢
          • 2015-04-26
          • 1970-01-01
          • 2013-11-27
          • 2019-10-13
          • 1970-01-01
          • 2012-11-08
          • 1970-01-01
          • 2012-02-27
          • 1970-01-01
          相关资源
          最近更新 更多