Menu

CSS Grid Justify-Content vs Align-Content: The Difference Explained
📰

CSS Grid Justify-Content vs Align-Content: The Difference Explained

DEV Community: css·kandz·about 15 hours ago
#zG00edoM
#devtocss#devto#dev#grid#align
Reading 0:00
15s threshold

CSS Grid justify-content is confusing.

You set it and nothing changes. You realize it only works if the grid tracks don't fill the container. You mix it up with align-content .

Let's break down how alignment actually works in CSS Grid.

The Core Issue: Empty Space

Both justify-content and align-content do the same thing: they distribute empty space in a container.

If your grid tracks take up 100% of the container's width or height, there is no empty space. Therefore, these properties do nothing.

You only see the effect when your grid tracks are smaller than the container.

Justify-Content (The Horizontal Axis)

justify-content aligns the grid columns (tracks) along the inline (horizontal) axis.

.container { display : grid ; grid-template-columns : 100px 100px ; /* Only 200px wide */ width : 400px ; /* Container is 400px wide */ justify-content : center ; }

Enter fullscreen mode

Exit fullscreen mode

Because there is 200px of empty space, justify-content: center pushes the columns to the…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.