Starting 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.
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
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.
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.
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With