I've read through A complete guide to Grid, but still confused with the differences between two sets of container properties, namely the "justify/align-items
" vs. "justify/align-content
".
My confusion revolves around the claim made by the author that the "-content
" set are there because
Sometimes the total size of your grid might be less than the size of its grid container
I think this applies to both, not unique to the "-content
" set.
Could someone help explain this? Preferably using some graphical illustration as examples.
The align-content property determines how flex lines are aligned along the cross-axis while the align-items property determines how flex items are aligned within a flex line and along the cross-axis.
The align-content property is a sub-property of the Flexible Box Layout module. It helps to align a flex container's lines within it when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.
The align-self applies to all flex items, allows this default alignment to be overridden for individual flex items. The align-content property only applies to multi-line flex containers, and aligns the flex lines within the flex container when there is extra space in the cross-axis.
To align the item horizontally within the grid, we use the justify-content property and set it to center . With justify-content we can align the columns start , end , stretch or center .
Let's start with clarifying the terminology:
The grid container is the overall container for the grid and grid items. It establishes the grid formatting context (as opposed to another formatting context, such as flex or block).
The grid is a group of intersecting vertical and horizontal lines that divides the grid container’s space into grid areas, which are boxes that contain grid items.
Grid items are boxes in a grid container that represent in-flow content (i.e., content that is not absolutely positioned).
Here's an illustration from the W3C:
The justify-content
and align-content
properties align the grid.
The justify-self
, justify-items
, align-self
and align-items
properties align the grid items.
With regard to the problem described in your question:
My confusion revolves around the claim made by the author that the "
-content
" set are there because: "Sometimes the total size of your grid might be less than the size of its grid container"
Well, you can see in the illustration that the grid is smaller than the grid container.
As a result, there is space remaining and the container is able to distribute this space to vertically center (align-content: center
) and right-align (justify-content: end
) the grid.
The extra space could also allow the grid to be spaced apart with values such as space-around
, space-between
and space-evenly
.
However, if the grid size equaled the container size, then there would be no free space, and align-content
/ justify-content
would have no effect.
Here's more from the spec:
10.3. Row-axis Alignment: the
justify-self
andjustify-items
propertiesGrid items can be aligned in the inline dimension by using the
justify-self
property on the grid item orjustify-items
property on the grid container.10.4. Column-axis Alignment: the
align-self
andalign-items
propertiesGrid items can also be aligned in the block dimension (perpendicular to the inline dimension) by using the
align-self
property on the grid item oralign-items
property on the grid container.10.5. Aligning the Grid: the
justify-content
andalign-content
propertiesIf the grid’s outer edges do not correspond to the grid container’s content edges (for example, if no columns are flex-sized), the grid tracks are aligned within the content box according to the
justify-content
andalign-content
properties on the grid container.(emphasis added)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With