CSS justify-content Property

The CSS justify-content property controls how available space is distributed between and around items along the main axis of a flex container or the inline axis of a grid container.

It is commonly used to center navigation links, space buttons evenly, align flex items at either end of a container, and position grid tracks when their combined size is smaller than the grid container. The property also applies to multicolumn layouts, where its behavior depends on the available space and column sizing.

justify-content Syntax

The justify-content property accepts positional alignment, distributed alignment, and supported overflow alignment values.

Basic Syntax

selector {
  justify-content: center;
}

Positional Alignment

.container {
  justify-content: start;
}
.container {
  justify-content: end;
}
.container {
  justify-content: center;
}
.container {
  justify-content: flex-start;
}
.container {
  justify-content: flex-end;
}

Distributed Alignment

.container {
  justify-content: space-between;
}
.container {
  justify-content: space-around;
}
.container {
  justify-content: space-evenly;
}
.container {
  justify-content: stretch;
}

Physical Alignment

.container {
  justify-content: left;
}
.container {
  justify-content: right;
}

Overflow Alignment

.container {
  justify-content: safe center;
}
.container {
  justify-content: unsafe center;
}

CSS-Wide Keywords

.container {
  justify-content: initial;
}
.container {
  justify-content: inherit;
}
.container {
  justify-content: unset;
}
.container {
  justify-content: revert;
}
.container {
  justify-content: revert-layer;
}

The initial value is normal. Its resulting alignment depends on the layout mode.

Property Values

The following values control how content is aligned or how available space is distributed.

Value Description
normal Uses the default alignment behavior for the layout mode.
start Aligns content toward the start of the relevant alignment axis.
end Aligns content toward the end of the relevant alignment axis.
flex-start Aligns flex items toward the main-start edge. Outside flex layout, it behaves like start.
flex-end Aligns flex items toward the main-end edge. Outside flex layout, it behaves like end.
center Centers content along the relevant axis.
space-between Distributes available space between items, with no additional space at the outer edges.
space-around Distributes space around each item, producing outer spaces half the size of the spaces between items.
space-evenly Distributes equal space between items and at both outer edges.
stretch Stretches eligible auto-sized items along the alignment axis. In flex layout, it behaves like flex-start.
left Aligns content toward the physical left side where applicable.
right Aligns content toward the physical right side where applicable.
safe Modifies positional alignment to avoid certain overflow situations.
unsafe Allows the requested positional alignment even when it causes overflow.
initial Resets the property to normal.
inherit Uses the computed value from the parent element.
unset Resets the property to normal because it is not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value normal
Inherited No
Applies to Flex containers, grid containers, and multicolumn containers
Computed value As specified
Animation type Discrete
Percentages Not applicable
Flexbox axis Main axis
Grid axis Inline axis

The property does not accept lengths or percentages directly. It distributes space according to the selected alignment keyword.

How It Works

The browser first determines the sizes of the items or tracks. It then calculates the remaining space along the relevant alignment axis.

When positive free space is available, justify-content determines where that space appears. If no free space remains, many alignment values produce no visible change.

Flex Container

.container {
  display: flex;
  justify-content: center;
}

Flex items are centered along the main axis when free space is available.

Grid Container

.container {
  display: grid;
  grid-template-columns: 100px 100px;
  justify-content: center;
}

The grid tracks are centered within the grid container when their combined width is smaller than the available inline size.

Important Consideration

In Flexbox, the property distributes space between flex items. In Grid, it aligns the grid tracks as a group rather than independently aligning each item inside its grid area.

normal Value

The normal value is the initial value. Its behavior depends on the layout context.

Flexbox

.container {
  display: flex;
  justify-content: normal;
}

In flex layout, normal behaves like start.

Grid

.container {
  display: grid;
  grid-template-columns: auto auto;
  justify-content: normal;
}

In grid layout, normal generally behaves like stretch, allowing eligible auto-sized tracks to absorb available space.

Important Consideration

The default behavior is not identical across layout modes. Do not assume normal always produces start alignment.

start and end Values

