在水平导航栏中居中左浮动元素?

时间:2014-03-27 23:34:34

标签: html css navbar

所以我有这个导航栏,你可以在这里看到 -

http://codepen.io/anon/pen/FJGvg/

我试图在导航栏中均匀分隔元素,但我无法让它在我的生活中发挥作用。我尝试了很多东西。这是代码:

<ul id="menu">
    <li><a>Test</a></li>
    <li><a>Test Different Length </a></li>
    <li><a>Test Again</a></li>
    <li><a>Test23</a></li>
</ul>

#menu {
    width: 50%;
    min-width: 700px;
    margin: auto;
    margin-top: 10px;
    padding: 10px 0 0 0;
    list-style: none;
    text-align: center;
    background-color: #111;
    background-image: linear-gradient(#444, #111);
    border-radius: 18px;
   /* box-shadow: 0 2px 1px #9c9c9c;*/
}

#menu li {
    display: inline;
    margin-left: auto;
    width: 25%;
    float: left;
    padding: 0 0 10px 0;
    position: relative;
}

#menu a {
    float: left;
    height: 25px;
    padding: 0 25px;
    color: #999;
    text-transform: uppercase;
    font: bold 12px/25px Arial, Helvetica;
    text-decoration: none;
    text-shadow: 0 1px 0 #000;
}

#menu li:hover > a {
    color: #fafafa;
}

/* Clear floated elements */
#menu:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
}

1 个答案:

答案 0 :(得分:1)

#menu {
    width: 50%;
    min-width: 700px;
    margin: 10px auto 0 auto;
    padding: 5px 0;
    list-style: none;
    text-align: center;
    background-color: #111;
    background-image: linear-gradient(#444, #111);
    border-radius: 18px;
   /* box-shadow: 0 2px 1px #9c9c9c;*/
}

#menu li {
    display: inline-block;
    width: 23%;   
    position: relative;
    padding:0;
}

#menu a {
    height: 25px;
    color: #999;
    text-transform: uppercase;
    font: bold 12px/25px Arial, Helvetica;
    text-decoration: none;
    text-shadow: 0 1px 0 #000;
}

#menu li:hover > a {
    color: #fafafa;
}

/* Clear floated elements */
#menu:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
}