How to Create a Masonry grid with flexbox in CSS? Last Updated : 06 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Masonry Grid in CSS is a layout technique that arranges items in a way that resembles a masonry wall, where items of varying heights are placed in columns, filling in gaps and creating a staggered appearance. This can be done using Flexbox by allowing items to wrap and adjusting their heights to create a visually appealing, column-based layout.These are the following approaches: Table of ContentFlexbox with Manual Row WrappingFlexbox with Align-Content and Variable HeightsFlexbox with Manual Row WrappingIn this approach, we are using Flexbox with manual row wrapping to create a masonry-like grid layout. By applying different flex values to the items, we simulate a masonry effect where items of varying sizes can be aligned, and Flexbox’s flex-wrap property makes sure they wrap appropriately within the container. Example: The below example uses Flexbox with Manual Row Wrapping to create a Masonry grid with Flexbox in CSS. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Flexbox with Manual Row Wrapping</title> <style> body { display: flex; flex-direction: column; align-items: center; padding: 20px; font-family: Arial, sans-serif; } h1 { color: green; } h2 { margin-top: 20px; font-size: 1.2em; text-align: center; } .container { display: flex; flex-wrap: wrap; gap: 10px; max-width: 1200px; } .item { flex: 1 1 calc(33.333% - 10px); margin-bottom: 10px; background-color: #f9f9f9; border: 1px solid #ddd; border-radius: 5px; padding: 20px; box-sizing: border-box; text-align: center; min-height: 200px; } .item:nth-child(2) { flex: 1 1 calc(50% - 10px); } .item:nth-child(4) { flex: 1 1 calc(25% - 10px); } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2>Flexbox with Manual Row Wrapping</h2> <div class="container"> <div class="item"> <h3>Languages</h3> <p>Python, JavaScript, Java, C++</p> </div> <div class="item"> <h3>Courses</h3> <p>Data Structures, Algorithms, Web Development</p> </div> <div class="item"> <h3>Articles</h3> <p>Introduction to Algorithms, Web Development Basics</p> </div> <div class="item"> <h3>Tutorials</h3> <p>Advanced Algorithms, JavaScript Essentials</p> </div> <div class="item"> <h3>Problems</h3> <p>Array Manipulation, String Matching</p> </div> <div class="item"> <h3>Interviews</h3> <p>Common Interview Questions, Preparation Tips</p> </div> </div> </body> </html> Output:OutputFlexbox with Align-Content and Variable HeightsIn this approach, we are using Flexbox with align-content to manage the alignment of rows in the container, while setting variable heights for the items to create a masonry-like effect. This layout makes sure that items of different heights align neatly within the rows. Example: The below example uses Flexbox with Align-Content and Variable Heights to Create a Masonry grid with flexbox in CSS. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Flexbox with Align-Content and Variable Heights</title> <style> body { display: flex; flex-direction: column; align-items: center; padding: 20px; font-family: Arial, sans-serif; } h1 { color: green; } h2 { margin-top: 20px; font-size: 1.2em; text-align: center; } .container { display: flex; flex-wrap: wrap; gap: 10px; max-width: 1200px; align-content: flex-start; } .item { background-color: #e0f7fa; border: 1px solid #b2ebf2; border-radius: 8px; padding: 15px; box-sizing: border-box; text-align: center; flex: 1 1 calc(33.333% - 10px); min-height: 150px; } .item:nth-child(1) { height: 250px; } .item:nth-child(2) { height: 180px; } .item:nth-child(3) { height: 200px; } .item:nth-child(4) { height: 300px; } .item:nth-child(5) { height: 220px; } .item:nth-child(6) { height: 170px; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2>Flexbox with Align-Content and Variable Heights</h2> <div class="container"> <div class="item"> <h3>Languages</h3> <p>Python, JavaScript, Java, C++</p> </div> <div class="item"> <h3>Courses</h3> <p>Data Structures, Algorithms, Web Development</p> </div> <div class="item"> <h3>Articles</h3> <p>Introduction to Algorithms, Web Development Basics</p> </div> <div class="item"> <h3>Tutorials</h3> <p>Advanced Algorithms, JavaScript Essentials</p> </div> <div class="item"> <h3>Problems</h3> <p>Array Manipulation, String Matching</p> </div> <div class="item"> <h3>Interviews</h3> <p>Common Interview Questions, Preparation Tips</p> </div> </div> </body> </html> Output:Output Comment More infoAdvertise with us Next Article How to Create a Masonry grid with flexbox in CSS? G gauravggeeksforgeeks Follow Improve Article Tags : Web Technologies CSS CSS-Questions Similar Reads How to create a Trello Layout with CSS Grid and Flexbox ? Trello layout is used to organize or manage information in stages. It can be created using CSS Grid and Flexbox. Let's create the layout as shown in the below image. In the image, Trello Layout consists of Trello List which holds the items or information in it. Layout Structure: Trello layout Exampl 2 min read How to Create a Responsive Image Gallery with Flexbox? A responsive image gallery adjusts to different screen sizes to ensure that images look good on all devices. Flexbox, a CSS layout model, provides a powerful way to create a responsive image gallery by allowing flexible and efficient arrangements of items within a container. ApproachCreate a contain 3 min read How to Create a Flexbox Grid? Flexbox is a powerful layout module in CSS that is designed to provide an efficient way to align and distribute space among items in a container. It is particularly useful for creating flexible and responsive grid layouts. Different Approach for Creating a Flexbox Grid:Table of ContentBasic Flexbox 4 min read How to Create a 2-Column Layout Grid with CSS? Creating a two-column layout is a common design pattern used in web development. This layout helps to organize content efficiently and is used across various types of websites. A 2-column layout divides the webpage into two sections, which can be useful for creating sidebars, main content areas, or 4 min read How to Create a 4-Column Layout Grid with CSS? We can create a layout with multiple columns using CSS. It is easier to design complex layouts. In this article, we are going to discuss how to create a 4-column layout grid using CSS. We will discuss different approaches, their syntax, and code examples of each method. A 4-column layout grid is a c 3 min read How to Create a Responsive Image Grid using CSS? A responsive image grid is a layout technique used to display images in a grid-like structure that adjusts dynamically based on the screen size to ensure that the grid looks good and functions well across various devices and screen resolutions. Below are the approaches to creat a responsive image gr 3 min read How to Create a 3-Column Layout Grid with CSS? Grid in CSS is a powerful layout system that allows you to create complex, responsive designs with ease. By defining rows and columns, you can position elements precisely within a grid container. Using grid properties like grid-template-columns, you can create flexible and adaptive layouts, such as 3 min read How to Align Last Row to Grid in Flexbox ? Flexbox is a very useful layout module of CSS (Cascading Style Sheets). It is very easy to create flexible and responsive layouts using Flexbox. But it doesnât always give the expected alignment, especially on the last row of a grid. Flexbox will distribute the items evenly across the last row, whic 2 min read How to Create a Responsive Image Gallery using CSS? Creating a responsive image gallery using CSS is a common task in web development. A responsive gallery adjusts to different screen sizes and ensures that images look good on devices ranging from mobile phones to desktop monitors. In this article, we will explore different approaches to creating a r 3 min read How to Create a Responsive CSS Grid Layout ? Here are different ways to create a responsive grid layout with CSS.1. Using auto-fill PropertyThis method can be used CSS Grid for a responsive layout. The grid-template-columns property adjusts columns based on space, keeping a minimum width of 200 pixels. Gaps between items are set with grid-gap. 3 min read Like