The start and end values align content toward the logical start or end of the alignment axis.

Start Alignment

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

End Alignment

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

Grid Start Alignment

.container {
  display: grid;
  grid-template-columns: 100px 100px;
  justify-content: start;
}

The grid tracks align toward the inline-start side of the grid container.

Important Consideration

Logical start and end alignment adapt to writing modes and direction. They are not permanently associated with the physical left or right side.

flex-start and flex-end Values

The flex-start and flex-end values align flex items toward the beginning or end of the flex container's main axis.

flex-start

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

flex-end

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

Reversed Flex Direction

.container {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-start;
}

With row-reverse, the main-start side is reversed. In a horizontal left-to-right layout, flex-start therefore aligns items toward the right.

Important Consideration

The flex-start and flex-end values follow the flex main axis, including its direction. Outside flex layout, they behave like start and end.

center Value

The center value places available free space equally before and after the aligned content.

Center Flex Items

.container {
  display: flex;
  justify-content: center;
}

Center Grid Tracks

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  justify-content: center;
}

Center a Navigation Group

.navigation {
  display: flex;
  justify-content: center;
  gap: 20px;
}

Important Consideration

Center alignment does not guarantee that content will fit inside its container. If content is wider than the available space, overflow may occur.

space-between Value

The space-between value distributes available space between items while keeping the first and last items at the opposite ends of the alignment axis.

Flexbox Example

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

Navigation Layout

.navigation {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Grid Example

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  justify-content: space-between;
}

Single Item Behavior

When there is only one item, space-between aligns it toward the start rather than distributing space around it.

Important Consideration

Use gap when a consistent minimum separation is required. space-between distributes remaining space and does not guarantee a particular gap size.

space-around Value

The space-around value distributes free space around each item. The outer spaces are half the size of the spaces between adjacent items.

Flexbox Example

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

Grid Example

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  justify-content: space-around;
}

Single Item Behavior

When only one item is present, the item is centered along the alignment axis.

Important Consideration

The outer spacing is smaller than the spacing between items. Use space-evenly when equal spacing at every location is desired.

space-evenly Value

The space-evenly value distributes available space equally between adjacent items and between the outermost items and the container edges.

Flexbox Example

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

Grid Example

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  justify-content: space-evenly;
}

Button Group

.button-group {
  display: flex;
  justify-content: space-evenly;
  gap: 10px;
}

The gap establishes a minimum separation between buttons. Any additional distributed space is added according to space-evenly.

Important Consideration

Equal distribution applies to available free space. The final visual distances may also include gaps, margins, and item dimensions.

stretch Value

The stretch value distributes available space by increasing the size of eligible auto-sized items along the alignment axis.

Grid Stretching

.container {
  display: grid;
  grid-template-columns: auto auto;
  justify-content: stretch;
}

Eligible auto-sized grid tracks can expand to use the available inline space.

Fixed Grid Tracks

.container {
  display: grid;
  grid-template-columns: 100px 100px;
  justify-content: stretch;
}

Fixed-width tracks do not stretch merely because justify-content is set to stretch.

Flexbox Stretch

.container {
  display: flex;
  justify-content: stretch;
}

In Flexbox, stretch behaves like flex-start. Flex item growth is controlled by properties such as flex-grow.

Important Consideration

Do not confuse justify-content: stretch with align-items: stretch. They operate on different alignment subjects and axes.

left and right Values

The left and right values provide physical alignment rather than logical start or end alignment.

Left Alignment

.container {
  display: grid;
  grid-template-columns: 100px 100px;
  justify-content: left;
}

Right Alignment

.container {
  display: grid;
  grid-template-columns: 100px 100px;
  justify-content: right;
}

Flexbox Example

.container {
  display: flex;
  flex-direction: row;
  justify-content: right;
}

Important Consideration

When the alignment axis is not parallel to the physical left-to-right axis, left and right behave like start. Logical values are generally preferable for interfaces that support multiple writing directions.

safe and unsafe Keywords

The safe and unsafe keywords modify positional alignment behavior when content may overflow its alignment container.

