Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full width horizontal nav bar with evenly spaced items

Starting point:

starting point

End point:

end point

I'm trying to have a horizontal navigation bar that fills 100% of it's container. In the first example, you'll see all of the items aligned to the left. I'm trying to get it to fill the full width of the container as shown in the second example. I want the spacing between all of the items to be uniform (unlike the way it's shown, I just put that together quickly to give you an idea of what I'm trying to do). I need the text to not be an image and the container it goes in is fluid not fixed.

like image 447
Tom Avatar asked Oct 04 '11 18:10

Tom


People also ask

How to create a horizontal navigation bar with floating list items?

Using inline or floating list items. One way to build a horizontal navigation bar is to specify the <li> elements as inline, in addition to the "standard" code from the previous page: display: inline; - By default, <li> elements are block elements. Here, we remove the line breaks before and after each list item, to display them on one line

How to create a navigation bar with equal-width navigation links?

Learn how to create a navigation bar with equal-width navigation links. <!-- The navigation menu --> width: 25%; /* Four equal-width links. If you have two links, use 50%, and 33.33% for three links, etc.. */ Tip: Go to our CSS Navbar Tutorial to learn more about navigation bars.

Should the navigation bar be centered or stretched out?

The navigation bar background should stretch the full width of the screen (not just the width of the centered content area), but The navigation elements themselves should still be centered over the content area. The sketch below outlines the basic design concept.

How much CSS should I use for my navigation bar?

If you have two links, use 50%, and 33.33% for three links, etc.. */ Tip: Go to our CSS Navbar Tutorial to learn more about navigation bars. Thank You For Helping Us!


1 Answers

With a static number of elements it's easy - http://jsfiddle.net/X56cJ/

However, if you want to have uniform spacing between the text, not the elements themselves - it becomes tricky. Again, if the number of elements doesn't change, you do it with css classes and static widths. Otherwise it'll have to be javascript

EDIT: Here's the JavaScript way of getting same space between elements.

HTML:

<ul class="menu">
    <li>About Us</li>
    <li>Our Products</li>
    <li>FAQs</li>
    <li>Contact</li>
    <li>Login</li>
</ul>

JS:

function alignMenuItems(){
    var totEltWidth = 0;
    var menuWidth = $('ul.menu')[0].offsetWidth;
    var availableWidth = 0;
    var space = 0;

    var elts = $('.menu li');
    elts.each(function(inx, elt) {
        // reset paddding to 0 to get correct offsetwidth
        $(elt).css('padding-left', '0px');
        $(elt).css('padding-right', '0px');

        totEltWidth += elt.offsetWidth;
    });

    availableWidth = menuWidth - totEltWidth;

    space = availableWidth/(elts.length);

    elts.each(function(inx, elt) {
        $(elt).css('padding-left', (space/2) + 'px');
        $(elt).css('padding-right', (space/2) + 'px');
    });
}

Full example here

like image 124
MK_Dev Avatar answered Oct 17 '22 16:10

MK_Dev