The Box Alignment Specification details how alignment works in various layout methods, on this page we explore how Box Alignment works in the context of CSS Grid Layout.
As this page aims to detail things which are specific to CSS Grid Layout and Box Alignment, it should be read in conjunction with the main Box Alignment page which details the common features of Box Alignment across layout methods.
In this example using Grid Layout, there is extra space in the Grid Container after laying out the fixed width tracks on the Inline (Main) Axis. This space is distributed using
justify-content. On the Block (Cross) Axis the alignment of the items inside their grid areas is controlled with
align-items. The first item overrides the
align-items value set on the group by setting
As a two dimensional layout method, when working with Grid Layout we always have two axes on which to align out items and therefore have access to all of the Box Alignment properties in order to do so.
The Inline Axis is the axis that corresponds to the direction that words in a sentence would run in the Writing Mode used. Therefore in a horizontal language such as English or Arabic the inline direction runs horizontally. Should you be in a Vertical Writing Mode the inline axis would run vertically.
To align things on the inline axis you use the properties that start with
The Block Axis cross the inline axis in the direction that blocks are displayed down the page - for example paragraphs in English are displayed one below the other vertically. This, therefore is the Block dimension.
To align things on the block axis you use the properties that start with
These properties deal with aligning the item inside the Grid Area it is placed into. The properties
justify-items are applied to the Grid Container and set the
justify-self properties as a group. This means that you can set alignment for all of your Grid Items at once, then override any items that need a different alignment by applying the
justify-self property to the rules for the individual Grid Item.
The initial value for
stretch so the item will stretch over the entire Grid Area. The exception to this rule is where the item has an intrinsic aspect ratio, for example an image. In this case the item will be aligned to
start in both dimensions in order that the image is not distorted.
These properties deal with aligning the tracks of the grid when there is extra space to distribute. This scenario would occur if the tracks that you have defined total less than the total width of the Grid Container.
Gap and legacy grid-gap properties
The Grid specification originally contained the definition for the properties
grid-gap. These have since been moved into the Box Alignment Specification and renamed to
gap. This allows them to be used for other layout methods where a gap between items makes sense.
At present only Microsoft Edge supports the unprefixed properties, therefore to use the gap properties in grid layout you should use the
grid-gap versions to ensure full compatibility. You could double up the properties and use both as you would for vendor prefixes.