How can I create a Masonry style layout with CSS Grid in Webflow that includes content blocks with variable height and dynamically adjusts to handle extra blocks?

Published on
September 22, 2023

To create a Masonry style layout with CSS Grid in Webflow that includes content blocks with variable height and dynamically adjusts to handle extra blocks, you can follow these steps:

  1. Prepare your content: Ensure you have the content blocks ready with their respective variable heights. This can include images, text, or any other element you want to display in the Masonry layout.

  2. Set up the grid: In Webflow, create a new section where you want the Masonry layout to appear. Make sure to set the section's display property to "grid" using the style panel.

  3. Define the grid properties: With the section selected, go to the style panel and click on the blue "+ Add" button next to "Grid". This will open the grid settings dialog.

  4. Setup gap and column properties: In the grid settings dialog, set the "Gap" value to define the spacing between the content blocks. Adjust this value according to your design preferences. Next, specify the number of columns you want in your Masonry layout. You can start with a single column and allow the grid to automatically adjust as needed.

  5. Position the content blocks: Place the content blocks within the section. By default, they will align in a single column. Adjust their width to fit the desired number of columns by selecting each block and using the style panel to set the "grid-column" property. For example, to span across two columns, set the "grid-column" property to "span 2".

  6. Enable Masonry layout: Webflow does not have a built-in Masonry layout option, but you can achieve it using custom CSS. Select the section, go to the style panel, and click the "+" button next to "Custom Code". In the "Head Code" tab, add the following CSS code:

.section-class-name {  display: grid;  grid-auto-flow: dense;}

Make sure to replace "section-class-name" with the actual class name of your section element.

  1. Add custom JavaScript: To make the Masonry layout dynamically adjust to extra blocks, you'll need to incorporate some custom JavaScript. Go to the "Custom Code" tab in the style panel and add the following JavaScript code:
<script>  function applyMasonryLayout() {    const section = document.querySelector(".section-class-name");    const blocks = section.querySelectorAll(".block-class-name");    for (let i = 0; i < blocks.length; i++) {      const gridColumnEnd = Math.ceil((i + 1) / section.childElementCount * 10);      blocks[i].style.gridRowEnd = `span ${blocks[i].offsetHeight}px`;      blocks[i].style.gridColumnEnd = `span ${gridColumnEnd}`;    }  }  window.addEventListener('load', applyMasonryLayout);  window.addEventListener('resize', applyMasonryLayout);</script>

Replace "section-class-name" with the actual class name of your section element and "block-class-name" with the class name of your content blocks.

  1. Publish and test: Finally, publish your Webflow site and test the Masonry layout by adding or removing content blocks. The layout should automatically adjust to handle the varying heights and dynamically distribute the blocks across the columns.

Now you have a Masonry style layout with CSS Grid in Webflow that can handle content blocks with variable heights and dynamically adapts to accommodate extra blocks.

Additional Questions:

  • Can I create a Masonry layout in Webflow without using CSS Grid?
  • What other grid settings can be adjusted in Webflow?
  • How can I create a responsive Masonry layout in Webflow?