Automatic two columns with CSS or JavaScript

Deadly 提交于 2019-11-29 01:58:25

the good news is, there is a CSS only solution. the bad news is, there isn't any major support for it in existing browsers. if it was implemented, it would look like this:

div.multi {
  column-count: 3
  column-gap: 10px;
  column-rule: 1px solid black;      
}

i think for now your best bet is probably server side as mentioned by monoxide

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.

Matt Dawdy

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.

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

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.

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.

tholane

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!