How to output multicolumn html without "widows"?

Posted by user314850 on Stack Overflow See other posts from Stack Overflow or by user314850
Published on 2010-04-21T22:35:10Z Indexed on 2010/04/22 0:03 UTC
Read the original article Hit count: 254

I need to output to HTML a list of categorized links in exactly three columns of text. They must be displayed similar to columns in a newspaper or magazine. So, for example, if there are 20 lines total the first and second columns would contain 7 lines and the last column would contain 6. The list must be dynamic; it will be regularly changed.

The tricky part is that the links are categorized with a title and this title cannot be a "widow". If you have a page layout background you'll know that this means the titles cannot be displayed at the bottom of the column -- they must have at least one link underneath them, otherwise they should bump to the next column (I know, technically it should be two lines if I were actually doing page layout, but in this case one is acceptable). I'm having a difficult time figuring out how to get this done.

Here's an example of what I mean:

Shopping     Link 3      Link1
Link 1       Link 4      Link2
Link 2                   Link 3
Link 3       Cars
             Link 1      Music
Games        Link 2      Link 1
Link 1              
Link 2       News

As you can see, the "News" title is at the bottom of the middle column, and so is a "widow". This is unacceptable. I could bump it to the next column, but that would create an unnecessarily large amount of white space at the bottom of the second column. What needs to happen instead is that the entire list needs to be re-balanced.

I'm wondering if anyone has any tips for how to accomplish this, or perhaps source code or a plug in. Python is preferable, but any language is fine. I'm just trying to get the general concept down.

© Stack Overflow or respective owner

Related posts about html

Related posts about css-layout