How to make divs stack without spaces and retain order on smaller sizes - in Bootstrap

后端 未结 7 1134
不思量自难忘°
不思量自难忘° 2021-01-18 02:47

I really don\'t know how to put this in words.

When I have 2 divs in a row, each has different height, so the next row have unwanted space.

But stack correct

7条回答
  •  生来不讨喜
    2021-01-18 03:36

    It seems to me that you want your div heights to be dynamic. That means, all the divs inside a row should have the height of the div with more content and therefore the height of the divs will depend on content that can vary. To accomplish that you need more than just CSS, you need some simple JQuery or JavaScript. Using JQuery:

    div 1
    div 2
    div 3
    div 4

    The idea here is to run the FixSizeOfRows() function when your last div has fully loaded. The function FixSizeOfRows() goes row by row injecting the height of the row to each of its child divs (note that the row height increases with the child div that has more content).

    I prefer JQuery when manipulating the DOM instead of plain JavaScript. With JavaScript you could do something similar but a bit more complex. Let me know if you need more clarification on how this code works.

提交回复
热议问题