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.
/* 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;
flex-start
flex-end
center
space-between
space-around
flex-start | flex-end | center | space-between | space-around
<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>
#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:
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 |
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.
© 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