How to Set Space Between the Flexbox ?

Setting space between Flexbox items involves using properties like justify-content with values like space-between or space-around, and gap, to evenly distribute space between items along the main axis, enhancing layout spacing and alignment in a flexible container.

There are some following approaches:

Table of Content

  • Using the justify-content property.
  • Using the gap property to Set Space

1. Using the justify-content property.

The justify-content property in CSS Flexbox aligns flex items along the main axis. It can distribute space between items with values like flex-start, flex-end, center, space-between, space-around, and space-evenly, controlling the alignment and spacing within a flex container.

 Syntax:

  • The value space-between is used for displaying flex items with space between the lines.
justify-content: space-between;
  • The value space-around is used for displaying flex items with space between, before and after the lines.
justify-content: space-around;

Example: In this example we example demonstrates using justify-content in CSS Flexbox to distribute space between items. space-around creates equal space around items, while space-between places equal space between items.

html
<!DOCTYPE html>
<html>

<head>
    <title>Space between flexbox</title>
    <style>
        .flex2 {
            display: flex;
            justify-content: space-around;
            background-color: green;
        }

        .flex3 {
            display: flex;
            justify-content: space-between;
            background-color: green;
        }

        .flex-items {
            background-color: #f4f4f4;
            width: 100px;
            height: 50px;
            margin: 10px;
            text-align: center;
            font-size: 40px;
        }

        h3 {
            text-align: center;
        }

        .Beginner {
            font-size: 40px;
            text-align: center;
            color: #009900;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="Beginner">w3wiki</div>
    <h3>Space between flexbox</h3>

    <br>
    <b>justify-content: space-around </b>
    <div class="flex2">
        <div class="flex-items">1</div>
        <div class="flex-items">2</div>
        <div class="flex-items">3</div>
    </div>
    <br>
    <b>justify-content: space-between </b>
    <div class="flex3">
        <div class="flex-items">1</div>
        <div class="flex-items">2</div>
        <div class="flex-items">3</div>
    </div>
    <br>

</body>

</html>

Output:

2. Using the gap property to Set Space

The gap property in CSS sets space between Flexbox or Grid items. It’s a shorthand for row-gap and column-gap, making it easy to manage spacing consistently without extra margins or padding, improving layout control and readability.

Syntax:

gap: value;

Example: In this example, we are using the gap property along with the flexbox property to add gap between the individual items.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .flex-container {
            display: flex;
            gap: 20px;
            /* Set the desired spacing between flex items */
        }

        .flex-item {
            background-color: lightblue;
            padding: 10px;
        }

        .Beginner {
            font-size: 40px;

            color: #009900;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="Beginner">w3wiki</div>
    <h3>Using gap property</h3>
    <div class="flex-container">
        <div class="flex-item">Element 1</div>
        <div class="flex-item">Element 2</div>
        <div class="flex-item">Element 3</div>
    </div>
</body>

</html>

Output:



Contact Us