Consider the main axis and cross axis of a flex container:
Source: W3C
To align flex items along the main axis there is one property:
To align flex items along the cross axis there are three properties:
In the image above, the main axis is horizontal and the cross axis is vertical. These are the default directions of a flex container.
However, these directions can be easily interchanged with the flex-direction
property.
/* main axis is horizontal, cross axis is vertical */
flex-direction: row;
flex-direction: row-reverse;
/* main axis is vertical, cross axis is horizontal */
flex-direction: column;
flex-direction: column-reverse;
(The cross axis is always perpendicular to the main axis.)
My point in describing how the axes' work is that there doesn't seem to be anything special about either direction. Main axis, cross axis, they're both equal in terms of importance and flex-direction
makes it easy to switch back and forth.
So why does the cross axis get two additional alignment properties?
Why are align-content
and align-items
consolidated into one property for the main axis?
Why does the main axis not get a justify-self
property?
Scenarios where these properties would be useful:
placing a flex item in the corner of the flex container
#box3 { align-self: flex-end; justify-self: flex-end; }
making a group of flex items align-right (justify-content: flex-end
) but have the first item align left (justify-self: flex-start
)
Consider a header section with a group of nav items and a logo. With justify-self
the logo could be aligned left while the nav items stay far right, and the whole thing adjusts smoothly ("flexes") to different screen sizes.
in a row of three flex items, affix the middle item to the center of the container (justify-content: center
) and align the adjacent items to the container edges (justify-self: flex-start
and justify-self: flex-end
).
Note that values space-around
and space-between
on
justify-content
property will not keep the middle item centered about the container if the adjacent items have different widths.
#container {_x000D_
display: flex;_x000D_
justify-content: space-between;_x000D_
background-color: lightyellow;_x000D_
}_x000D_
.box {_x000D_
height: 50px;_x000D_
width: 75px;_x000D_
background-color: springgreen;_x000D_
}_x000D_
.box1 {_x000D_
width: 100px;_x000D_
}_x000D_
.box3 {_x000D_
width: 200px;_x000D_
}_x000D_
#center {_x000D_
text-align: center;_x000D_
margin-bottom: 5px;_x000D_
}_x000D_
#center > span {_x000D_
background-color: aqua;_x000D_
padding: 2px;_x000D_
}
_x000D_
<div id="center">_x000D_
<span>TRUE CENTER</span>_x000D_
</div>_x000D_
_x000D_
<div id="container">_x000D_
<div class="box box1"></div>_x000D_
<div class="box box2"></div>_x000D_
<div class="box box3"></div>_x000D_
</div>_x000D_
_x000D_
<p>note that the middlebox will only be truly centered if adjacent boxes are equal width</p>
_x000D_
As of this writing, there is no mention of justify-self
or justify-items
in the flexbox spec.
However, in the CSS Box Alignment Module, which is the W3C's unfinished proposal to establish a common set of alignment properties for use across all box models, there is this:
Source: W3C
You'll notice that justify-self
and justify-items
are being considered... but not for flexbox.
I'll end by reiterating the main question:
Why are there no "justify-items" and "justify-self" properties?
This question is related to
css
flexbox
language-lawyer
w3c
This was asked on the www-style list, and Tab Atkins (spec editor) provided an answer explaining why. I'll elaborate on that a bit here.
To start out, let's initially assume our flex container is single-line (flex-wrap: nowrap
). In this case, there's clearly an alignment difference between the main axis and the cross axis -- there are multiple items stacked in the main axis, but only one item stacked in the cross axis. So it makes sense to have a customizeable-per-item "align-self" in the cross axis (since each item is aligned separately, on its own), whereas it doesn't make sense in the main axis (since there, the items are aligned collectively).
For multi-line flexbox, the same logic applies to each "flex line". In a given line, items are aligned individually in the cross axis (since there's only one item per line, in the cross axis), vs. collectively in the main axis.
Here's another way of phrasing it: so, all of the *-self
and *-content
properties are about how to distribute extra space around things. But the key difference is that the *-self
versions are for cases where there's only a single thing in that axis, and the *-content
versions are for when there are potentially many things in that axis. The one-thing vs. many-things scenarios are different types of problems, and so they have different types of options available -- for example, the space-around
/ space-between
values make sense for *-content
, but not for *-self
.
SO: In a flexbox's main axis, there are many things to distribute space around. So a *-content
property makes sense there, but not a *-self
property.
In contrast, in the cross axis, we have both a *-self
and a *-content
property. One determines how we'll distribute space around the many flex lines (align-content
), whereas the other (align-self
) determines how to distribute space around individual flex items in the cross axis, within a given flex line.
(I'm ignoring *-items
properties here, since they simply establish defaults for *-self
.)
I know this is not an answer, but I'd like to contribute to this matter for what it's worth. It would be great if they could release justify-self
for flexbox to make it truly flexible.
It's my belief that when there are multiple items on the axis, the most logical way for justify-self
to behave is to align itself to its nearest neighbours (or edge) as demonstrated below.
I truly hope, W3C takes notice of this and will at least consider it. =)
This way you can have an item that is truly centered regardless of the size of the left and right box. When one of the boxes reaches the point of the center box it will simply push it until there is no more space to distribute.
The ease of making awesome layouts are endless, take a look at this "complex" example.
I just found my own solution to this problem, or at least my problem.
I was using justify-content: space-around
instead of justify-content: space-between;
.
This way the end elements will stick to the top and bottom, and you could have custom margins if you wanted.
The justify-self
and justify-items
properties are not implemented in flexbox. This is due to the one-dimensional nature of flexbox, and that there may be multiple items along the axis, making it impossible to justify a single item. To align items along the main, inline axis in flexbox you use the justify-content
property.
Reference: Box alignment in CSS Grid Layout
I know this doesn't use flexbox, but for the simple use-case of three items (one at left, one at center, one at right), this can be accomplished easily using display: grid
on the parent, grid-area: 1/1/1/1;
on the children, and justify-self
for positioning of those children.
<div style="border: 1px solid red; display: grid; width: 100px; height: 25px;">_x000D_
<div style="border: 1px solid blue; width: 25px; grid-area: 1/1/1/1; justify-self: left;"></div>_x000D_
<div style="border: 1px solid blue; width: 25px; grid-area: 1/1/1/1; justify-self: center;"></div>_x000D_
<div style="border: 1px solid blue; width: 25px; grid-area: 1/1/1/1; justify-self: right;"></div>_x000D_
</div>
_x000D_
Source: Stackoverflow.com