Home > Back-end >  Break content into multiple columns if needed, distribute elements from left to right
Break content into multiple columns if needed, distribute elements from left to right

Time:11-20

If we have some buttons like

enter image description here

and when the number of buttons increases so that a max height is reached, how do we get them in multiple columns like this?

enter image description here

and if the display becomes to narrow to accomodate for the columns (any number of columns that sooner or later are too wide) allow a higher "max height" like

enter image description here

I tried this

columns: 3;
column-gap: 10px;

Which results in button being distributed evenly between columns.

(The number of columns is also locked to 3).

If I set height of the parent, columns gets higher and possibly fewer (but I dont want the parent to have unnecessary height).

enter image description here

CodePudding user response:

You just need to use flex with property flex-direction: column;

.container {
  display:inline-flex;
  flex-direction: column;
  flex-wrap: wrap;
  height:200px;
}
.item {
  background-color:black;
  height:30px;
  width:100px;
  margin-bottom:5px;
  margin-right:5px;
  color:#fff
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
  <div class="item">7</div>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

To change the height when many items I think you need javaScript

  •  Tags:  
  • css
  • Related