A Pen by Chris Coyier on CodePen.
Created
January 31, 2020 19:48
-
-
Save charlesvestal/6dedfbfd2557cbd5870e4a82e599f0ba to your computer and use it in GitHub Desktop.
Masonry with Flexbox + JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .masonry-with-columns#masonry-with-columns | |
| - for i in (1..15) | |
| div #{i} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const numCols = 3; | |
| const colHeights = Array(numCols).fill(0); | |
| const container = document.getElementById('masonry-with-columns'); | |
| Array.from(container.children).forEach((child, i) => { | |
| const order = i % numCols; | |
| child.style.order = order; | |
| colHeights[order] += parseFloat(child.clientHeight); | |
| }) | |
| container.style.height = Math.max(...colHeights) + 'px'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body { | |
| margin: 0; | |
| padding: 1rem; | |
| } | |
| .masonry-with-columns { | |
| display: flex; | |
| flex-direction: column; | |
| flex-wrap: wrap; | |
| max-height: 1000px; | |
| div { | |
| flex: 1 0 auto; | |
| background: #EC985A; | |
| color: white; | |
| margin: 0 1rem 1rem 0; | |
| text-align: center; | |
| font-family: system-ui; | |
| font-weight: 900; | |
| font-size: 2rem; | |
| } | |
| @for $i from 1 through 36 { | |
| div:nth-child(#{$i}) { | |
| $h: (random(400) + 100) + px; | |
| height: $h; | |
| line-height: $h; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment