Summary
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 implement yet.
Initial value | flex-start |
---|---|
Applies to | flex containers |
Inherited | no |
Media | visual |
Computed value | as specified |
Animatable | no |
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; /* 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> </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; }
Results in:
Specifications
Specification | Status | Comment |
---|---|---|
CSS Flexible Box Layout Module The definition of 'justify-content' in that specification. |
Last Call Working Draft | Initial definition |
Browser compatibility
Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 18.0 (18.0) (behind a pref) [1] 20.0 (20.0) |
21.0-webkit | 11 | 12.10 | 9 |
Feature | Firefox Mobile (Gecko) | Android | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | ? | ? | No support | 12.10 | ? |
[1] Firefox supports only single-line flexbox until Firefox 28. To activate flexbox support, for Firefox 18 and 19, the user has to change the about:config preference "layout.css.flexbox.enabled" to true
.