I'm really confused. When looking for online resources and documentation, most of the documentation of these properties seem to reference Flex-box, not grid. And I don't know how applicable the documentation for the equivalent properties in Flex-box is to grid.
So, I've tried referencing https://css-tricks.com/snippets/css/complete-guide-grid/, which defines them as follows:
justify-items - Aligns the content inside a grid item along the row axis
justify-content - This property aligns the grid along the row axis
justify-self - Aligns the content inside a grid item along the row axis
But I still don't understand what the difference between them is. So, I have 3 questions I want to clarify.
- Is the
justify-items
property in Flex-box the same as thejustify-items
property in Grid? or are they different somehow? (In other words, can I reuse Flex-box documentation for Grid) - What do (justify-)content, self and items do?
- How are (justify-)content, self and items different?
Any clarification would be greatly appreciated.
Edit: Since everyone keeps giving me Flex-box resources, I am asking about css-grid, NOT flex-box.
Key differences between
justify-content
,justify-items
andjustify-self
in CSS Grid:justify-content
property controls the alignment of grid columns. It is set on the grid container. It does not apply to or control the alignment of grid items.justify-items
property controls the alignment of grid items. It is set on the grid container.justify-self
property overridesjustify-items
on individual items. It is set on grid items and inherits the value ofjustify-items
, by default.Example
Here's a 2x3 grid.
The container is:
justify-content
The
justify-content
property aligns columns within the container.With
justify-content: space-between
both columns are pinned to the edges. The grid items shift only because they exist inside those columns. They are otherwise unaffected.Note that this property works only when there is free space in the container. If any of the columns were sized with
fr
, then all free space would be consumed, andjustify-content
would have no effect.justify-items
The
justify-items
property aligns grid items within their tracks (not the entire container)With
justify-items: center
the grid items are centered within their columns.justify-self
The
justify-self
property overridesjustify-items
on individual items.align-content
,align-items
andalign-self
These properties do the same as their
justify-*
counterparts, but in the perpendicular direction.More here: What is the difference between align-items vs. align-content in Grid Layout?
Spec Reference
CSS Box Alignment Module
You wrote:
Although the Flex and Grid specs provide their own definitions for keyword alignment properties, such as
justify-items
andalign-content
, the W3C is in the process of phasing out alignment properties for individual box models and implementing their new Box Alignment Module, which seeks to define a set of alignment properties for use across all box models.From the flexbox spec:
There are similar references in the Grid spec.
OK, I think I figured it out thanks to Michael_B. My confusion came from the fact that sometimes different properties would randomly not change anything about the layout of the grid.
justify-content allows you to position the grid within it's grid container. This is why the justify-content property will have no effect if the grid-container is the same size as the grid. (Which is always the case if you use fr units). This is also why it can have the values: space-around, space-between and space-evenly (In addition to start, end, centre and stretch), which will break up the grid and place the grid items within the grid container. This is a property of the grid container.
justify-items allows you to set a default position for content placed in the grid's grid items (A grid item being a box in the grid, as defined in Michael_B's post). This is a property of the grid container.
justify-self allows you to override the default position of content in an individual cell. This will override the position set by justify-items. Hence, if you use justify-self on all children of the container, setting justify-items on the grid container will have no effect. This is a property of the content inside a grid item.
Note: If you make a grid-item a grid itself, (In other words, the content inside a grid item is a grid) then you can position it within the outer grid item using either the justify-self property or the justify-content property on the inner grid's grid container, since the inner-grid's grid container is one of the outer grid's grid items's content.
As you might expect, all of this also applies to the align-* properties.
Please correct me if I got anything wrong
Justify-self is for aligning the content's position within its cell horizontally.
While align-self is for aligning the content's position within its cell vertically.
Here is the result for aligning the items using
justify-self: start;
THE RESULT FOR THE CODE
justify-self: start;
To answer your questions:
1
As reiallenramos mentioned, "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." - MDN
2-3
This screen shot from W3 does an excellent job of showing what they do and the differences between them.
Good To Knows:
For more information and example, I would suggest you check out:
And for some inspiration: