Koodoz Design

a blog about all things goudagood! Koodoz Design is a creative design studio based in Richmond, Melbourne. We help businesses unleash their full potential through visual communication and clever design.

Subscribe for a free feed! Mmmm…

Newspaper Style Columns With (and Without) CSS3

written by Daniel Angel

26 Aug 2009

Newspaper

One of the coolest features of CSS3 is the ability to create newspaper-like columns on your layouts without any extra markup. We always find ourselves “cheating”, using things like <div id="column-one"> which is not very good, so this property would save us a lot of time and unnecessary code.

CSS3 solves all this with the use of some really cool properties: column-width, column-gap and column-rule. I guess they’re pretty self explanatory, except for column-rule which basically behaves like a border in between columns, so it can be treated like one, using width, style, etc. If you need more details, you can go to the source.

The problem? you guessed it! It’s still not supported by some browsers, namely IE and Opera (need to check IE8 and Chrome, though). Anyway, for this blog post I decided to start testing cross-browser solutions for the problem and this is what I’ve come up with so far.

First, we use the property for the browsers that support it, so if we have something like:

<div id="content">
<p>The quick brown fox jumped over the lazy dog</p>
…
<p>And they lived happily everafter</p>
</div>

we then style it like this:

div#content {
	-moz-column-count: 4;
	-webkit-column-count: 4;
}

Notice the prefixes -moz and -webkit. The reason for their presence lies in the fact that amazingly enough, the specification is still a draft (it was originally suggested in 2004!), so we need those. They’re obviously not 100% standard but in this case I would favour common sense as opposed to standards compliance. The styles themselves can get immensely fancier, using the column-gap and column-rule properties, but let’s keep it simple for the purposes of this post.

If you use a decent browser and look at that code, you should see something like this:

Demonstration of CSS columns on decent browsers

Everyone else should see the style-less rendering:

Document without columns styling

Taking care of all other browsers

So, to make sure no one else misses out on the fun, we can use a little jQuery. There are many jQuery plugins to deal with multi-columns, but Systemantics’ columnize plugin was the best-suited one I could find, so cheers to them!

We don’t want to apply the columnize styles to the browsers that support column-width, so we need to serve the javascript only to those that absolutely need it. You can do so by using jQuery’s browser detection, but I would suggest using get_browser() instead. It gives you much more control:

<?php if ( ($browser != 'Firefox') && ($browser != 'Safari') ) { ?>	
	// enter columnize code here
<?php } ?>

Using columnize is pretty straightforward. You invoke jquery and the plugin, and then simply do something like:

$("#content").columnize({columns: 4});

By default, columnize adds the class .column to each of the generated columns, so be sure to style it properly. The overall width of each element should be equal to the width of the parent element, divided by the number of columns, so for instance, if your parent element is 1200px wide and you have four columns, the overall width of each .column element should be 300px. Columnize has tons of other options as well, so be sure to check them out.

You can look at the demonstration here. As usual, comments and questions are always welcome. Thanks for reading!

Tags: , , , , ,

This entry is filed under Web Design. You can follow any responses to this entry through the RSS 2.0 feed.

You can leave a response, or trackback from your own site.


7 Responses to “Newspaper Style Columns With (and Without) CSS3”

  1. Joel: 3 September 2009

    great article – keep them coming!

  2. Mike: 6 October 2009

    Hi Daniel,

    Fantastic site, and great article, simple and informative.

    I’ve been having a play with this for my website which needs to look like a spoof newspaper, works fine on FF (of course), but I can’t get it to play on IE. I’m new to jQuery and php, so that’s where I’m probably falling down. Any pointers would be great.

    Here’s the page:
    http://www.theclassifiedsband.co.uk/band.php
    or
    http://www.theclassifiedsband.co.uk/band.html

    Cheers

  3. Daniel Angel: 7 October 2009

    Mike, thanks for your comment.

    IE support can definitely be painful. My advice: don’t worry about IE6 … make it “degrade gracefully” as for IE7+, you can either tinker with jQuery a little longer or you could use get_browser() (if you’re using php) and serve a couple of extra floated DIVs to get it to work. Let us know how it goes :)

    Cheers!

  4. Mike: 9 October 2009

    Hey Dave, thanks for your post.

    Not quite sure what you mean by ’serve a couple of extra floated DIVs to get it to work’

    Do you mean if the browser isn’t Safari or FF I should redirect to another page with leftcolumn, midcloumn, rightcolumn DIVs?

    It feels as though that’s in fact not what you mean, and I am missing a beautiful nugget of web wizardry.

    Thanks in advance for clearing that up for me. =P

  5. Daniel Angel: 14 October 2009

    Hey Mike, that’s exactly what I mean. Not the best solution, but, hey, blame it on Microsoft for not supporting the cool stuff :P

    However, you can automate the whole process using a simple php function that splits your text and wraps it into a set number of columns. It would depend on how you store/keep your original text, but it could be something like this:

    function makeColumns($string, $number_of_columns) {

    $words = explode(' ', $string);
    $number_of_words = ceil((count($words))/$number_of_columns));
    $new_string = '';

    $columns = array_chunk($words, $number_of_words);

    foreach($columns as $key => $column) {
    $columns[$key] = '<div>'.implode(' ', $column).'</div>';
    $new_string .= $columns[$key];
    }

    return $new_string;

    }

    DISCLAIMER: I’m just typing as I think … haven’t tested it, but I think you get the idea.

    It’s quick and dirty, sure, but a) it serves its purpose b) it’s the burden Microsoft places on us web development folk.

    Hope I was helpful!

  6. פורומים: 15 October 2009

    great post, going to use it in my site. just wondering what is better this or columnize…

  7. Daniel Angel: 30 October 2009

    @פורומים thanks for your comment. It depends on who your target audience are. Take a look at your stats. Too many IE users? Go for columnize. If your audience is mostly made up of firefox/safari/chrome/konqueror users, you can use css3 and make it degrade gracefully for IE users.


Show your love: speak now…

Back to top!