W3cubDocs

/CSS

justify-content

The CSS justify-content property defines how the browser distributes space between and around flex items along the main-axis of their container.

The alignment is done after the lengths and auto margins are applied, meaning that, if there is at least one flexible element, with flex-grow different from 0, it will have no effect as there won't be any available space.

Do not assume that this property will only apply on flex containers in the future and therefore do not simply hide it by setting another display value. The CSSWG is working into extending its usage to any block element. The draft specification is still in its very early stage and isn't implemented yet.

Initial value flex-start
Applies to flex containers
Inherited no
Media visual
Computed value as specified
Animation type discrete
Canonical order the unique non-ambiguous order defined by the formal grammar

See Using CSS flexible boxes for more properties and information.

Syntax

/* Pack flex items from the start */
justify-content: flex-start;

/* Pack items from the end */
justify-content: flex-end;

/* Pack items around the center */ 
justify-content: center;

/* Distribute items evenly
The first item at the start, the last at the end */
justify-content: space-between;

/* Distribute items evenly
Items have equal space around them */
justify-content: space-around;

/* Distribute items evenly
Items have a half-size space on either end */
justify-content: space-evenly;

/* Global values */
justify-content: inherit;
justify-content: initial;
justify-content: unset;

Values

flex-start
The flex items are packed starting from the main-start. Margins of the first flex item is flushed with the main-start edge of the line and each following flex item is flushed with the preceding.
flex-end
The flex items are packed starting from the main-end. The margin edge of the last flex item is flushed with the main-end edge of the line and each preceding flex item is flushed with the following.
center
The flex items are packed toward the center of the line. The flex items are flushed with each other and aligned in the center of the line. Space between the main-start edge of the line and first item and between main-end and the last item of the line is the same.
space-between
Flex items are evenly distributed along the line. The spacing is done such as the space between two adjacent items is the same. Main-start edge and main-end edge are flushed with respectively first and last flex item edges.
space-around
Flex items are evenly distributed so that the space between two adjacent items is the same. The empty space before the first and after the last items equals half of the space between two adjacent items.

Formal syntax

flex-start | flex-end | center | space-between | space-around

Examples

HTML Content

<div id="container">
  <p>justify-content: flex-start</p>
  <div id="flex-start">
    <div></div>
    <div></div>
    <div></div>
  </div>

  <p>justify-content: flex-end</p>
  <div id="flex-end">
    <div></div>
    <div></div>
    <div></div>
  </div>

  <p>justify-content: center</p>
  <div id="center">
    <div></div>
    <div></div>
    <div></div>
  </div>

  <p>justify-content: space-between</p>
  <div id="space-between">
    <div></div>
    <div></div>
    <div></div>
  </div>

  <p>justify-content: space-around</p>
  <div id="space-around">
    <div></div>
    <div></div>
    <div></div>
  </div>

<p>justify-content: space-evenly</p>
  <div id="space-evenly">
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

CSS

#container > div {
  display: flex;
  font-family: "Courier New", "Lucida Console", monospace;
}

#container > div > div {
  width: 50px;
  height: 50px;
  background: linear-gradient(-45deg, #788cff, #b4c8ff);
}

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

#center {
  justify-content: center;
}

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

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

#space-around {
  justify-content: space-around;
}

#space-evenly {
  justify-content: space-evenly;
}

Results in:

Specifications

Specification Status Comment
CSS Flexible Box Layout Module
The definition of 'justify-content' in that specification.
Candidate Recommendation Initial definition
CSS Box Alignment Module
The definition of 'justify-content' in that specification.
Working Draft Initial definition

Browser compatibility

Feature Firefox (Gecko) Chrome Edge Internet Explorer Opera Safari
Basic support 18.0 (18.0)[1]
20.0 (20.0)[2]
21.0 -webkit
29.0[3]
(Yes)-webkit
(Yes)
11 12.10 9
‘space-evenly’ 52.0 (52.0) No ? No ? ?
Feature Firefox Mobile (Gecko) Android Android Webview Edge IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support ? ? (Yes)[3] (Yes)-webkit
(Yes)
No support 12.10 ? (Yes)[3]

[1] Firefox supports only single-line flexbox until Firefox 27. To activate flexbox support, for Firefox 18 and 19, the user has to change the about:config preference layout.css.flexbox.enabled to true.

[2] In addition to the unprefixed support, Gecko 48.0 (Firefox 48.0 / Thunderbird 48.0 / SeaMonkey 2.45) added support for a -webkit prefixed version of the property for web compatibility reasons behind the preference layout.css.prefixes.webkit, defaulting to false. Since Gecko 49.0 (Firefox 49.0 / Thunderbird 49.0 / SeaMonkey 2.46) the preference defaults to true.

[3] Older versions of the spec treat absolute positioned children as though they are a 0 by 0 flex item. Later spec versions take them out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.

See also

© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content