Open In App

How to Create a Flexbox Grid?

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:


Next Article

Similar Reads