I've been tasked with generating a horizontal nav list that looks like this:
the point being that the items need to be individually spaced from absolute left to right.
Setting widths is a pain because different browsers appear to interpret them differently. Also, the number of items in this list will change depending on the user.
Any advice would be appreciated!
Following @Dean advice, I've found myself with the below - which is pretty much correct. The only thing I'm thinking is that the left two elements are unfortunately short, hence the large gap. I'm hoping the client will be happy with text-align; center on all the elements with a touch of padding at the side!
The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.
The spacing between list items in an orderedlist or itemizedlist element can be minimized by the document author by adding a spacing="compact" attribute to the list element. In HTML output, the list will get a compact="compact" attribute on the list start tag to reduce spacing in the browser.
Space can be added between each list item by setting a margin on the "LI". Margin can be set on the top, bottom or top and bottom of each list item. This version has a margin of ". 1em" on top and bottom of the list items.
We use CSS padding-left property, to create a space between bullets and the text. It is used to set the padding area on the left of an element.
I made a jsFiddle of your menu... everything is perfectly spaced, dynamic, and it goes all the way to the left/right edges without JavaScript or weird/ugly HTML semantic issues. (And it should work in IE 6, if it matters.)
http://jsfiddle.net/bXKFA/2/
HTML:
<div id="menuwrapper">
<div class="menuitem">CAREERS</div>
<div class="menuitem">TRADE</div>
<div class="menuitem">CONTACT US</div>
<div class="menuitem">PRIVACY POLICY</div>
<div class="menuitem">T&CS</div>
<div class="menuitem">SITEMAP</div>
<div class="menuitem">CORPORATE</div>
<div class="menuitem">ACCESSIBILITY</div>
<span class="stretcher"></span>
</div>
CSS:
#menuwrapper {
height: auto;
background: #000;
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
}
.menuitem {
width: auto;
height: 40px;
vertical-align: top;
display: inline-block;
*display: inline;
zoom: 1
background: #000;
color: yellow;
}
.stretcher {
width: 100%;
display: inline-block;
font-size: 0;
line-height: 0;
}
I based it on thirtydot's answer in this thread...
Fluid width with equally spaced DIVs
You won't be able to get this working well in IE6, but you can use this for all major browsers:
ul {
display: table;
table-layout: fixed; /* the magic dust that ensure equal width */
}
li { display: table-cell; text-align: center; }
For IE6 (possibly 7) you will need to use Javascript to calculate the widths dynamically.
Also don't forget to text-align: left your first list item, and text-align: right the last.
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