How to make text over flow into two columns automatically
Asked Answered
J

7

14

I'm currently developing a website and my client wants the text of various articles to overflow into two columns. Kind of like in a newspaper? So it would look like:

Today in Wales, someone actually      Nobody was harmed in
did something interesting.            the incident, although one 
Authorities are baffled by this       elderly victim is receiving
development and have arrested the     counselling.
perpetrator.     

Is there a way I can do this with just CSS alone? I'd prefer not to have to use multiple divs. I'm open to using JavaScript too, but I'm really bad at that, so help would be appreciated. I was thinking maybe JavaScript could count how many <p>'s there are in the content div, and then move the second half of them to be floated right based on that?

Jackelynjackeroo answered 11/10, 2008 at 23:12 Comment(0)
E
16

The good news is that there is a CSS-only solution. If it was implemented, it would look like this:

div.multi {
  column-count: 3;
  column-gap: 10px;
  column-rule: 1px solid black;      
}
Exalted answered 11/10, 2008 at 23:31 Comment(0)
D
5

I'd probably handle it in your backend, whatever that happens to be. An example in PHP might look like:

$content = "Today in Wales, someone actually did something...";
// Find the literal halfway point, should be close to the textual halfway point
$pos = int(strlen($content) / 2);
// Find the end of the nearest word
while ($content[$pos] != " ") { $pos++; }
// Split into columns based on the word ending.
$column1 = substr($content, 0, $pos);
$column2 = substr($content, $pos+1);

It should probably be possible to do something similar in JavaScript with InnerHTML, but personally I'd avoid that whole situation because more and more people are using plugins like NoScript that disables JavaScript till it's explicitly allowed for x site, and above anything else, div's and CSS were designed to degrade nicely. A JavaScript solution would degrade horribly in this case.

Dermatoglyphics answered 11/10, 2008 at 23:24 Comment(0)
O
2

Here's a JQuery plugin which does columns automatically, and can even vary number of columns based on screen size.

I haven't used this myself, but check it out.

Occasion answered 11/10, 2008 at 23:31 Comment(0)
I
0

If you are using Mootools, you can check out MooColumns.

Isochor answered 11/10, 2008 at 23:58 Comment(0)
P
-1

First off, i don't think just css can do that, but i would love to be proven wrong.

Second, just counting paragraphs won't help you at all, you need at least all the heights and calculate the middle of the text height based on that, but you'd have to account for window resizing etc. I don't think there is a reasonably simple off the shelf solution. Unfortunately i'm pessimistic about finding a perfect solution to this problem, But it is an interesting one.

Pah answered 11/10, 2008 at 23:21 Comment(0)
S
-1

This is difficult to achieve in HTML/CSS/JS for a reason (although I'm sure it's possible).

Newspapers use multiple columns to reduce the line width make text more readable. This is fine on paper because when you finish one column you flip your eye up to the beginning of the next.

On the web we use scrolling to allow text to continue past the bounds of the screen therefore don't need columns.

Select answered 12/10, 2008 at 8:14 Comment(0)
S
-1

This is supported in a Mozilla only CSS extension: -moz-column-count. See : https://developer.mozilla.org/en/CSS3_Columns

Scheller answered 17/9, 2010 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.