9

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.

1
  • 2
    You'll usually need to know how many items you have in your menu to pull this one off. if you have 5 items, you set each item as 20% width etc. Oct 4, 2011 at 18:39

3 Answers 3

10

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

2
  • Yeah, I'll have a static number of elements, and that's what I'm shooting for is uniform spacing between the text. Any suggestions on going the Javascript route?
    – Tom
    Oct 4, 2011 at 19:24
  • If you have a static number of elements, than you can use pure CSS to specify the width of each element. It'll take a bit of time to get the widths right, but it's doable. While you could use JS for this, CSS will be a bit more simple.
    – MK_Dev
    Oct 4, 2011 at 22:11
3

Using display: table on your parent, and display: table-cell on your children will do the trick. This is not supported in <= IE7.

http://codepen.io/simply-simpy/pen/jzski

3
  • 1
    That only works if all your titles are the same size. Add <li><a href="#">fivesixseveneightnineten</li> to your list and you'll see what I mean.
    – bronson
    Sep 29, 2014 at 22:05
  • 1
    Thanks @bronson -- I added a width: 25% and it works with a longer link. -- codepen.io/simply-simpy/pen/jzski Sep 29, 2014 at 22:49
  • Flex-box is probably a better solution moving forward. Feb 12, 2016 at 16:44
1

If you know how many elements you'll have, you can specify a width of each element as a percent. Otherwise it won't be possible without resorting to tables or javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.