GETah
GETah

Reputation: 21409

Horizontal list items in a navigation bar

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?

Upvotes: 2

Views: 218

Answers (2)

Pavlo
Pavlo

Reputation: 44889

An old, tried-and-tested way is to use table layout:

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

.nav li {
    display: table-cell;
}

A modern approach is to use flexbox (IE 10+, may require a lot of prefixes, since there are three versions of the standard):

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

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

Demo for both versions: http://jsfiddle.net/3qM5y/1/

Upvotes: 5

Travis J
Travis J

Reputation: 82267

jsFiddle Demo

demo with jQuery and a hover

There are a few issues to address. First, without specifically determining the width of each menu item, you must use infer the width. A common approach is to use the built in algorithm used for calculating table cells. To do this, you will need to set the container to display:table and the items to display:table-cell.

Also, the ul tends to have some default padding, you should probably remove the initial padding by using padding-left: 0px; on the container.

Last, make sure that your anchor elements are going to be properly sized. This means setting them with display: inline-block; and width:100%.

Upvotes: 2

Related Questions