Is it possible to float the boxes in a way that minimizes vertical space while still preserving the columns? (The order of the boxes is not important)
If you're using Bootstrap, the col-xx
classes may conflict with the styles defined in the bootstrap css file. To achieve your desired layout, consider breaking away from Bootstrap styling for this specific scenario and element(s). You can continue using Bootstrap styles for other elements.
To achieve this layout, CSS columns are necessary. Make sure to include relevant media queries for responsiveness.
Keep your markup unchanged but remove the col-x
classes to avoid applying Bootstrap styles.
<div class="container-fluid">
<div id="col" class="row">
<div>Lorem Ipsum</div>
...
</div>
</div>
You can define custom styles for child div
elements as follows:
div#col > div { margin: 8px; padding: 6px; }
div#col > div:first-child { margin-top: 0px; } /* remove top margin from first child */
Implement media queries for the parent container to adjust the number of columns based on screen width, for example:
@media screen and (min-width:761px) {
div#col { column-count: 4; column-gap: 0; padding: 8px; }
}
@media screen and (min-width:760px) {
div#col { column-count: 3; column-gap: 0; padding: 8px; }
}
...
Complete Code Snippet:
div#col > div {
margin: 8px; padding: 6px; background-color: #efefef;
-webkit-column-break-inside: avoid;
}
div#col > div:first-child { margin-top: 0px; }
@media screen and (min-width:761px) {
div#col {
-webkit-column-count: 4; -webkit-column-gap: 0; padding: 8px;
-moz-column-count: 4; -moz-column-gap: 0;
column-count: 4; column-gap: 0;
}
}
@media screen and (max-width:760px) {
div#col {
-webkit-column-count: 3; -webkit-column-gap: 0; padding: 8px;
-moz-column-count: 3; -moz-column-gap: 0;
column-count: 3; column-gap: 0;
}
}
@media screen and (max-width:480px) {
div#col {
-webkit-column-count: 2; -webkit-column-gap: 0; padding: 8px;
-moz-column-count: 2; -moz-column-gap: 0;
column-count: 2; column-gap: 0;
}
}
@media screen and (max-width:360px) {
div#col {
-webkit-column-count: 1; -webkit-column-gap: 0; padding: 8px;
-moz-column-count: 1; -moz-column-gap: 0;
column-count: 1; column-gap: 0;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div id="col" class="row">
<div>Lorem Ipsum</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Lorem Ipsum</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
</div>
</div>
Live Example: http://jsfiddle.net/abhitalks/ar29jfqt/2/
Test the layout by resizing the window in the provided fiddle to observe how the columns adjust themselves accordingly.