I am able to do the list using float:left;
like this
But I would like to show it like this (as 2 or more columns)
How can I do that?
@sandeep gave good solution. Unfortunately does not work in IE(need ie7 and above).
any help?
I am able to do the list using float:left;
like this
But I would like to show it like this (as 2 or more columns)
How can I do that?
@sandeep gave good solution. Unfortunately does not work in IE(need ie7 and above).
any help?
This works just fine cross-browser, no JS required. You just limit the width of your columns.
If you are stuck with them all in one UL on page load, you can split them out with jQuery to create the same results:
Below is a columnizer, takes as argument the number of columns.
Call: $(elem).columnize(3)
http://jsfiddle.net/Bdsj9/28/
Tested in IE6 from wine in Ubuntu 10.04: works (looks better if you increase the width in the stylesheet I borrowed from @micha -- thanks, btw)
What it does is first calculate the sortorder, because with just styling this will not work. To calculate the sortorder you need to know the number of columns up front. This you can use as a divisor to introduce a 'clear: left'.
Also, using the number of list items and the number of columns you need to estimate a number of rows. This can be used as a divisor to sort the items based on the remainer between their index and the number of rows.
To granulate the sortation, the original index is also taken into account...
After the last orginal item has been placed, the number of columns needs to be reduced by 1. That's why I store the isFinal-boolean. I'm sure this could be done more elegantly with some smart computation, though.
Then introduce the 'clear: left's at the right place and store the original position in a class so you can resort in a later stage (for instance when inserting or removing a list item dynamically)
Best!
The CSS:
Using jquery:
Please take a look at http://api.jquery.com/even-selector/ and http://api.jquery.com/odd-selector/
SEE MY NEW ANSWER--MUCH BETTER THAN THIS
If you are dealing with fixed width items, then this pure css solution that works in IE7+. The example is http://jsfiddle.net/VVMnq/33/. This would require you to know some things about the html you are working with (where the new column is to start). Here is column 2 longer, just to see how it handles it: http://jsfiddle.net/VVMnq/42/
HTML
CSS
As long as your list items are a fixed width, like in your examples, couldn't you just do something like in this fiddle? http://jsfiddle.net/swNYE/1/
And wherever your list gets spit out, simply apply the "left" class to the first half, and the "right" class to the second half. If you're dynamically adding content through Javascript, then you'd simply run through the li's each time something is added, and apply the new correct class.
HTML:
CSS:
NEW ANSWER that is totally different from the first. This assumes the height is always 5 high (which from comments on jblasco's solution made by anglimas, that is the requirement). The solution here is pure css (though if the number of elements is unknown, then some javascript would be required to "count" and set a class called
first
to indicate which element is in the first row).The solution works in IE7+, and will accommodate any number of columns.
See http://jsfiddle.net/VVMnq/107/.
HTML
CSS