A common problem which constantly crops up when developing websites is how to create an effective
2 columned or
multi columned layout using CSS; a problem which frequently occurs and with what seems to be an elusive solution.
Thankfully, the W3C have come up with a solution; The CSS3 rule(s) that we are interested in are known as the
column rules. Most modern browsers have all used their own prefixes on these rules until they are officially implemented as CSS3 rules:
/* Mozilla browsers */
-moz-column-width
-moz-column-gap
-moz-column-count
-moz-column-rule
/* Webkit Browsers 3 */
-webkit-column-width
-webkit-column-gap
-webkit-column-count
-webkit-column-rule
/* Opera */
-o- ...
etc.
I'll be demonstrating with the
-moz- rules.
We have 2 options on tackling the issue; we can either choose to define a set number of columns using
-moz-column-count - these will spread out equally over the given width to fill the whole area - or we can define a set width for each column using
-moz-column-width which will fit in as many columns of the set width as possible.
Using column-width
Here's an example of how to use it:
<html>
<head>
<title>Multi Column Example using CSS3</title>
<style type="text/css">
#wrapper {
width: 500px;
text-align: justify;
}
#multi-column-example {
-moz-column-width: 200px;
-moz-column-gap: 15px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="multi-column-example">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sagittis, arcu sit amet posuere fermentum, sem tellus iaculis ipsum, id iaculis sapien mi a nunc. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed mollis augue quis sem scelerisque id pellentesque dolor condimentum. Suspendisse potenti. Morbi lacinia urna tortor. Nulla facilisi. Pellentesque vestibulum augue eget nibh mollis tristique. Aliquam erat volutpat. Donec at pharetra nibh. Nullam vel leo arcu, non auctor nunc. Maecenas placerat ultrices mauris porttitor mattis. Integer tortor dui, fringilla vitae cursus tempus, faucibus vel elit. Phasellus aliquet, nulla vulputate pulvinar consequat, justo lacus consequat eros, ac pulvinar nulla elit sit amet mauris.
</div>
</div>
</body>
</html>
The effect:
Note that there would be 70px left under this example, but only 2 columns have been created. If there isn't enough space to fix in another column, the next one will not be created. Also note that if a gap is not set, then it will default. In Firefox, this is 15px.
Using column-count
Here is the same example, with just the CSS changed:
<html>
<head>
<title>Multi Column Example using CSS3</title>
<style type="text/css">
#wrapper {
width: 500px;
text-align: justify;
}
#multi-column-example {
-moz-column-count: 3;
-moz-column-gap: 25px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="multi-column-example">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sagittis, arcu sit amet posuere fermentum, sem tellus iaculis ipsum, id iaculis sapien mi a nunc. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed mollis augue quis sem scelerisque id pellentesque dolor condimentum. Suspendisse potenti. Morbi lacinia urna tortor. Nulla facilisi. Pellentesque vestibulum augue eget nibh mollis tristique. Aliquam erat volutpat. Donec at pharetra nibh. Nullam vel leo arcu, non auctor nunc. Maecenas placerat ultrices mauris porttitor mattis. Integer tortor dui, fringilla vitae cursus tempus, faucibus vel elit. Phasellus aliquet, nulla vulputate pulvinar consequat, justo lacus consequat eros, ac pulvinar nulla elit sit amet mauris.
</div>
</div>
</body>
</html>
The effect:
3 columns will fill up the maximum area given to them.
Using column-rule
Column rules are applied in bewteen column, and are used to decorate the gaps inbetween columns, muh like a border does on an element. They are simply used like so:
-moz-column-rule: 1px solid #666;
and gives an effect like so: