dimanche 26 juin 2016

In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

Consider the main axis and cross axis of a flex container:

enter image description here                                                                                                                        Source: W3C

To align flex items along the main axis there is one property: justify-content

To align flex items along the cross axis there are three properties: align-content, align-items and align-self.

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 (the flexbox way) 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 in relation to the container if the adjacent items have different widths (see demo).


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, they have this:

enter image description here                                                                                                                         Source: W3C

You'll notice that justify-self and justify-items are being considered... but not for flexbox.

(Interestingly, contrary to the table illustration, the actual definition of justify-items in this document says it applies to flex containers.)


I'll end by reiterating the main question:

Why are there no "justify-items" and "justify-self" properties?

Aucun commentaire:

Enregistrer un commentaire