CSS Grid
Horizontal Positioning of Elements with
justify-items and justify-content
Horizontal alignment of items within a CSS Grid is controlled with the CSS properties justify-items and justify-content.
- The property justify-items can have any of the following four values:
- The property justify-content can have any of the following three values:
HTML
CSS
justify-items: start;
Left cell
Middle cell
Right cell
HTML
CSS
justify-items: center;
Left cell
Middle cell
Right cell
HTML
CSS
justify-items: end;
Left cell
Middle cell
Right cell
HTML
CSS
justify-items: stretch;
Note:
grid-gap property defines the space between cells.
Effect: Each item in the grid occupies the full width available, with the contents left-aligned.
Left cell
Middle cell,
centre-justified
Right cell,
right-justified
Make grid items occupy the full available width
and center or right-justify their contents
HTML
CSS
justify-items: stretch;
Notes: you must define classes for the nested divs.
Notes: you must add nested divs within the middle and right cells
Left cell
Middle cell
Right cell
HTML
CSS
justify-content: space-evenly;
Notes: grid-gap is not used and column-widths are defined such that the total width of all the columns is less than the width of the grid itself.
Effect: Equal space between elements and at each end of the row.
Left cell
Middle cell
Right cell
HTML
CSS
justify-content: space-between;
Note: grid-gap is not used and column-widths are defined such that the total width of all the columns is less than the width of the grid itself.
Effect: Equal space between elements with no space at either end of the row.
Left cell
Middle cell
Right cell
HTML
CSS
justify-content: space-around;
Notes: grid-gap is not used and column-widths are defined such that the total width of all the columns is less than the width of the grid itself.
Effect: Equal space between elements with half that space at each end of the row.