Suppose you have a list (ul
or ol
) that contains many short items which takes up a lot of vertical space and generates much horizontal whitespace:
Depending on the data and design of the containing document styling the list in columns might be approciate.
One way to columnize the list is to float the list items and assigning a concrete width to it.
ul.columns li { float: left; width: 50%; }
ul.three li { float: left; width: 33%; }
ul.four li { float: left; width: 25%; }
Now that the list is columnize, one might want to spice up the thing with images for the individual items. Because background image positions can be better controlled; we use them instead of list-style-image
. The list items then need a bit of padding to avoid that the text covers the icons.
The CSS2 box model formula to calculate the width of an element looks like this: element width = width + padding + border The problem with this formula is that we cannot set the width
to a percentage value and use a non-percent value for the padding
, i.e. control the maximum width in a across all browsers — this is the reason against max-width
…
One possible solution is to wrap the list items in another tag, e.g. span
to apply the padding to — the items may already be wrapped in a tag, e.g. the origin for the technique described in this document is a list of links so you might not need to add a wrapper element.
Using the solution on the sample data yields the following CSS:
ul.columns li {
float: left;
width: 50%;
}
ul.three li {
width: 33%;
}
ul.four li {
width: 25%;
}
ul.iconified li span {
background: #FFF url(icon.gif) no-repeat scroll top left;
padding-left: 24px;
}
<ul class="columns iconified">
<ul class="columns three iconified">
<ul class="columns four iconified">
The columns would look nicer if they had a top-bottom, left-right order instead of the current left-right, top-bottom one. Possible solutions for this include:
The drawback of these solutions is that the separation of content and style is ruined and a new design needs adjustments on the server (which is bad when you provide different skins).
With these drawbacks in mind a JavaScript script to reorder the list items is a better choice.
The provided columnizeItemOrder
function has the following limitations:
Include the JavaScript file columnize.js in your page and execute columnizeItemOrder(<list id>, <number of columns>);
for each column list.
The head for these samples:
<script type="text/javascript" src="columnize.js"></script>
<style type="text/css"">
ul.columns li {
float: left;
width: 50%;
}
ul.three li {
width: 33%;
}
ul.four li {
width: 25%;
}
ul.iconified li span {
background: #FFF url(icon.gif) no-repeat scroll top left;
padding-left: 24px;
}
</style>
columnizeItemOrder("list2", 2);
columnizeItemOrder("list3", 3);
columnizeItemOrder("list4", 4);
columnizeItemOrder("list5", 5);