justify-content
align-content
On the parent
display: grid-template-columns: grid-template-areas: grid-gap:
On the child
grid-columns: grid-row:
New values
repeat() fr
display grid-template-columns grid-template-rows grid-template-areas grid-template (shorthand) grid-column-gap grid-row-gap grid-gap ( gap ) place-items ( justify-items | align-items ) place-content (justify-content | align-content) grid-auto-columns grid-auto-rows grid-auto-flow grid
display: inline | block | list-item | inline-list-item | inline-block | flex | inline-flex| grid | inline-grid | table | inline-table | table-row-group | table-header-group | table-footer-group | table-row | table-cell | table-column-group | table-column | table-caption | ruby | ruby-base | ruby-text | ruby-base-container | ruby-text-container | contents | none | flow | flow-root
display: inline-grid;
Declaring display: grid
gives you a one column grid
All the children converted to grid items
grid-template-columns
&
grid-template-rows
Defines the columns and rows of the grid with a space-separated list of values representing the track size
grid-template-columns: none | <track-list> | <auto-track-list> grid-template-rows: none | <track-list> | <auto-track-list>
<track-list> = <line-name>? [ <track-size> | <track-repeat> ]
<track-size>
- can be a length, a percentage, or a fraction of the free space (fr) in
the grid <line-name>
- An ident or stringgrid-template-columns: 200px 1fr max-content minmax(min-content, 1fr);
grid-template-columns: 150px 150px 150px; repeat(3, 150px); 275px repeat(2, 150px); 100px repeat(2, 1fr) 2fr;
grid-template-rows: 150px 150px 150px; repeat(3, 150px); 275px repeat(2, 150px); 100px 1fr 2fr;
Mix units!!!
That's the power of grid! Or,at least one of the powers.
You can name grid lines:
grid-template-columns: [start] 150px 150px 150px [end];
<ident>
When items are placed outside of the tracks defined by
grid-template-rows
,
grid-template-columns
,
and
grid-template-areas
,
implicit grid tracks by added. These properties size those tracks
minmax( min, max )
function
Defines a size range greater than or equal to min and less than or equal to max.
minmax( <size> , <size> )
where <size>
is:
<length> | <percentage> | <flex> | min-content | max-content | auto
Used by:
grid-template-columns: minmax(min-content, 200px) minmax(300px, 1fr) 250px; grid-template-rows: grid-auto-columns: minmax( 100px , 1fr ); grid-auto-rows: minmax( 3em , max-content );
min-max()
functionIntrinsic Size
functionmin-max()
function (again)fr
Fraction Unit
describes a fraction of the available* space
repeat()
repeat notation: repeat(# of repeats, length)
*The fr unit distributes available space, not all space. If a tracks has a large link or image, there's' less distributable space.
auto-fill
valueGlobal (only one value per direction) spacing between grid items:
grid-gap
used in grid, flex, columns.grid-column-gap: 20px; grid-row-gap: 1em; grid-gap: 1em 20px;
Margin works! Feature or bug?
display
propertygrid-template-columns
, and grid-template-rows
grid gap
or grid-column-gap
and
grid-row-gap
Positioning Grid Items
.myItem { grid-row-start: 2; grid-row-end: 4; grid-column-start: 2; grid-column-end: 5; }
.myItem { grid-row: 2 / 4; grid-column: 2 / 5; }
.myItem { grid-area: 2 / 2 / 4 / 5; }
grid-area
shorthandgrid-area: a / b / c / d grid-row-start / grid-column-start / grid-row-end / grid-column-end
grid-area: a / b / c;grid-column-end was omitted
grid-column-start
|| auto
grid-area: a / b;
grid-row-end
and grid-column-end
omitted grid-row-start
|| auto
grid-column-start
|| auto
grid-area: a;Only
grid-row-start
is declaredgrid-row-start
is named, all four longhands are set to that value. Otherwise, set to
auto
grid-column-start grid-column-end grid-column grid-row-start grid-row-end grid-row grid-area
justify-self align-self
grid-template-areas
and
grid-area
Specifies named grid areas.
grid-template-areas: "a b b" "a c d";
article { grid-area: b; }
Our magic layout:
body { grid-template-areas: "header header header" "nav article aside" "footer footer footer"; }
header { grid-area: header; } nav { grid-area: nav; } article { grid-area: article; } aside { grid-area: aside; } footer { grid-area: footer; }
Container Properties
justify-items align-items justify-content align-content grid-auto-columns grid-auto-rows grid-auto-flow grid
Item properties
justify-self align-self
justify-items
propertyjustify-items: normal | stretch | baseline | start | end | center | flex-end | flex-start | legacy | safe | unsafe | left | right | center | self-end | self-start | start | stretch | unset
Aligns items in the inline direction (horizontal)
justify-items: normal | stretch | | [ ? ] | [ legacy || [ left | right | center ] ]
<baseline-position> = baseline | first baseline (start) | last baseline (end)
<overflow-position>? <self-position> = [safe | unsafe]? [right | center | left | start | flex-start | flex-end | end | self-start]aligning all the content within each cell
<overflow-position> = unsafe | safe
safe
If it overflows the alignment container, it aligns as if the alignment mode were start
.
unsafe
No matter the relative sizes overflowing, the alignment value is honored.
legacy || left | center | right
legacy
Value inherits into descendants. Computes to inherit
(if
declared) or normal
(if defaulting) if left | center | right
not present.
align-items
propertyAligns items in the block direction (vertical)
align-items: baseline | center | end | flex-end | flex-start | left | normal | right | safe | self-end | self-start | start | stretch | unsafe
align-self
and
justify-self
.
place-items
propertyplace-items: <align-items> <justify-items
Defines how the items are aligned with respect to the grid if the size of all the items combined is not the same size as the container.
justify-content: baseline | center | end | flex-end | flex-start | left | normal | right | space-around | space-between | space-evenly | start | stretch | safe | unsafe
align-content: baseline | center | end | flex-end | flex-start | left | normal | right | space-between | space-evenly | start | stretch | safe | unsafe
Tip: auto
track sizes (and only auto
track sizes) can be stretched by the
align-content
and justify-content
properties
justify-content
align-content