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
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: Example: To understa
2 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 fl
8 min read
How To Animate CSS Grid?
CSS Grid is a powerful layout system built in CSS that can easily create complex and responsive web layouts, it defines a 2D layout system that can precisely control rows and columns, you can place elements within this grid by specifying their positions in rows and columns, unlike Flexbox, which is
6 min read
How to Create a Sticky Footer Using CSS Flexbox?
Creating a sticky footer using CSS Flexbox ensures that the footer remains at the bottom of the viewport or the content and provide a consistent layout experience. In this article we will cover approach to create a sticky footer using CSS Flexbox. ApproachSet the html and body elements to 100% heigh
3 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 single column grid ?
In this article, we will learn how to create a single-column grid. We will be discussing two approaches to the task. Approach 1: Using the jQuery Mobile Grid: jQuery Mobile is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Th
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 de
15+ 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