Code Snippet
Multiple Columns
Here is an example of a simple three-column class:
.three-col {
-moz-column-count: 3;
-moz-column-gap: 20px;
-webkit-column-count: 3;
-webkit-column-gap: 20px;
}Of which you would apply to a block of text like so:
<p class="three-col">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>Example
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus
Note that the height of each column is auto-balanced, as per the spec.
Also note this demo and sample code is using moz and webkit vendor prefixes, should only work in Gecko (Firefox 1.5+, et al.) and Webkit (Safari 3+, Chrome, et al.) browsers. No native support in Internet Explorer or Opera yet that I know of.
All Related Properties
.three-col {
-moz-column-count: 3;
-moz-column-gap: 20px;
-webkit-column-count: 3;
-webkit-column-gap : 20px;
-moz-column-rule-color: #ccc;
-moz-column-rule-style: solid;
-moz-column-rule-width: 1px;
-webkit-column-rule-color: #ccc;
-webkit-column-rule-style: solid ;
-webkit-column-rule-width: 1px;
}You can also set the column-width (with prefixes) but it generally makes more sense to let it auto calculate that.
The rule ("rule", as in, a line) will split the gap down the middle. You can use the same values as you would a border.
Take care not to have your text blocks be so enormously tall that they are taller than a (fairly small) browser window, otherwise it's the same problem as text being wider than the browser window (scrolling back and forth to read = sucks). Also consider text-align: justify;
JavaScript Fallback
Is presented in this A List Apart article.
just ordinary question like always :-)
is IE also compatible with this column-count?
thanks
Hi, I was just refreshing myself on this technique and tested it in a bunch of different browsers. IE 9 and under provide no support to this at all, so you should use a jQuery plugin as a fallback.
I tested in the IE 10 platform tester and it does indeed work, and without vendor prefixes. I guess better late than never.
He says in the article..
“sample code is using moz and webkit vendor prefixes, should only work in Gecko (Firefox 1.5+, et al.) and Webkit (Safari 3+, Chrome, et al.) browser”
It looks like IE is not a compatible browser at this time.
Still, it degrades nicely to a single column – yet again, IE users get the information in a logical, normal manner, just not as visually pleasing!
It works in IE with the JS fallback.
The ALA article was from 2005. I wonder if anyone has come up with any new fallback scripts in the last six years. Mainly for IE since probably 99.9997% of other CSS browsers in use these days probably support the multi-column layout module.
who cares of IE6 :P
IE is losing market anyway.. bring on the CSS2.1 + :)
good, nice artical
This seems to work in the latest version of Opera. Using Opera 11.10 and Dragonfly I was able to apply the css. Here is a <a href="http://dl.dropbox.com/u/886672/css-tricks/multiple-columns-works-in-opera.png">screenshot</a>
Oops looks like I mangled that lets try this: http://dl.dropbox.com/u/886672/css-tricks/multiple-columns-works-in-opera.png
Does this work with just text? Or can one use images along with text? Fairly newcomer so excuse the seemingly silly ?
I wrote a blog article on this just the other day :) Great Stuff. Wish it had IE support though…
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.