How to create a Masonry Layout using Flexbox in Webflow?

TL;DR

To create a Masonry layout using Flexbox in Webflow, you can take advantage of the powerful grid system and flexbox properties available in the Designer. Here's a step-by-step guide on how you can achieve this:

​

Step 1: Set up the grid container

- Start by adding a new section to your Webflow project, or use an existing one.

- Once you have your section in place, give it a class name, like "masonry-container".

- Go to the Styles panel and set the display property of the container to "grid". This will create a grid context for its child elements. You may also adjust other properties, such as the grid gap, to define the spacing between items.

​

Step 2: Create the grid items

- Inside the masonry container, add divs for each item you want to display in the masonry layout.

- Give each div a class name, like "masonry-item".

- In the Styles panel, set the flex property of the items to "auto". This will make the items grow horizontally to fit the available space.

- You may also add styles to make each item visually appealing, such as setting a background color, adding padding, or adjusting margins.

​

Step 3: Customize the grid

- Select the masonry container and go to the Grid section in the Styles panel.

- Adjust the grid template columns to define the desired width of each column. For example, you can use a fraction-based layout like "repeat(auto-fit, minmax(300px, 1fr))" to ensure the items adapt to different screen sizes.

- You can also experiment with other grid-related properties, such as aligning the items using justify-items or align-items.

​

Step 4: Fine-tuning the Masonry effect

- By default, the grid items will stack vertically, but they won't create a masonry effect. To achieve this, we'll use some custom code.

- Open the Custom Code section of the project settings (in the Designer) or in the page settings (for specific pages).

- In the Head Code section, insert the following code snippet:

​

```

<script>

  Webflow.push(function() {

    var masonryContainer = $('.masonry-container');

    var masonryItems = $('.masonry-item');

​

    // Set up layout on page load

    masonryContainer.imagesLoaded(function() {

      masonryContainer.masonry({

        itemSelector: '.masonry-item',

        transitionDuration: 0

      });

    });

  });

</script>

```

This code uses the Masonry JavaScript library to create the masonry effect. The `imagesLoaded` function ensures the layout is only initiated once all images are loaded, preventing visual discrepancies.

​

Step 5: Preview and adjust

- Save your changes and switch to Preview mode to see the Masonry layout in action.

- If you need to make adjustments, you can go back to the Designer and modify the grid container, item styles, or the grid-related properties to get your desired layout.

- Remember to publish your project for the changes to take effect on the live site.

​

That's it! By combining the power of Webflow's grid system, flexbox, and custom code, you can create a Masonry layout that showcases your content in an elegant and dynamic fashion.

Rate this answer

Other Webflow Questions