0

I'm looking for a way to dynamically break a continuous text into multiple columns only if the content of the first column is overflowing. It should be responsive and work for any device width. The solution can be (S)css or JavaScript.

I already tried using column-count like suggested here but this always creates multiple columns no matter if there is still enough space in the first column.

This is what I am looking for visually

This is about what I want to achieve just without the mentioned column-count

.content {
  height: 90vh;
}

.text {
  width: 300px;
  column-count: 2;
}
 <div class="content">
   <div class="text">
      Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Cras ultricies ligula sed magna dictum porta. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Nulla porttitor accumsan tincidunt. Lorem ipsum dolor sit amet,
      consectetur adipiscing elit. Pellentesque in ipsum id orci porta dapibus. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus.
   </div>
 </div>
TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

2

You need to set height to .text so it would know when to move to the next column:

.content {
  height: 90vh;
}

.text {
  height:100%;
  width: 300px;
  column-count: 1;
}
<div class="content">
   <div class="text">
      Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Cras ultricies ligula sed magna dictum porta. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Nulla porttitor accumsan tincidunt. Lorem ipsum dolor sit amet,
      consectetur adipiscing elit. Pellentesque in ipsum id orci porta dapibus. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus.
   </div>
 </div>
Kosh
  • 16,966
  • 2
  • 19
  • 34
  • This seems like a neat solution. You may want to look into widow and orphan settings (by default you get at least two lines in a column for example) which many browsers, though apparently not Firefox, support. – A Haworth Feb 10 '22 at 07:01
0

This is pretty hacky, but I think you can do this in JS by calculating the size of the text and then modifying column-count if it exceeds a certain value. Something like:

const textElement = document.getElementsByClassName('text')[0];
const textElementHeight = textElement.getBoundingClientRect().height
const numColumns = textElementHeight / SOME_CONSTANT;
textElement.style.columnCount = numColumns
Anna Azzam
  • 252
  • 2
  • 7