In an experiment to display text/paragraphs in a newspaper-style multi-column layout lead me to Randy Simons’ Automatic Column text source-code. This neat piece of JavaScript code re-arranges paragraphs to fit liquidly-sized columns of text into the available space, maintaining equal heights for each column. It’s really neat in a liquid layout because dependent on the size of a users browser viewable area or screen resolution it will show either fewer or more columns based on your desired ‘em’ size for your columns.
Yes, a completely CSS solution is ideal – and hopefully this is only a year or so from being reality with the latest browsers – but in the meanwhile this solution degrades when the user doesn’t have JavaScript and is fast.
Any interesting read is the A List Apart article CSS Swag: Multi-Column Lists.
My use-case uncovered some minor problems with the script; it didn’t deal with images embed within the paragraphs of text, short blocks of text where columns just didn’t work, I needed a way to toggle the columns on and off (as some users didn’t like them) and while making this addition to the site it seemed like a good time to implement an Increase/Decrease font-size feature.
So the script modifications and samples presented below implement these changes.
Of note; the web-site that these modifications were developed for no longer uses this script. Why? Multiple columns introduce a few problems, so have a think about your use-case.
- The content management system input was not reliable and invalid XHTML caused poorly rendered columns
- Very long news articles meant that the user would scroll down to read the first column and have to scroll up to read the top of the next, down again and so on. Not a big problem with the “..read on” links added – but extra clicks and scrolling in the end.
- Users were used to the single column layout
How to use
Please see “multicolumn-sample.html” in the sample or download links below for a complete implementation. There is also a sample in the multicolumn-development.js file including an advanced tweak I will mention below.
These are the steps for a minimum implementation:
- Copy multicolumn.js to your web-site source directory
- Within the HEAD tag of our HTML files which you would like to add liquid multi-column paragraphs to add the following code:
<script type="text/javascript" src="multicolumn.js"></script>
<script type="text/javascript" src="multicolumn-helper.js"></script>
<style type="text/css"><!--
@import url(multicolumn.css);
--></style>
- Enclose your PARAGRAPH tags within the HTML document that you wish to appear as liquid multi-column paragraphs with a DIV tag with the CLASSNAME “multicolumn”, we also need a second DIV – so don’t miss that:
<div class="multicolumn"><div>
<p>
Paragraph content.
</p>
<p>
Paragraph content.
</p>
</div></div>
- Load your page in your web-browser and test.
If you would like to customise the default minimum column widths there is a quick comment in multicolumn-development.js but I will repeat it here. You will need to tweak the CSS in multicolumn.css. Specifically the following rule where 22em can be adjusted up or down.
.multicolumn-Columnized div {
...
width: 22em;
...
}
You will see in the sample below how to use the new multicolumnHelper class, but for reference it is as simple as including the following HTML code snippet in your page – customise the display as you see fit whether that be graphical buttons or making the list ‘inline’. These font size helpers will work whether the columns are turned on or off.
<ul>
<li><a href="#" id="multicolumn-ColumnsButton" onclick="return multicolumnHelper.toggleColumns(this)">Hide Columns</a></li>
<li><a href="#" onclick="return multicolumnHelper.fontIncrease(this)">Font Larger ++</a></li>
<li><a href="#" onclick="return multicolumnHelper.fontDecrease(this)">Font Smaller --</a></li>
</ul>
Sample
Download
Hopefully these changes are of use. And credit again to Randy Simons for the bulk of the code.