Open In App

Difference Between Justify-Content and Align-Items In CSS

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

In CSS, "justify-content" aligns items along the main axis (usually horizontal), controlling space distribution within a container. In contrast, "align-items" aligns items along the cross-axis (usually vertical), managing how items are placed relative to each other within the container. Both are key for arranging items in Flexbox and Grid layouts.

CSS Justify-Content

The justify-content CSS property aligns the items of the flexible container when the items do not use all the space available on the horizontal or main axis.

Syntax:

justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly | initial | inherit;

Property values:

Property value

Description

flex-start

Items are positioned at the beginning of the container

flex-end

Items are positioned at the end of the container

center

Items are positioned in the center of the container

space-between

Items are positioned such that they will have space between them

space-around

Items are positioned such that they will have space before, between, and after them

space-evenly

Items are positioned such that they will have equal space around them

initial

Sets this property to its default value

inherit

Inherits this property from its parent element

Note: The default value for the justify-content property is flex-start.

Example: Explanation of Justify-Content through an example

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .container {
            width: 400px;
            height: 100px;
            border: 1px solid #c3c3c3;
            display: flex;
        }

        .container>div {
            width: 70px;
            height: 70px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
        }

        .center {
            justify-content: center;
        }

        .start {
            justify-content: flex-start;
        }

        .end {
            justify-content: flex-end;
        }

        .space-between {
            justify-content: space-between;
        }

        .space-around {
            justify-content: space-around;
        }
    </style>
</head>

<body>
    <h2> Different Alignments with the CSS justify-content Property </h2>
    <p> The following examples demonstrate 
      different values for the "justify-content" property: </p>
    <div class="container center">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> justify-content: center </b> aligns items at 
          the center of the container. </p>
    <div class="container start">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> justify-content: flex-start </b> aligns items
           at the start of the container. </p>
    <div class="container end">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> justify-content: flex-end </b> aligns
           items at the end of the container. </p>
    <div class="container space-between">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> justify-content: space-between </b> distributes
           items evenly with space between them. </p>
    <div class="container space-around">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> justify-content: space-around </b> distributes 
           items evenly with space around them. </p>
</body>

</html>

Output:

Screenshot-2024-08-06-093443
Example For Justify-Content

CSS Align-Items

The align-items CSS property specifies the alignment for the items inside a flexbox or a grid container.

Syntax:

align-items: normal | stretch | positional alignment | flex-start | flex-end | baseline | initial | inherit;

Property values:

In below table, the values that can be set for the align-items CSS property are described briefly.

Property value

Description

normal

For flexbox and grid items, it behaves like 'stretch'. For grid items with a defined block size it behaves like 'start'.

stretch

Items are stretched to fit the container

center

Items are positioned at the center of the container

flex-start

Items are positioned at the beginning of the container

flex-end

Items are positioned at the end of the container

start

Items are positioned at the beginning of their individual grid cells in the direction of the block

end

Items are positioned at the end of their individual grid cells in the direction of the block

baseline

Items are positioned at the baseline of the container

initial

Sets this property to its default value

inherit

Inherits this property from its parent element

Note: The default value for align-items property is normal.

Example: Explanation of Align-Items through an example

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .container {
            width: 400px;
            height: 100px;
            border: 1px solid #c3c3c3;
            display: flex;
        }

        .container>div {
            width: 70px;
            height: 70px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
        }

        .stretch {
            align-items: stretch;
        }

        .center {
            align-items: center;
        }

        .start {
            align-items: flex-start;
        }

        .end {
            align-items: flex-end;
        }

        .baseline {
            align-items: baseline;
        }
    </style>
</head>

<body>
    <h2> Different Alignments with the CSS align-items Property </h2>
    <p> The following examples demonstrate 
        different values for the "align-items" property: </p>
    <div class="container stretch">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> align-items: stretch </b> makes items 
           stretch to fill the container
           along the cross axis. </p>
    <div class="container center">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> align-items: center </b> aligns items at
           the center of the container along the cross axis. </p>
    <div class="container start">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> align-items: flex-start </b> aligns items at the 
          start of the container along the cross axis. </p>
    <div class="container end">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> align-items: flex-end </b> aligns items at the 
          end of the container along the cross axis. </p>
    <div class="container baseline">
        <div style="background-color:coral;">
            1
        </div>
        <div style="background-color:lightblue;">
            2
        </div>
        <div style="background-color:pink;">
            3
        </div>
    </div>
    <p><b> align-items: baseline </b> aligns
           items along their baseline. </p>
</body>

</html>

Output:

Screenshot-2024-08-06-093925
Example For Align-Items

Difference Between Justify-Content and Align-Items

Aspect

justify-content

align-items

Basic function

It aligns the flex items along the main axis.

It aligns the flex items along the cross axis.

Main axis

The main axis is determined by the flex-direction which is horizontal by default.

The cross axis is perpendicular to the main axis.

Default value

flex-start

normal

Possible values

flex-start, flex-end, center, space-between, space-around, space-evenly, initial, inherit

normal, stretch, positional alignment, flex-start, flex-end, baseline, initial, inherit

Impact on layout

It affects the spacing between the items and their alignment on the main axis.

It affects how the items are aligned within their container's height.

For horizontal layout

It aligns the items horizontally if the main axis is horizontal.

It aligns the items vertically if the main axis is horizontal.

For vertical layout

It aligns the items vertically if the main axis is vertical.

It aligns the items horizontally if the main axis is vertical.

To center items

To center items horizontally we can use 'justify-content: center'

To center items vertically we can use 'align-items: center'

Spacing

It distributes the space between items or around them on the main axis.

It does not distribute space. It aligns items to either the start, end or center of the cross axis.

Applies to

It applies to block containers, flex containers and grid containers.

It applies to flex containers and grid containers.

Axis

main/inline

cross/block

Aligns

It aligns the content within element (effectively adjusts the padding)

It aligns the items inside box (controls the child item's 'justify-self: auto')

Control

It controls the alignment of all the items on the main axis.

It controls the alignment of all items on the cross axis.

Conclusion

The CSS properties, the justify-content and the align-items, can be utilized so that the HTML elements can be aligned to the desired position or rather alignment in the browser window. But knowing the difference enables to make more efficient choices to use the CSS properties more effectively.


Next Article
Article Tags :

Similar Reads