Safe Center Alignment

.container {
  display: flex;
  justify-content: safe center;
}

If centering would cause unsafe overflow, safe alignment can fall back toward start alignment.

Unsafe Center Alignment

.container {
  display: flex;
  justify-content: unsafe center;
}

This requests centering even when the content overflows the available space.

Safe End Alignment

.container {
  display: grid;
  grid-template-columns: 150px 150px;
  justify-content: safe end;
}

Important Consideration

The overflow alignment keywords are used with positional alignment values. They are not general replacements for overflow, and they do not make oversized content shrink to fit.

justify-content in Flexbox

In a flex container, justify-content distributes free space along the main axis after flex item sizes and flexible lengths have been resolved.

Horizontal Flex Layout

.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

With a horizontal writing mode and left-to-right direction, the main axis runs horizontally from left to right.

Vertical Flex Layout

.container {
  display: flex;
  flex-direction: column;
  min-height: 300px;
  justify-content: space-between;
}

With a column direction in horizontal writing, the main axis runs vertically. The first and last items are distributed toward opposite ends of the available height.

Flexible Items

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

Because the items can grow to consume available space, there may be no remaining free space for justify-content to distribute.

Important Consideration

Flex item sizing occurs before main-axis free-space distribution. If the items already fill the container, changing justify-content may appear to have no effect.

justify-content in Grid

In CSS Grid, justify-content aligns the grid tracks along the container's inline axis when their combined size leaves available space.

Center Fixed Tracks

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  justify-content: center;
}

Distribute Grid Tracks

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  justify-content: space-between;
}

Fractional Tracks

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  justify-content: space-between;
}

Fractional tracks typically expand to use the available inline space. When no free space remains, space-between has no additional space to distribute.

Align Individual Grid Items

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  justify-items: center;
}

Use justify-items to align items within their individual grid areas instead of aligning the grid tracks as a group.

Important Consideration

Grid track sizing and content alignment are separate steps. To see the effect of justify-content, use tracks that do not already occupy all available inline space.

Multicolumn Layout

The justify-content property also applies to multicolumn containers. It can distribute available inline space when the column arrangement does not already occupy the full container.

Multicolumn Example

.article {
  columns: 3 150px;
  column-gap: 20px;
  justify-content: space-between;
}

Column Alignment

.article {
  column-width: 180px;
  column-gap: 20px;
  justify-content: center;
}

Important Consideration

Multicolumn alignment depends on column sizing and the space available after column layout. Browser support for applying the full range of justify-content values to multicolumn layouts may differ from Flexbox and Grid.

Flex Direction

The flex-direction property determines the main axis of a flex container. This changes the direction in which justify-content operates.

Row Direction

.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

Column Direction

.container {
  display: flex;
  flex-direction: column;
  min-height: 300px;
  justify-content: center;
}

Row-Reverse Direction

.container {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-start;
}

Column-Reverse Direction

.container {
  display: flex;
  flex-direction: column-reverse;
  min-height: 300px;
  justify-content: flex-end;
}

Important Consideration

For flex layouts, justify-content always operates along the main axis, not necessarily horizontally. Writing mode and direction also affect the physical orientation of that axis.

Wrapping and Multiple Lines

When flex items wrap onto multiple lines, justify-content distributes available space separately within each flex line.

Wrapping Flex Items

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 15px;
}

Center Items on Each Line

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 15px;
}

Align Multiple Flex Lines

.container {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
  justify-content: center;
  min-height: 300px;
}

The align-content property distributes space between flex lines along the cross axis, while justify-content aligns items within each line along the main axis.

Important Consideration

A final flex line containing only one item may align differently under space-between because that line has no pair of items between which to distribute space.

Gaps and Auto Margins

Gaps and auto margins affect how much free space remains for justify-content.

Using gap

.container {
  display: flex;
  justify-content: space-between;
  gap: 20px;
}

The gap property creates a fixed minimum separation between items. Additional free space is distributed according to justify-content.

Using Auto Margins

