I'm wondering if it is possible to do this in CSS, without javascript:
A list of N <li>
items, with display inline, equal width, and the width of the all equal to the width of the container
I can have 3 <li>
items, in this case the <li>
width will be 33%, or I can have 4 <li>
items, then the li width will be 25%.
It is possible with CSS3 flex boxes, as demonstrated in this fiddle (for webkit browsers only). There are other browser custom properties that would make this work for recent versions of Firefox and IE. If you need something that works for Opera or older versions of IE then there is a JavaScript library called Flexie which might work.
Credit to The CSS3 Flexible Box Layout (flexbox) for the information on the browser support.
HTML
CSS
Assuming the
li
s are generated from some server-side code, you can use the following "trick":Make a standard left-floated list and you can (or must) set display to inline to avoid IE6 doubling a possibly existing margin-left.
Assuming you have a static page, you can set your list up like this:
HTML:
and CSS:
The use of a clearfix-class is demonstrated here
You could, with a limited number of possibilities. In CSS3 you can't do it for an arbitrary number of columns, though. You may be able to in CSS4; I don't know yet.
I hope you get the idea. Do you want to do this? I hope not.
This is a perfect example for usage of
display: table
. Works in modern browsers and IE8+...Support chart
JSFiddle
css:
html: