2

I am trying to build a navigation bar like the Github's one (without using the Bootstrap nav* selectors - please see this jsfiddle enter image description here

So I have the following HTML:

<div class="container" style="margin-top: 20px;">
    <div class="span9">
        <ul class="unstyled main-tabs">
            <li>
                <a href="#">Link1</a>
            </li>
            <li>
                <a href="#">Link2</a>
            </li>
            <li>
                <a href="#">Link3</a>
            </li>
        </ul>
    </div>
</div>

Note the span9 class that I am using to force the navigation bar to occupy only 9/12 of my screen.

And the following CSS:

.main-tabs {
    position: relative;
    margin-bottom: 20px;
    font-size: 12px;
    font-weight: bold;
    background-color: #eaeaea;
    background-image: -moz-linear-gradient(#fafafa, #eaeaea);
    background-image: -webkit-linear-gradient(#fafafa, #eaeaea);
    background-image: linear-gradient(#fafafa, #eaeaea);
    background-repeat: repeat-x;
    border: 1px solid #eaeaea;
    border-bottom-color: #cacaca;
    border-radius: 3px;
}

.main-tabs a {
    display: block;
    text-align: center;
    line-height: 35px;
    font-size: 12px;
    color: #777;
    text-decoration: none;
    text-shadow: 0 1px 0 white;
    border-right: 1px solid #eee;
    border-right-color: rgba(0,0,0,0.04);
    border-left: 1px solid #fcfcfc;
    border-left-color: rgba(255,255,255,0.7);
    border-bottom: 2px solid #DADADA;
}

.main-tabs li {
    list-style-type: none;
}

.main-tabs li .active {
    border-bottom: 2px solid greenyellow;
}

The result is: enter image description here

I changed .main-tabs li to:

 .main-tabs li {
        list-style-type: none;
        float: left;
    }

Then

.main-tabs li {
    list-style-type: none;
    display: block;
}

This still gives the same results :(

But this did not really help. enter image description here

QUESTION Without hardcoding the width of li or ul elements (for responsiveness matters), is there a way to get the same navigation bar like the one on Github?

4

2 回答 2

5

一种古老的、久经考验的方法是使用表格布局:

.nav {
    display: table;
    width: 100%;
    margin: 0;
    padding: 0;
}

.nav li {
    display: table-cell;
}

一种现代方法是使用 flexbox(IE 10+,可能需要很多前缀,因为标准有三个版本):

.nav-alt {
    display: flex;
    margin: 0;
    padding: 0;
}

.nav-alt li {
    flex: auto;
    list-style-type: none;
}

两个版本的演示:http: //jsfiddle.net/3qM5y/1/

于 2013-09-23T19:50:19.390 回答
2

jsFiddle Demo

demo with jQuery and a hover

有几个问题需要解决。首先,在没有具体确定每个菜单项的宽度的情况下,您必须使用推断宽度。一种常见的方法是使用用于计算表格单元格的内置算法。为此,您需要将容器设置为display:table并将项目设置为display:table-cell

此外,它们ul往往有一些默认填充,您可能应该通过padding-left: 0px;在容器上使用来删除初始填充。

最后,确保您的锚元素尺寸合适。这意味着用display: inline-block;和设置它们width:100%

于 2013-09-23T20:10:20.977 回答