A Complete Guide to CSS Grid

admin

imageIntroduction to CSS Grid [Introduction to CSS Grid](#aa-introduction-to-css-grid)

CSS Grid Layout (aka “Grid” or “CSS Grid”), is a two-dimensional grid-based layout system that, compared to any web layout system of the past, completely changes the way we design user interfaces.CSS has always been used to layout our web pages, but it’s never done a very good job of it.First, we used tables, then floats, positioning and inline-block, but all of these methods were essentially hacks and left out a lot of important functionality (vertical centering, for instance).

Flexbox is also a very great layout tool, but its one-directional flow has different use cases — and they actually work together quite well! Grid is the very first CSS module created specifically to solve the layout problems we’ve all been hacking our way around for as long as we’ve been making websites.

The intention of this guide is to present the Grid concepts as they exist in the latest version of the specification.So I won’t be covering the out-of-date Internet Explorer syntax (even though you can absolutely

use Grid in IE 11) or other historical hacks.

CSS Grid basics [CSS Grid basics](#aa-css-grid-basics)

As of March 2017, most browsers shipped native, unprefixed support for CSS Grid: Chrome (including on Android), Firefox, Safari (including on iOS), and Opera.Internet Explorer 10 and 11 on the other hand support it, but it’s an old implementation with an outdated syntax.The time to build with grid is now!

To get started you have to define a container element as a grid with

, set the column and row sizes with

display: grid

and

grid-template-columns

, and then place its child elements into the grid with

grid-template-rows

and

grid-column

.Similarly to flexbox, the source order of the grid items doesn’t matter.

Your CSS can place them in any order, which makes it super easy to rearrange your grid with media queries.

Imagine defining the layout of your entire page, and then completely rearranging it to accommodate a different screen width all with only a couple lines of CSS.Grid is one of the most powerful CSS modules ever introduced.

grid-row

Important CSS Grid terminology [Important CSS Grid terminology](#aa-important-css-grid-terminology)

Before diving into the concepts of Grid it’s important to understand the terminology.Since the terms involved here are all kinda conceptually similar, it’s easy to confuse them with one another if you don’t first memorize their meanings defined by the Grid specification.But don’t worry, there aren’t many of them.

Grid Container

The element on which

display: grid is applied.It’s the direct parent of all the grid items.In this example

container is the grid container.

Grid Line

The dividing lines that make up the structure of the grid.

They can be either vertical (“column grid lines”) or horizontal (“row grid lines”) and reside on either side of a row or column.Here the yellow line is an example of a column grid line.

Grid Track

The space between two adjacent grid lines.

You can think of them as the columns or rows of the grid.Here’s the grid track between the second and third-row grid lines.

Grid Area

The total space surrounded by four grid lines.A grid area may be composed of any number of grid cells.Here’s the grid area between row grid lines 1 and 3, and column grid lines 1 and 3.

Grid Item

The children (i.e.

direct descendants) of the grid container.Here the

item elements are grid items, but

sub-item isn’t.

Grid Cell

The space between two adjacent row and two adjacent column grid lines.It’s a single “unit” of the grid.Here’s the grid cell between row grid lines 1 and 2, and column grid lines 2 and 3.

CSS Grid properties [CSS Grid properties](#aa-css-grid-properties)

Properties for the Parent

(Grid Container)

Jump links

display

Defines the element as a grid container and establishes a new grid formatting context for its contents.

Values:

grid– generates a block-level grid

inline-grid– generates an inline-level grid

.container { display: grid | inline-grid; }

The ability to pass grid parameters down through nested elements (aka subgrids) has been moved to

level 2 of the CSS Grid specification.

Here’s a quick explanation.

grid-template-columns

grid-template-rows

Defines the columns and rows of the grid with a space-separated list of values.

The values represent the track size, and the space between them represents the grid line.

Values:– can be a length, a percentage, or a fraction of the free space in the grid using the

unit (

fr

– an arbitrary name of your choosing

.container { grid-template-columns: ……; /* e.g.1fr 1fr minmax(10px, 1fr) 3fr repeat(5, 1fr) 50px auto 100px 1fr */ grid-template-rows: ……; /* e.g.min-content 1fr min-content 100px 1fr max-content */ }

Grid lines are automatically assigned positive numbers from these assignments (-1 being an alternate for the very last row).

But you can choose to explicitly name the lines.

Note the bracket syntax for the line names:

.container { grid-template-columns: [first] 40px [line2] 50px [line3] auto [col4-start] 50px [five] 40px [end]; grid-template-rows: [row1-start] 25% [row1-end] 100px [third-line] auto [last-line]; }

Note that a line can have more than one name.For example, here the second line will have two names: row1-end and row2-start:

.container { grid-template-rows: [row1-start] 25% [row1-end row2-start] 25% [row2-end]; }

If your definition contains repeating parts, you can use the

to streamline things: repeat() notation

.container { grid-template-columns: repeat(3, 20px [col-start]); }

Which is equivalent to this:

.container { grid-template-columns: 20px [col-start] 20px [col-start] 20px [col-start]; }

If multiple lines share the same name, they can be referenced by their line name and count.

.item { grid-column-start: col-start 2; }

The

fr unit allows you to set the size of a track as a fraction of the free space of the grid container.

For example, this will set each item to one third the width of the grid container:

.container { grid-template-columns: 1fr 1fr 1fr; }

The free space is calculated after any non-flexible items.In this example the total amount of free space available to the

fr units doesn’t include the 50px:

.container { grid-template-columns: 1fr 50px 1fr 1fr; }

grid-template-areas

Defines a grid template by referencing the names of the grid areas which are specified with the

property.Repeating the name of a grid area causes the content to span those cells.

A period signifies an empty cell.The syntax itself provides a visualization of the structure of the grid.

grid-area

Values:

– the name of a grid area specified with

grid-area

.– a period signifies an empty grid cell

none– no grid areas are defined

.container { grid-template-areas: “ | .| none | …” “…”; }

Example:

.item-a { grid-area: header; } .item-b { grid-area: main; } .item-c { grid-area: sidebar; } .item-d { grid-area: footer; } .container { display: grid; grid-template-columns: 50px 50px 50px 50px; grid-template-rows: auto; grid-template-areas: “header header header header” “main main .sidebar” “footer footer footer footer”; }

That’ll create a grid that’s four columns wide by three rows tall.The entire top row will be composed of the header area.

The middle row will be composed of two main areas, one empty cell, and one sidebar area.The last row is all footer.

Each row in your declaration needs to have the same number of cells.

You can use any number of adjacent periods to declare a single empty cell.As long as the periods have no spaces between them they represent a single cell.

Notice that you’re not naming lines with this syntax, just areas.When you use this syntax the lines on either end of the areas are actually getting named automatically.

If the name of your grid area is foo, the name of the area’s starting row line and starting column line will be foo-start, and the name of its last row line and last column line will be foo-end.This means that some lines might have multiple names, such as the far left line in the above example, which will have three names: header-start, main-start, and footer-start.

grid-template

A shorthand for setting

,

grid-template-rows

, and

grid-template-columns

in a single declaration.

grid-template-areas

Values:

none– sets all three properties to their initial values

/

– sets

and

grid-template-columns

to the specified values, respectively, and sets

grid-template-rows

to

grid-template-areas

none

.container { grid-template: none | / ; }

It also accepts a more complex but quite handy syntax for specifying all three.Here’s an example:

.container { grid-template: [row1-start] “header header header” 25px [row1-end] [row2-start] “footer footer footer” 25px [row2-end] / auto 50px auto; }

That’s equivalent to this:

.container { grid-template-rows: [row1-start] 25px [row1-end row2-start] 25px [row2-end]; grid-template-columns: auto 50px auto; grid-template-areas: “header header header” “footer footer footer”; }

Since

grid-template doesn’t reset the implicit grid properties (

,

grid-auto-columns

, and

grid-auto-rows

), which is probably what you want to do in most cases, it’s recommended to use the

grid-auto-flow

property instead of

grid

grid-template.

column-gap

row-gap

grid-column-gap

grid-row-gap

Specifies the size of the grid lines.You can think of it like setting the width of the gutters between the columns/rows.

Values:

– a length value

.container { /* standard */ column-gap: ; row-gap: ; /* old */ grid-column-gap: ; grid-row-gap: ; }

Example:

.container { grid-template-columns: 100px 50px 100px; grid-template-rows: 80px auto 80px; column-gap: 10px; row-gap: 15px; }

The gutters are only created between the columns/rows, not on the outer edges.

Note: The

grid- prefix will be removed and

grid-column-gap and

grid-row-gap renamed to

column-gap and

row-gap.

The unprefixed properties are already supported in Chrome 68+, Safari 11.2 Release 50+, and Opera 54+.

gap

grid-gap

A shorthand for

and

row-gap column-gap

Values:

– length values

.container { /* standard */ gap: ; /* old */ grid-gap: ; }

Example:

.container { grid-template-columns: 100px 50px 100px; grid-template-rows: 80px auto 80px; gap: 15px 10px; }

If no

is specified, it’s set to the same value as

row-gap column-gap

Note: The

grid- prefix is deprecated (but who knows, may never actually be removed from browsers).Essentially

grid-gap renamed to

gap.The unprefixed property is already supported in Chrome 68+, Safari 11.2 Release 50+, and Opera 54+.

justify-items

Aligns grid items along the inline (row) axis (as opposed to

which aligns along the block (column) axis).This value applies to all grid items inside the container.

align-items

Values:

start– aligns items to be flush with the start edge of their cell

end– aligns items to be flush with the end edge of their cell

center– aligns items in the center of their cell

stretch– fills the whole width of the cell (this is the default)

.container { justify-items: start | end | center | stretch; }

Examples:

.container { justify-items: start; }

.container { justify-items: end; }

.container { justify-items: center; }

.container { justify-items: stretch; }

This behavior can also be set on individual grid items via the

property.

justify-self

align-items

Aligns grid items along the block (column) axis (as opposed to

which aligns along the inline (row) axis).This value applies to all grid items inside the container.

justify-items

Values:

stretch– fills the whole height of the cell (this is the default)

start– aligns items to be flush with the start edge of their cell

end– aligns items to be flush with the end edge of their cell

center– aligns items in the center of their cell

baseline– align items

along text baseline.There are modifiers to

baseline—

first baselineand

last baselinewhich will use the baseline from the first or last line in the case of multi-line text.

.container { align-items: start | end | center | stretch; }

Examples:

.container { align-items: start; }

.container { align-items: end; }

.container { align-items: center; }

.container { align-items: stretch; }

This behavior can also be set on individual grid items via the

property.

align-self

There are also modifier keywords

safe and

unsafe (usage is like

align-items: safe end).The

safe keyword means “try to align like this, but not if it means aligning an item such that it moves into inaccessible overflow area”, while

unsafe will allow moving content into inaccessible areas (“data loss”).

place-items

place-items sets both the

align-items and

justify-items properties in a single declaration.

Values:

/

– The first value sets

align-items, the second value

justify-items.If the second value is omitted, the first value is assigned to both properties.

For more details, see

and align-items .

justify-items

This can be very useful for super quick multi-directional centering:

.center { display: grid; place-items: center; }

justify-content

Sometimes the total size of your grid might be less than the size of its grid container.

This could happen if all of your grid items are sized with non-flexible units like

px.In this case you can set the alignment of the grid within the grid container.This property aligns the grid along the inline (row) axis (as opposed to

which aligns the grid along the block (column) axis).

align-content

Values:

start– aligns the grid to be flush with the start edge of the grid container

end– aligns the grid to be flush with the end edge of the grid container

center– aligns the grid in the center of the grid container

stretch– resizes the grid items to allow the grid to fill the full width of the grid container

space-around– places an even amount of space between each grid item, with half-sized spaces on the far ends

space-between– places an even amount of space between each grid item, with no space at the far ends

space-evenly– places an even amount of space between each grid item, including the far ends

.container { justify-content: start | end | center | stretch | space-around | space-between | space-evenly; }

Examples:

.container { justify-content: start; }

.container { justify-content: end; }

.container { justify-content: center; }

.container { justify-content: stretch; }

.container { justify-content: space-around; }

.container { justify-content: space-between; }

.container { justify-content: space-evenly; }

align-content

Sometimes the total size of your grid might be less than the size of its grid container.This could happen if all of your grid items are sized with non-flexible units like

px.

In this case you can set the alignment of the grid within the grid container.This property aligns the grid along the block (column) axis (as opposed to

which aligns the grid along the inline (row) axis).

justify-content

Values:

start– aligns the grid to be flush with the start edge of the grid container

end– aligns the grid to be flush with the end edge of the grid container

center– aligns the grid in the center of the grid container

stretch– resizes the grid items to allow the grid to fill the full height of the grid container

space-around– places an even amount of space between each grid item, with half-sized spaces on the far ends

space-between– places an even amount of space between each grid item, with no space at the far ends

space-evenly– places an even amount of space between each grid item, including the far ends

.container { align-content: start | end | center | stretch | space-around | space-between | space-evenly; }

Examples:

.container { align-content: start; }

.container { align-content: end; }

.container { align-content: center; }

.container { align-content: stretch; }

.container { align-content: space-around; }

.container { align-content: space-between; }

.container { align-content: space-evenly; }

place-content

place-content sets both the

align-content and

justify-content properties in a single declaration.

Values:

/

– The first value sets

align-content, the second value

justify-content.If the second value is omitted, the first value is assigned to both properties.

All major browsers except Edge support the

place-content shorthand property.

For more details, see

and align-content https://css-tricks.com/snippets/css/complete-guide-grid/#prop-justify-content

justify-content

grid-auto-columns

grid-auto-rows

Specifies the size of any auto-generated grid tracks (aka implicit grid tracks).Implicit tracks get created when there are more grid items than cells in the grid or when a grid item is placed outside of the explicit grid.(see

The Difference Between Explicit and Implicit Grids)

Values:– can be a length, a percentage, or a fraction of the free space in the grid (using the

unit)

https://css-tricks.com/snippets/css/complete-guide-grid/#fr-unit

.container { grid-auto-columns:…; grid-auto-rows:…; }

To illustrate how implicit grid tracks get created, think about this:

.container { grid-template-columns: 60px 60px; grid-template-rows: 90px 90px; }

This creates a 2 x 2 grid.

But now imagine you use

and

grid-column

to position your grid items like this:

grid-row

.item-a { grid-column: 1 / 2; grid-row: 2 / 3; } .item-b { grid-column: 5 / 6; grid-row: 2 / 3; }

We told .item-b to start on column line 5 and end at column line 6, but we never defined a column line 5 or 6.

Because we referenced lines that don’t exist, implicit tracks with widths of 0 are created to fill in the gaps.We can use

and

grid-auto-columns

to specify the widths of these implicit tracks:

grid-auto-rows

.container { grid-auto-columns: 60px; }

grid-auto-flow

If you have grid items that you don’t explicitly place on the grid, the auto-placement algorithm kicks in to automatically place the items.This property controls how the auto-placement algorithm works.

Values:

row– tells the auto-placement algorithm to fill in each row in turn, adding new rows as necessary (default)

column– tells the auto-placement algorithm to fill in each column in turn, adding new columns as necessary

dense– tells the auto-placement algorithm to attempt to fill in holes earlier in the grid if smaller items come up later

.container { grid-auto-flow: row | column | row dense | column dense; }

Note that dense only changes the visual order of your items and might cause them to appear out of order, which is bad for accessibility.

Examples:

Consider this HTML:

item-a
item-b
item-c
item-d
item-e

You define a grid with five columns and two rows, and set

grid-auto-flow to

row (which is also the default):

.container { display: grid; grid-template-columns: 60px 60px 60px 60px 60px; grid-template-rows: 30px 30px; grid-auto-flow: row; }

When placing the items on the grid, you only specify spots for two of them:

.item-a { grid-column: 1; grid-row: 1 / 3; } .item-e { grid-column: 5; grid-row: 1 / 3; }

Because we set

grid-auto-flow to

row, our grid will look like this.Notice how the three items we didn’t place (item-b, item-c and item-d) flow across the available rows:

If we instead set

grid-auto-flow to

column, item-b, item-c and item-d flow down the columns:

.container { display: grid; grid-template-columns: 60px 60px 60px 60px 60px; grid-template-rows: 30px 30px; grid-auto-flow: column; }

grid

A shorthand for setting all of the following properties in a single declaration:

,

grid-template-rows

,

grid-template-columns

,

grid-template-areas

,

grid-auto-rows

, and

grid-auto-columns

(Note: You can only specify the explicit or the implicit grid properties in a single grid declaration).

grid-auto-flow

Values:

none– sets all sub-properties to their initial values.

– works the same as the

shorthand.

grid-template

/ [ auto-flow && dense? ] ?– sets

to the specified value.If the

grid-template-rows

auto-flowkeyword is to the right of the slash, it sets

to

grid-auto-flow

column.

If the

densekeyword is specified additionally, the auto-placement algorithm uses a “dense” packing algorithm.If

is omitted, it is set to

grid-auto-columns

auto.

[ auto-flow && dense? ] ? / – sets

to the specified value.If the

grid-template-columns

auto-flowkeyword is to the left of the slash, it sets

to

grid-auto-flow

row.If the

densekeyword is specified additionally, the auto-placement algorithm uses a “dense” packing algorithm.If

is omitted, it is set to

grid-auto-rows

auto.

Examples:

The following two code blocks are equivalent:

.container { grid: 100px 300px / 3fr 1fr; } .container { grid-template-rows: 100px 300px; grid-template-columns: 3fr 1fr; }

The following two code blocks are equivalent:

.container { grid: auto-flow / 200px 1fr; } .container { grid-auto-flow: row; grid-template-columns: 200px 1fr; }

The following two code blocks are equivalent:

.container { grid: auto-flow dense 100px / 1fr 2fr; } .container { grid-auto-flow: row dense; grid-auto-rows: 100px; grid-template-columns: 1fr 2fr; }

And the following two code blocks are equivalent:

.container { grid: 100px 300px / auto-flow 200px; } .container { grid-template-rows: 100px 300px; grid-auto-flow: column; grid-auto-columns: 200px; }

It also accepts a more complex but quite handy syntax for setting everything at once.

You specify

,

grid-template-areas

and

grid-template-rows

, and all the other sub-properties are set to their initial values.What you’re doing is specifying the line names and track sizes inline with their respective grid areas.

This is easiest to describe with an example:

grid-template-columns

.container { grid: [row1-start] “header header header” 1fr [row1-end] [row2-start] “footer footer footer” 25px [row2-end] / auto 50px auto; }

That’s equivalent to this:

.container { grid-template-areas: “header header header” “footer footer footer”; grid-template-rows: [row1-start] 1fr [row1-end row2-start] 25px [row2-end]; grid-template-columns: auto 50px auto; }

Properties for the Children

(Grid Items)

Jump links

float,

display: inline-block,

display: table-cell,

vertical-align and

column-* properties have no effect on a grid item.

grid-column-start

grid-column-end

grid-row-start

grid-row-end

Determines a grid item’s location within the grid by referring to specific grid lines.

grid-column-start/

grid-row-start is the line where the item begins, and

grid-column-end/

grid-row-end is the line where the item ends.

Values:

– can be a number to refer to a numbered grid line, or a name to refer to a named grid line

span – the item will span across the provided number of grid tracks

span – the item will span across until it hits the next line with the provided name

auto– indicates auto-placement, an automatic span, or a default span of one

.item { grid-column-start: | | span | span | auto; grid-column-end: | | span | span | auto; grid-row-start: | | span | span | auto; grid-row-end: | | span | span | auto; }

Examples:

.item-a { grid-column-start: 2; grid-column-end: five; grid-row-start: row1-start; grid-row-end: 3; }

.item-b { grid-column-start: 1; grid-column-end: span col4-start; grid-row-start: 2; grid-row-end: span 2; }

If no

grid-column-end/

grid-row-end is declared, the item will span 1 track by default.

Items can overlap each other.You can use

z-index to control their stacking order.

Learn more about the

span notation in

this article by DigitalOcean.

grid-column

grid-row

Shorthand for

+

grid-column-start

, and

grid-column-end

+

grid-row-start

, respectively.

grid-row-end

Values:

/

– each one accepts all the same values as the longhand version, including span

.item { grid-column: / | / span ; grid-row: / | / span ; }

Example:

.item-c { grid-column: 3 / span 2; grid-row: third-line / 4; }

If no end line value is declared, the item will span 1 track by default.

grid-area

Gives an item a name so that it can be referenced by a template created with the

property.Alternatively, this property can be used as an even shorter shorthand for

grid-template-areas

+

grid-row-start

+

grid-column-start

+

grid-row-end

.

grid-column-end

Values:

– a name of your choosing

/

/

/

– can be numbers or named lines

.item { grid-area: | / / / ; }

Examples:

As a way to assign a name to the item:

.item-d { grid-area: header; }

As the short-shorthand for

+

grid-row-start

+

grid-column-start

+

grid-row-end

:

grid-column-end

.item-d { grid-area: 1 / col4-start / last-line / 6; }

justify-self

Aligns a grid item inside a cell along the inline (row) axis (as opposed to

which aligns along the block (column) axis).This value applies to a grid item inside a single cell.

align-self

Values:

start– aligns the grid item to be flush with the start edge of the cell

end– aligns the grid item to be flush with the end edge of the cell

center– aligns the grid item in the center of the cell

stretch– fills the whole width of the cell (this is the default)

.item { justify-self: start | end | center | stretch; }

Examples:

.item-a { justify-self: start; }

.item-a { justify-self: end; }

.item-a { justify-self: center; }

.item-a { justify-self: stretch; }

To set alignment for all the items in a grid, this behavior can also be set on the grid container via the

property.

justify-items

align-self

Aligns a grid item inside a cell along the block (column) axis (as opposed to

which aligns along the inline (row) axis).This value applies to the content inside a single grid item.

justify-self

Values:

start– aligns the grid item to be flush with the start edge of the cell

end– aligns the grid item to be flush with the end edge of the cell

center– aligns the grid item in the center of the cell

stretch– fills the whole height of the cell (this is the default)

.item { align-self: start | end | center | stretch; }

Examples:

.item-a { align-self: start; }

.item-a { align-self: end; }

.item-a { align-self: center; }

.item-a { align-self: stretch; }

To align all the items in a grid, this behavior can also be set on the grid container via the

property.

align-items

place-self

place-self sets both the

align-self and

justify-self properties in a single declaration.

Values:

auto– The “default” alignment for the layout mode.

/

– The first value sets

align-self, the second value

justify-self.If the second value is omitted, the first value is assigned to both properties.

Examples:

.item-a { place-self: center; }

.item-a { place-self: center stretch; }

All major browsers except Edge support the

place-self shorthand property.

Special Units & Functions

fr units

You’ll likely end up using a lot of

fractional units in CSS Grid, like

1fr.They essentially mean “portion of the remaining space”.

So a declaration like:

grid-template-columns: 1fr 3fr;

Means, loosely,

25% 75%.Except that those percentage values are much more firm than fractional units are.For example, if you added padding to those percentage-based columns, now you’ve broken 100% width (assuming a

content-box box model).Fractional units also much more friendly in combination with other units, as you can imagine:

grid-template-columns: 50px min-content 1fr;

Sizing Keywords

When sizing rows and columns, you can use all the

lengths you are used to, like

px, rem, %, etc, but you also have keywords:

min-content: the minimum size of the content.Imagine a line of text like “E pluribus unum”, the min-content is likely the width of the word “pluribus”.

max-content: the maximum size of the content.Imagine the sentence above, the

max-contentis the length of the whole sentence.

auto: this keyword is a lot like

frunits, except that they “lose” the fight in sizing against

frunits when allocating the remaining space.

– Fractional units: see above

Sizing Functions

– The

fit-content()function uses the space available, but never less than

min-contentand never more than

max-content.

– The

minmax()function does exactly what it seems like: it sets a minimum and maximum value for what the length is able to be.

This is useful for in combination with relative units.Like you may want a column to be only able to shrink so far.This is

extremely useful and probably what you want:

grid-template-columns: minmax(100px, 1fr) 3fr;

– The

min()function.

– The

max()function.

The repeat() Function and Keywords

The

repeat() function can save some typing:

grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr; /* easier: */ grid-template-columns: repeat(8, 1fr); /* especially when: */ grid-template-columns: repeat(8, minmax(10px, 1fr));

But

repeat() can get extra fancy when combined with keywords:

auto-fill: Fit as many possible columns as possible on a row, even if they are empty.

auto-fit:Fit whatever columns there are into the space.Prefer expanding columns to fill space rather than empty columns.

This bears the most famous snippet in all of CSS Grid and one of the

all-time great CSS tricks:

grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

The difference between the keywords is

spelled out in detail here.

Masonry

An experimental feature of CSS grid is masonry layout.Note that there are

lots of approaches to CSS masonry, but mostly of them are trickery and either have major downsides or aren’t what you quite expect.The spec has an official way now, and this is behind a flag in Firefox:

.container { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: masonry; }

See Rachel’s article for a deep dive.

Subgrid

Subgrid is an extremely useful feature of grids that allows grid items to have a grid of their own that inherits grid lines from the parent grid.

.parent-grid { display: grid; grid-template-columns: repeat(9, 1fr); } .grid-item { grid-column: 2 / 7; display: grid; grid-template-columns: subgrid; } .child-of-grid-item { /* gets to participate on parent grid! */ grid-column: 3 / 6; }

This is

only supported in Firefox right now, but it really needs to get everywhere.

It’s also useful to know about

display: contents;.

This is not the same as subgrid, but it can be a useful tool sometimes in a similar fashion.

CSS Grid browser support [CSS Grid browser support](#aa-css-grid-browser-support)

This browser support data is from

Caniuse, which has more detail.A number indicates that browser supports the feature at that version and up.

Desktop

|Chrome||Firefox||IE||Edge||Safari|

|57||52||11*||16||10.1|

Mobile / Tablet

|Android Chrome||Android Firefox||Android||iOS Safari|

|119||119||119||10.3|

Fluid columns snippet [Fluid columns snippet](#aa-fluid-columns-snippet)

Fluid width columns that break into more or less columns as space is available, with no media queries!

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); /* This is better for small screens, once min() is better supported */ /* grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr)); */ gap: 1rem; }

CSS Grid animation [CSS Grid animation](#aa-css-grid-animation)

According to the CSS Grid Layout Module Level 1 specification, there are 5 animatable grid properties:

grid-gap,

grid-row-gap,

grid-column-gapas length, percentage, or calc.

grid-template-columns,

grid-template-rowsas a simple list of length, percentage, or calc, provided the only differences are the values of the length, percentage, or calc components in the list.

As of this writing, only the animation of

(grid-)gap,

(grid-)row-gap,

(grid-)column-gap is implemented in any of the tested browsers.

|Browser|

|Firefox||supported ✅ 53+||supported ✅ 66+||supported ✅ 66+|

|Safari 12.0||not supported ❌||not supported ❌||not supported ❌|

|Chrome||supported ✅ 66+||not supported ❌||not supported ❌|

|Chrome for Android 66+, Opera Mini 33+||supported ✅||not supported ❌||not supported ❌|

|Edge||supported ✅ 16+||not supported ❌||not supported ❌|

CSS-Grid tricks! [CSS-Grid tricks!](#aa-css-grid-tricks)

https://css-tricks.com/4-css-grid-properties-and-one-value-for-most-of-your-layout-needs/”>

4 CSS Grid Properties (and One Value) for Most of Your Layout Needs

https://css-tricks.com/a-calendar-in-three-lines-of-css/”>

A Calendar in Three Lines of CSS

https://css-tricks.com/a-clever-sticky-footer-technique/”>

A Clever Sticky Footer Technique

https://css-tricks.com/a-grid-of-logos-in-squares/”>

A Grid of Logos in Squares

https://css-tricks.com/a-lightweight-masonry-solution/”>

A Lightweight Masonry Solution

https://css-tricks.com/accordion-rows-in-css-grid/”>

Accordion Rows in CSS Grid

https://css-tricks.com/a-responsive-grid-layout-with-no-media-queries/”>

A responsive grid layout with no media queries

https://css-tricks.com/an-auto-filling-css-grid-with-max-columns/”>

An Auto-Filling CSS Grid With Max Columns of a Minimum Size

https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/”>

Auto-Sizing Columns in CSS Grid: `auto-fill` vs `auto-fit`

https://css-tricks.com/breaking-css-grid-explained/”>

Breaking Out with CSS Grid Explained

https://css-tricks.com/bringing-css-grid-to-wordpress-layouts/”>

Bringing CSS Grid to WordPress Layouts

https://css-tricks.com/building-css-grid-overlay/”>

Building a CSS Grid Overlay

https://css-tricks.com/building-a-conference-schedule-with-css-grid/”>

Building a Conference Schedule with CSS Grid

https://css-tricks.com/building-a-hexagonal-grid-using-css-grid/”>

Building a hexagonal grid using CSS grid

https://css-tricks.com/breaking-the-grid/”>

Breaking the Grid

https://css-tricks.com/css-grid-and-custom-shapes-part-1/”>

CSS Grid and Custom Shapes, Part 1

https://css-tricks.com/cool-little-css-grid-tricks-for-your-blog/”>

Cool Little CSS Grid Tricks for Your Blog

https://css-tricks.com/counting-css-counters-css-grid/”>

Counting With CSS Counters and CSS Grid

https://css-tricks.com/creating-a-bar-graph-with-css-grid/”>

Creating a Bar Graph with CSS Grid

https://css-tricks.com/css-grid-can-do-auto-height-transitions/”>

CSS Grid Can Do Auto Height Transitions

https://css-tricks.com/css-grid-one-layout-multiple-ways/”>

CSS Grid: One Layout, Multiple Ways

https://css-tricks.com/snippets/css/css-grid-starter-layouts/”>

CSS Grid Starter Layouts

https://css-tricks.com/equal-width-columns-in-css-grid-are-kinda-weird/”>

Equal Width Columns in CSS Grid are Kinda Weird

https://css-tricks.com/expandable-sections-within-a-css-grid/”>

Expandable Sections Within a CSS Grid

https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/”>

Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers

https://css-tricks.com/flexbox-like-just-put-elements-in-a-row-with-css-grid/”>

Flexbox-like “just put elements in a row” with CSS grid

https://css-tricks.com/grid-content-re-ordering-and-accessibility/”>

Grid, content re-ordering and accessibility

https://css-tricks.com/implicit-grids-repeatable-layout-patterns-and-danglers/”>

Implicit Grids, Repeatable Layout Patterns, and Danglers

https://css-tricks.com/look-ma-no-media-queries-responsive-layouts-using-css-grid/”>

Look Ma, No Media Queries! Responsive Layouts Using CSS Grid

https://css-tricks.com/making-a-bar-chart-with-css-grid/”>

Making A Bar Chart with CSS Grid

https://css-tricks.com/overlapping-header-with-css-grid/”>

Overlapping Header with CSS Grid

https://css-tricks.com/positioning-overlay-content-with-css-grid/”>

Positioning Overlay Content with CSS Grid

https://css-tricks.com/preventing-a-grid-blowout/”>

Preventing a Grid Blowout

https://css-tricks.com/simple-named-grid-areas/”>

Simple Named Grid Areas

https://css-tricks.com/techniques-for-a-newspaper-layout-with-css-grid-and-border-lines-between-elements/”>

Techniques for a Newspaper Layout with CSS Grid and Border Lines Between Elements

https://css-tricks.com/the-holy-grail-layout-with-css-grid/”>

The Holy Grail Layout with CSS Grid

https://css-tricks.com/using-position-sticky-with-css-grid/”>

Using Position Sticky With CSS Grid

Learning CSS Grid [Learning CSS Grid](#aa-learning-css-grid)

https://css-tricks.com/collection-interesting-facts-css-grid-layout/”>

A Collection of Interesting Facts about CSS Grid Layout

https://css-tricks.com/introduction-fr-css-unit/”>

An Introduction to the `fr` CSS unit

https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/”>

Auto-Sizing Columns in CSS Grid: `auto-fill` vs `auto-fit`

https://css-tricks.com/centering-css-complete-guide/”>

Centering in CSS: A Complete Guide

https://css-tricks.com/css-grid-layout-module-level-2/”>

CSS Grid Layout Module Level 2

https://css-tricks.com/css-grid-in-ie-css-grid-and-the-new-autoprefixer/”>

CSS Grid in IE: CSS Grid and the New Autoprefixer

https://css-tricks.com/css-grid-replace-flexbox/”>

Does CSS Grid Replace Flexbox?

https://css-tricks.com/dont-use-grid-system-others/”>

Don’t Use My Grid System (or any others)

https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/”>

Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers

https://css-tricks.com/fit-content-and-fit-content/”>

fit-content and fit-content()

https://css-tricks.com/getting-started-css-grid/”>

Getting Started with CSS Grid

https://css-tricks.com/flexbox-grids-layouts-best-friends/”>

Flexbox and Grids, your layout’s best friends

https://css-tricks.com/grid-level-2-and-subgrid/”>

Grid Level 2 and Subgrid

https://css-tricks.com/grid-for-layout-flexbox-for-components/”>

Grid for layout, flexbox for components

https://css-tricks.com/hello-subgrid/”>

Hello Subgrid!

https://css-tricks.com/is-css-float-deprecated/”>

Is CSS float deprecated?

https://css-tricks.com/quick-whats-the-difference-between-flexbox-and-grid/”>

Quick! What’s the Difference Between Flexbox and Grid?

https://css-tricks.com/some-css-grid-strategies-for-matching-design-mockups/”>

Some CSS Grid Strategies for Matching Design Mockups

https://css-tricks.com/the-auto-flowing-powers-of-grids-dense-keyword/”>

The Auto-Flowing Powers of Grid’s Dense Keyword

https://css-tricks.com/difference-explicit-implicit-grids/”>

The Difference Between Explicit and Implicit Grids

https://css-tricks.com/things-ive-learned-css-grid-layout/”>

Things I’ve Learned About CSS Grid Layout

https://css-tricks.com/thinking-outside-the-box-with-css-grid/”>

Thinking Outside the Box with CSS Grid

https://css-tricks.com/to-grid-or-not-to-grid/”>

To grid or not to grid

https://css-tricks.com/using-grid-named-areas-to-visualize-and-reference-your-layout/”>

Using Grid Named Areas to Visualize (and Reference) Your Layout

https://css-tricks.com/why-we-need-css-subgrid/”>

Why we need CSS subgrid

CSS Grid videos [CSS Grid videos](#aa-css-grid-videos)

More CSS Grid sources [More CSS Grid sources](#aa-more-css-grid-sources) CSS Grid Layout Module Level 1(W3C) Grid by Example(Rachel Andrew) Learning CSS Grid Layout(Rachel Andrew) CSS Grid Layout: Introduction(DigitalOcean) CSS Grid Layout: The Repeat Notation(DigitalOcean) CSS Grid Layout: The Span Keyword(DigitalOcean) CSS Grid Layout: The Fr Unit(DigitalOcean) CSS Grid: Holy Grail Layout(DigitalOcean) How To Use CSS Grid Properties to Justify and Align Content and Items(DigitalOcean).

Leave a Reply

Your email address will not be published. Required fields are marked *

Next Post

How to Play Football Squares

[Download Audio](#) [Download Audio](#) This article was co-authored by [wikiHow Staff](/wikiHow:Content-Management).Our trained team of editors and researchers validate articles for accuracy and comprehensiveness.wikiHow’s [Content Management Team](/wikiHow:Content-Management) carefully monitors the work from our editorial staff to ensure that each article is backed by trusted research and meets our high quality standards. […]
How to Play Football Squares

Subscribe US Now