Open In App

How to Fix Justify-content Property Not Working in CSS Flexbox?

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

The CSS Flexbox is a powerful layout module that allows for the efficient alignment and distribution of the space among items in a container. One of its core properties justify-content is used to align flex items along the main axis. However, there are instances where the justify-content property might not work as expected. This article will explore the possible reasons and solutions for such issues.

Some Common Issues and Solutions:

Introduction to Flexbox and Justify-Content

The Flexbox or the Flexible Box Layout provides a more efficient way to design layouts by aligning and distributing space within a container. The justify-content property is crucial in controlling the alignment of the flex items along the main axis.

Let's start with a simple example where the justify-content property is not working as expected.

Example: This example shows the justify-content property is not working as expected.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <div class="box">1</div>
        <div class="box">2</div>
        <div class="box">3</div>
    </div>
</body>
</html>
CSS
.container {
    display: flex;
    justify-content: center;
    height: 100vh;
    background-color: lightgrey;
}

.box {
    width: 100px;
    height: 100px;
    background-color: teal;
    margin: 10px;
}

Output:

IMAGE_1

In the above example, the justify-content: center property should center the flex items horizontally within the .container. However, if it doesn't work here are the some common reasons and solutions.

Common justify-content values

  • flex-start: The Aligns items to the start of the container.
  • flex-end: The Aligns items to the end of the container.
  • center: The Centers items in the container.
  • space-between: The Distributes items evenly with the first item at the start and the last item at the end.
  • space-around: The Distributes items evenly with the equal space around them.
  • space-evenly: The Distributes items with the equal space between them.

Incorrect Parent Container Display Property

Problem: For justify-content to work the parent container must have the display: flex or display: inline-flex property.

Issue:

.container {
/* Incorrect: Missing display property */
justify-content: center;
border: 1px solid #000;
height: 100px;
}
.item {
background: lightblue;
padding: 10px;
margin: 5px;

Solution: In this example setting display: flex on the .container ensures that the justify-content: center property centers the flex items.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Flexbox Example</title>
    <style>
        .container {
            display: flex;
            justify-content: center; /* or any other justify-content value */
        }
        
        .item {
            background-color: lightblue;
            padding: 10px;
            margin: 5px;
        }
    </style>
</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:

a

Flex Direction Misalignment

Problem: The justify-content property aligns items along the main axis. If the flex direction is set incorrectly the alignment may not work as intended.

Issue:

.container {
display: flex;
flex-direction: column; /* Incorrect direction for horizontal alignment */
justify-content: center;
border: 1px solid #000;
height: 200px;
}
.item {
background: lightcoral;
padding: 10px;
margin: 5px;
}

Solution: By ensuring that the flex-direction is set to the row (the default) the justify-content: center property will correctly center the items along the horizontal axis.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Example</title>
    <style>
        .container {
            display: flex;
            flex-direction: row; /* Default is row */
            justify-content: center;
        }

        .item {
            background-color: lightblue;
            padding: 10px;
            margin: 5px;
        }
    </style>
</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 :

a

Insufficient Space in the Container

Problem: If the container does not have enough space to the accommodate the items and space distribution justify-content may not show the desired effect.

Issue:

.container {
display: flex;
justify-content: space-between;
width: 100px; /* Insufficient width for items */
border: 1px solid #000;
}
.item {
background: lightgreen;
padding: 10px;
margin: 5px;
}

Solution: Ensuring the container has enough width allows the justify-content: space-between property to the distribute the items evenly with the space between them.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Example</title>
    <style>
        .container {
            display: flex;
            justify-content: space-between;
            width: 100%; /* or a suitable width */
        }

        .item {
            background-color: lightblue;
            padding: 10px;
            margin: 5px;
        }
    </style>
</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 :

a

Flex Items Taking Full Width

Problem: If flex items are set to the take the full width of the container justify-content won't have any visible effect.

Issue:

.container {
display: flex;
justify-content: space-around;
border: 1px solid #000;
}
.item {
width: 100%; /* Flex items taking full width of container */
background: lightpink;
padding: 10px;
margin: 5px;
}

Solution: By setting the flex items' width to the 20%, the justify-content: space-around property distributes the items evenly with the space around them.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Example</title>
    <style>
        .container {
            display: flex;
            justify-content: space-around;
        }

        .item {
            background-color: lightblue;
            padding: 10px;
            margin: 5px;
            width: 20%; /* Ensure items do not take full width */
        }
    </style>
</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:

a

Nested Flex Containers

Problem: If there are nested flex containers ensure that justify-content is applied to the correct container.

Issue:

.outer-container {
display: flex;
justify-content: center; /* Applied to outer container */
border: 1px solid #000;
height: 150px;
}
.inner-container {
display: flex;
justify-content: space-between; /* Applied to inner container */
border: 1px solid #000;
width: 200px;
}
.item {
background: lightgray;
padding: 10px;
}

Solution: In this example, justify-content: center is applied to the outer container and justify-content: space-between is applied to the inner container ensuring the proper alignment within the nested structures.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flexbox Example</title>
    <style>
        .outer-container {
            display: flex;
            justify-content: center;
        }

        .inner-container {
            display: flex;
            justify-content: space-between;
        }

        .item {
            background-color: lightblue;
            padding: 10px;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="outer-container">
        <div class="inner-container">
            <div class="item">Item 1</div>
            <div class="item">Item 2</div>
        </div>
    </div>
</body>
</html>

Output:

a

Conclusion

The justify-content property in CSS Flexbox is essential for the aligning items along the main axis of the container. The Common issues such as the incorrect parent container properties misaligned flex direction insufficient container space full-width flex items and nested flex containers can prevent it from the working correctly. By addressing these issues and following best practices we can ensure that justify-content works as expected providing the desired alignment for the flex items.


Next Article

Similar Reads