Home Blog Creating Grid Columns with Uneven Row Heights, Resembling column-count, While Preserving Element Order

Creating Grid Columns with Uneven Row Heights, Resembling column-count, While Preserving Element Order

This straightforward demonstration illustrates how to achieve a masonry effect within a grid layout featuring columns and rows of varying content heights. It closely resembles the column-count approach while ensuring the preservation of elements order.

Display Content in Columns Keeping the Elements Order

Live Demo

The demo employs the regular CSS property display: grid for the wrapper element. For demonstration purposes, we limit the number of columns. Additionally, the solution incorporates vanilla JavaScript (without dependencies) that iterates through the grid elements. Each element can then be positioned vertically with the expected margin-block-start, taking into account the previous element in the same column, its size, and, of course, the grid row gap for consistency.

For additional options on aligning content within the grid, feel free to explore my other demo: Free CSS and HTML Grid Generator – Demo and Code Snippet.