集中navbar css / html

时间:2015-12-14 09:39:47

标签: html css navbar

如果你能帮助我解决这个小问题,我很感激(我想),我只是想让导航栏居中,我不知道该怎么做,因为我是这个领域的新手,谢谢再次引起你的注意,以及每个可以帮助我的人。

我想要导航栏的示例:

Navbar

<!-- ---- HTML ---- -->

<div id="menu">
<ul>
  <li><a href="#home">Início</a></li>
  <li><a href="#news">Blog</a></li>
  <li><a href="#contact">Relatar erros</a></li>
  <li><a href="#contact">Guia</a></li>
  <li><a href="#contact">Doar </a></li>
  <li><a href="#about">Sobre</a></li>
  <li><a href="#login">Contato</a></li>
  </ul>
</ul>
</div>

<!-- ---- CSS ---- -->

#menu {
    color:white;
    text-align:center;
    padding:5px;
    margin-right: -5px;
    margin-left: -5px;
    margin-top: -4px; }

ul {
    list-style-type: none;  margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #0F0F0F;
    margin-right: -8px;
    margin-left: -8px; }

li {
    float: left;

}

li:last-child {
    border-right: none; }

li a {
    display: block;     
    font: bold 14px Franklin Gothic Demi,Tahoma,Halvetica;
    color: red;
    border-bottom:3px solid #0c0c0c;
    border-style: solid;
    border-width: 0 0 3px 0;
    border-color: black;
    text-align: center;
    padding: 12px 12px;
    text-decoration: none; }

li a:hover:not(.active) {
    background-color: #000000;
    border-bottom:3px solid #ff0000;
    color: white; }

4 个答案:

答案 0 :(得分:1)

你可以这样做:

<强> CSS

ul {
    list-style-type: none;  margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #0F0F0F;
    margin-right: -8px;
    margin-left: -8px; 
    text-align: center;
    }

li {
    display: inline-block;

}

<强> DEMO HERE

答案 1 :(得分:0)

问题在于float: left;中的li

只需使用display: inline-block;即可。

&#13;
&#13;
#menu {
    color:white;
    text-align:center;
    padding:5px;
    margin-right: -5px;
    margin-left: -5px;
    margin-top: -4px; }

ul {
    list-style-type: none;  margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #0F0F0F;
    margin-right: -8px;
    margin-left: -8px; }

li {
    display: inline-block;

}

li:last-child {
    border-right: none; }

li a {
    display: block;     
    font: bold 14px Franklin Gothic Demi,Tahoma,Halvetica;
    color: red;
    border-bottom:3px solid #0c0c0c;
    border-style: solid;
    border-width: 0 0 3px 0;
    border-color: black;
    text-align: center;
    padding: 12px 12px;
    text-decoration: none; }

li a:hover:not(.active) {
    background-color: #000000;
    border-bottom:3px solid #ff0000;
    color: white; }
&#13;
<div id="menu">
<ul>
  <li><a href="#home">Início</a></li>
  <li><a href="#news">Blog</a></li>
  <li><a href="#contact">Relatar erros</a></li>
  <li><a href="#contact">Guia</a></li>
  <li><a href="#contact">Doar </a></li>
  <li><a href="#about">Sobre</a></li>
  <li><a href="#login">Contato</a></li>
</ul>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用display:inline-block;代替float:left而不要忘记display:inline-block;发生的remove extra spaces

为什么必须删除 float:left;

根据W3C

  

<强>浮筒

     

浮动是一个在当前向左或向右移动的框   线。

所以text-align:center;li没有任何影响。

&#13;
&#13;
#menu {
  color: white;
  text-align: center;
  padding: 5px;
  margin-right: -5px;
  margin-left: -5px;
  margin-top: -4px;
}
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #0F0F0F;
  margin-right: -8px;
  margin-left: -8px;
}
li {
  display: inline-block;
}
&#13;
<div id="menu">
  <ul>
    <li><a href="#home">Início</a>
    </li>
    <li><a href="#news">Blog</a>
    </li>
    <li><a href="#contact">Relatar erros</a>
    </li>
    <li><a href="#contact">Guia</a>
    </li>
    <li><a href="#contact">Doar </a>
    </li>
    <li><a href="#about">Sobre</a>
    </li>
    <li><a href="#login">Contato</a>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

试试这个:

 #menu {
    color:white;
    text-align:center;
    padding:5px;
    margin-right: -5px;
    margin-left: -5px;
    margin-top: -4px; }

ul {
    list-style-type: none;  
    overflow: hidden;
    background-color: #0F0F0F;
    margin-right: -8px;
    margin-left: -8px; 
    text-align: center;
    }

li {
    display: inline-block;

}

li:last-child {
    border-right: none; }

li a {
    display: block;     
    font: bold 14px Franklin Gothic Demi,Tahoma,Halvetica;
    color: red;
    border-bottom:3px solid #0c0c0c;
    border-style: solid;
    border-width: 0 0 3px 0;
    border-color: black;
    text-align: center;
    padding: 12px 12px;
    text-decoration: none; }

li a:hover:not(.active) {
    background-color: #000000;
    border-bottom:3px solid #ff0000;
    color: white; }

DEMO HERE