How to Create a Flexbox Grid?
Last Updated :
01 Aug, 2024
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:
Basic Flexbox Grid
Using a basic flexbox grid we can set up a container with the display: flex; property and define the flex properties for the child elements to distribute space evenly.
- Create the basic structure of the web page using <div> elements for the container and items in the HTML document.
- Set the container's display property to flex and enable wrapping with flex-wrap: wrap.
- Define the flex properties for the child elements to distribute space evenly using flex: 1 1 200px;
Syntax:
.container {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.item {
flex: 1 1 200px;
}
Example: This HTML uses Flexbox to create a responsive grid where items wrap with a gap, adjusting their size based on available space.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<style>
.container {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.item {
flex: 1 1 200px;
background-color: lightblue;
padding: 20px;
text-align: center;
border: 1px solid #ccc;
}
</style>
<title>Basic Flexbox Grid</title>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
</body>
</html>
Output:
Responsive Flexbox Grid
For making grid responsive, we can use media queries to adjust the flex properties based on different screen sizes.
- Create the basic structure of the web page using <div> elements for the container and items in the HTML document.
- Set the container's display property to flex and enable wrapping with flex-wrap: wrap.
- Use media queries to adjust the flex properties based on different screen sizes.
Syntax:
.container {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.item {
flex: 1 1 200px;
}
@media (max-width: 600px) {
.item {
flex: 1 1 100%;
}
}
Example: This demonstrates a responsive flexbox grid layout where items adjust their size and arrangement based on the screen width.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<style>
.container {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.item {
flex: 1 1 200px;
background-color: lightgreen;
padding: 20px;
text-align: center;
border: 1px solid #ccc;
}
@media (max-width: 600px) {
.item {
flex: 1 1 100%;
}
}
</style>
<title>Responsive Flexbox Grid</title>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
</body>
</html>
Output:
Nested Flexbox Grid
Using Nested Flexbox grids we can place a Flexbox container inside another Flexbox container to create complex layouts.
- Create the basic structure of the web page using nested <div> elements for the outer and inner containers and items in the HTML document.
- Set the display property to flex for both the outer and inner containers.
- Enable wrapping with flex-wrap: wrap for both containers and define the flex properties for the child elements.
Syntax:
.outer-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.inner-container {
display: flex;
flex-wrap: wrap;
gap: 10px;
flex: 1 1 300px;
}
.item {
flex: 1 1 100px;
}
Example: This demonstrates nested flexbox layout, where '.outer-container' holds multiple '.inner-container' elements, each with its own grid of '.item' elements.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.outer-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.inner-container {
display: flex;
flex-wrap: wrap;
gap: 10px;
flex: 1 1 300px;
background-color: lightcoral;
padding: 10px;
border: 1px solid #ccc;
}
.item {
flex: 1 1 100px;
background-color: lightyellow;
padding: 20px;
text-align: center;
border: 1px solid #ccc;
}
</style>
<title>Nested Flexbox Grid</title>
</head>
<body>
<div class="outer-container">
<div class="inner-container">
<div class="item">Item A</div>
<div class="item">Item B</div>
<div class="item">Item C</div>
</div>
<div class="inner-container">
<div class="item">Item D</div>
<div class="item">Item E</div>
<div class="item">Item F</div>
</div>
</div>
</body>
</html>
Output:
Aligning Items
Flexbox provides properties to align and justify items within the container. We can use align-items, justify-content, and other alignment properties to change the positioning of grid items.
- Create the basic structure of the web page using <div> elements for the container and items in the HTML document.
- Set the container's display property to flex and enable wrapping with flex-wrap: wrap.
- Use the align-items and justify-content properties to control the alignment and spacing of the items.
Syntax:
.container {
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center; /* Align items vertically */ justify-content: center; /* Align items horizontally */
}
.item {
flex: 1 1 150px;
}
Example: This demonstrates aligning item within the container using using flexbox grid.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
justify-content: center;
height: 100vh;
background-color: lightgray;
}
.item {
flex: 1 1 150px;
background-color: lightblue;
padding: 20px;
text-align: center;
border: 1px solid #ccc;
}
</style>
<title>Aligning Items in Flexbox Grid</title>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
</body>
</html>
Output:
Similar Reads
How to Create a Masonry grid with flexbox in CSS? 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 cre
3 min read
Complete Guide to CSS Flexbox Flexbox, short for Flexible Box Layout, is a one-dimensional layout method for aligning and distributing space among items in a container. It allows you to design layouts that adapt to different screen sizes, making it ideal for responsive web design.Flex Container: The parent element that holds fle
7 min read
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 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
What is CSS flexbox ? CSS Flexible Layout Box, popularly known as Flexbox is a powerful one-dimensional layout model. It helps to lay, align and distribute items (children) efficiently inside a container (parent). Â Important Features:One-dimensional layout model: Flex is a one-dimensional layout model as it can only deal
15+ min read
React Suite FlexboxGrid Component React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. FlexboxGrid component allows the user to use 24 grids as it is a grid layout component that is implemented via CSS Flexbox. We can use the following approach in
3 min read