.navigation {
  display: flex;
  justify-content: flex-start;
}
.navigation .actions {
  margin-inline-start: auto;
}

The auto margin consumes available space and pushes the actions toward the inline-end side in a typical horizontal row layout.

Combining gap and space-evenly

.container {
  display: flex;
  justify-content: space-evenly;
  gap: 12px;
}

Important Consideration

Positive free space is assigned to applicable auto margins before justify-content distributes any remaining space. This can make the alignment declaration appear ineffective.

justify-content vs. align-items

Both properties control alignment, but they operate along different axes in a flex container.

Property Flexbox Behavior
justify-content Aligns and distributes items along the main axis.
align-items Aligns items along the cross axis.

Center Along Both Axes

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 250px;
}

Column Layout

.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 250px;
}

In a typical horizontal writing mode, justify-content centers items vertically in a column flex container, while align-items centers them horizontally.

Important Consideration

Do not associate justify-content permanently with horizontal alignment. Its axis changes with flex-direction.

justify-content vs. justify-items

In Grid, justify-content and justify-items control different alignment subjects.

Property Grid Behavior
justify-content Aligns the grid tracks together within the grid container.
justify-items Sets the default inline-axis alignment of items within their individual grid areas.
justify-self Controls the inline-axis alignment of an individual grid item.

Center the Grid Tracks

.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  justify-content: center;
}

Center Items Within Grid Areas

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  justify-items: center;
}

Combine Both Properties

.container {
  display: grid;
  grid-template-columns: repeat(3, 120px);
  justify-content: center;
  justify-items: center;
}

Important Consideration

Use justify-content for the placement of the grid as a whole and justify-items for the placement of items inside grid areas.

Inheritance

The justify-content property is not inherited. Its initial value is normal.

Parent Alignment

.parent {
  display: flex;
  justify-content: center;
}

A nested flex or grid container does not automatically inherit the parent's justify-content value.

Explicit Inheritance

.child {
  display: flex;
  justify-content: inherit;
}

Reset to Initial Value

.child {
  justify-content: initial;
}

This resets the property to normal.

Using unset

.child {
  justify-content: unset;
}

Because the property is not inherited, unset behaves like initial.

justify-content Example

This example demonstrates the six commonly used Flexbox alignment values. Each container has the same three items, allowing you to compare how available space is distributed.

HTML Example

<div class="justify-content-demo">
  <div class="demo-row">
    <h3>flex-start</h3>
    <div class="demo-container start">
      <div class="demo-item">1</div>
      <div class="demo-item">2</div>
      <div class="demo-item">3</div>
    </div>
  </div>
  <div class="demo-row">
    <h3>flex-end</h3>
    <div class="demo-container end">
      <div class="demo-item">1</div>
      <div class="demo-item">2</div>
      <div class="demo-item">3</div>
    </div>
  </div>
  <div class="demo-row">
    <h3>center</h3>
    <div class="demo-container center">
      <div class="demo-item">1</div>
      <div class="demo-item">2</div>
      <div class="demo-item">3</div>
    </div>
  </div>
  <div class="demo-row">
    <h3>space-between</h3>
    <div class="demo-container between">
      <div class="demo-item">1</div>
      <div class="demo-item">2</div>
      <div class="demo-item">3</div>
    </div>
  </div>
  <div class="demo-row">
    <h3>space-around</h3>
    <div class="demo-container around">
      <div class="demo-item">1</div>
      <div class="demo-item">2</div>
      <div class="demo-item">3</div>
    </div>
  </div>
  <div class="demo-row">
    <h3>space-evenly</h3>
    <div class="demo-container evenly">
      <div class="demo-item">1</div>
      <div class="demo-item">2</div>
      <div class="demo-item">3</div>
    </div>
  </div>
</div>

CSS Example

