Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make the content of a div go to the next column by exceeding the height of this div

Tags:

html

css

I apologize if I have not formulated the question well, I hope to make myself understood. basically I have a list of elements, these are distributed in each line. I would like that when this line is going to exceed the total size of the div, the list will continue in the space available in front.

enter image description here

<div>
  <ul>
    <li>
      1  
    </li>
    <li>
      2  
    </li>
    <li>
      3  
    </li>
    <li>
      4  
    </li>
    <li>
      5  
    </li>
    <li>
      6
    </li>      
  </ul>
</div>

div
{ 
 border: 1px solid red;
 height: 100px;
 width:300px;
}

how can I do it?

https://jsfiddle.net/ug0f38b1/

like image 772
yavg Avatar asked Dec 06 '25 05:12

yavg


1 Answers

I'm using flexbox to solve this with the direction set to column. You need the height of the ul to fill the parent container so it knows when to break.

The items are naturally distributed as the list grows. enter image description here

div {
  border: 1px solid red;
  height: 100px;
  width: 300px;
}

ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 100%;
  margin: 0;
}
<div>
  <ul>
    <li>
      1
    </li>
    <li>
      2
    </li>
    <li>
      3
    </li>
    <li>
      4
    </li>
    <li>
      5
    </li>
    <li>
      6
    </li>
  </ul>
</div>

jsFiddle

Adjusting column spacing

One nice thing about using flexbox for this is that we can set the ul to be inline-flex which allows easy control over the column spacing.

enter image description here

jsFiddle

like image 157
Andy Hoffman Avatar answered Dec 09 '25 17:12

Andy Hoffman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!