.justify-content-demo .demo-row {
  margin-bottom: 20px;
}
.justify-content-demo h3 {
  margin: 0 0 8px;
  font-size: 1rem;
}
.justify-content-demo .demo-container {
  display: flex;
  gap: 8px;
  padding: 10px;
  border: 2px solid #2563eb;
  background-color: #eff6ff;
}
.justify-content-demo .demo-item {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 45px;
  height: 45px;
  background-color: #2563eb;
  color: #fff;
  font-weight: bold;
}
.justify-content-demo .start {
  justify-content: flex-start;
}
.justify-content-demo .end {
  justify-content: flex-end;
}
.justify-content-demo .center {
  justify-content: center;
}
.justify-content-demo .between {
  justify-content: space-between;
}
.justify-content-demo .around {
  justify-content: space-around;
}
.justify-content-demo .evenly {
  justify-content: space-evenly;
}
Browser Result

flex-start

1
2
3

flex-end

1
2
3

center

1
2
3

space-between

1
2
3

space-around

1
2
3

space-evenly

1
2
3

The example shows how each value positions the same items differently. The amount of distributed space changes with the width of the container.

Try a Column Layout

.justify-content-demo .demo-container {
  flex-direction: column;
  height: 250px;
}

The alignment values now operate vertically in a typical horizontal writing mode.

Try a Larger Gap

.justify-content-demo .demo-container {
  gap: 20px;
}

The larger gap reduces the remaining free space available for distribution.

Common Mistakes

Using justify-content on a Normal Block Container

.container {
  justify-content: center;
}

For an ordinary block container, this declaration does not center its child elements. Use an appropriate layout mode such as Flexbox or Grid.

Expecting Vertical Alignment in a Row Flex Container

.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

In a typical horizontal writing mode, this centers items horizontally. Use align-items to align them vertically.

Using an Invalid Value

.container {
  justify-content: middle;
}

This is invalid. Use center for center alignment.

Expecting space-between to Create Space Without Free Space

If items already occupy the full available axis, there is no positive free space for space-between to distribute.

Confusing Grid Tracks with Grid Items

In Grid, justify-content aligns the grid tracks as a group. Use justify-items or justify-self for individual item alignment.

Ignoring Auto Margins

Auto margins can consume available space before justify-content is applied.

Assuming start Always Means Left

Logical start alignment depends on writing mode, direction, and the relevant alignment axis.

Expecting stretch to Grow Flex Items

In flex layout, justify-content: stretch behaves like flex-start. Use flex-grow to control flexible growth.

Important Considerations

Keep the following points in mind when using justify-content:

  • The property aligns and distributes content along the relevant alignment axis.
  • In Flexbox, it operates along the main axis.
  • In Grid, it operates along the inline axis and aligns the grid tracks as a group.
  • The initial value is normal.
  • The property is not inherited.
  • It accepts positional and distributed alignment keywords.
  • It does not accept length or percentage values directly.
  • center centers content when free space is available.
  • space-between places distributed space between items.
  • space-around creates smaller outer spaces than inner spaces.
  • space-evenly distributes equal free space at all positions.
  • stretch does not grow flex items along the main axis.
  • flex-direction determines the main axis in Flexbox.
  • Writing mode and direction affect logical alignment.
  • Flex item growth can consume space before alignment occurs.
  • Auto margins can consume available free space.
  • The gap property establishes spacing independently of distributed alignment.
  • Wrapped flex lines are justified separately.
  • Use align-items for cross-axis item alignment in Flexbox.
  • Use justify-items for item alignment within grid areas.
  • The safe keyword can help avoid certain overflow situations.
  • Support for multicolumn alignment can differ from Flexbox and Grid.

Browser Support

The CSS justify-content property is widely supported in modern browsers, including Chrome, Edge, Firefox, Safari, and Opera. Core alignment values are well established in Flexbox and Grid.

Support for certain values and layout-specific behaviors, including overflow alignment keywords and multicolumn alignment, may vary. Check compatibility when using these features in production layouts.

For current compatibility information, visit Can I Use? and search for justify-content.

Specifications

The justify-content property is defined in the CSS Box Alignment Module Level 3 specification.

CSS Box Alignment Module Level 3: justify-content

The specification defines content distribution, positional alignment, and overflow alignment across supported layout modes.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: justify-content