Table of Contents

CSS - Grid

About

grid 1)is a CSS layout system that provides a mechanism:

With grid, you can easily defines layout that requires horizontal and vertical alignment such as the below form2)

Grid Form With Horizontal Vertical Alignment

Feature

Flex Vs Grid Css 3)

Concept

Container

A grid container 4) is the container element that has the display value set to:

#grid  {
  display: grid;
}

It contains the grid.

Grid

The grid is defined 7) with the following three properties:

They define the grid of a grid container by specifying its explicit grid tracks.

The grid property 8) is a shorthand that sets all of three explicit grid properties

Example: the following grid declares a grid with :

main {
  grid: "H    H "
        "A    B "
        "F    F " 30px
  /     auto 1fr;
}

Area

A grid area 9) is a logical space name:

The grid area are defined using:

Example:

#grid  {
  display: grid;
  grid-template-areas: ". area1"
                       "area2 area1"
                       ". area1";
}
main {
  grid: auto 1fr auto / repeat(5, 1fr);
  min-height: 100vh;
}

Positioning

You can specify the position where the element should go, using :

Longhand properties shorthand equivalent full shorthand equivalent
grid-row-start
grid-row-end 11)
grid-row 12) grid-area 13)
grid-column-start
grid-column-end 14)
grid-column 15) grid-area 16)

Example:

Placement grid-area grid-row, grid-column grid-row-start/end, grid-column-start/end
Place into named grid area a
grid-area: a;
grid-row: a; 
grid-column: a;
grid-row-start: a; 
grid-row-end: a; 
grid-column-start: a; 
grid-column-end: a;
Auto-place into next empty area
grid-area: auto;
grid-row: auto;  
grid-column: auto;
Place into row 2, column 4
grid-area: 2 / 4;
grid-row: 2; 
/* Place item in the second row. */
grid-column: 4; 
/* Place item in the fourth column. */
Place into column 3, span all rows
grid-area: 1 / 3 / -1;
grid-row: 1 / -1; 
grid-column: 3;
/* 1st grid row line = top of grid container */
grid-row-start: 1; 
/* span all rows */
grid-row-end: -1;
/* 3rd grid col line */
grid-column-start: 3;
/* right padding edge */ 
grid-column-end: auto; 
Place using named lines
grid-area: header-start / 
sidebar-start / 
footer-end / 
sidebar-end;
grid-row: header-start / footer-end; 
grid-column: sidebar-start / sidebar-end;

In css, you would apply them in rule block. Example in a 2 columns x 3 rows.

#stats    { grid-column: 1; grid-row: 2; align-self: start; }
#controls { grid-column: 2; grid-row: 3; justify-self: center; }

Sizing

Sizing a grid means sizing the tracks. See the tracks definition section to know how to size a grid.

Item

The grid item 19) are:

As of Grid level 1, only the direct children of the container become grid items and can then be placed on the grid

Cell

A grid cell 20) is the intersection of:

It is the smallest unit of the grid that can be referenced when positioning grid items.

Track

Grid track 21) is a generic term for:

It's the space between any two adjacent lines on the grid.

The below properties define grid tracks:

They controls how wide or tall the column or row may grow.

The sizing calculation is done by the grid sizing algorithm

Example of a Grid with two columns and three rows. 22)

#grid {
  display: grid;
  grid-template-columns: 150px 1fr;  /* two columns */
  grid-template-rows: 50px 1fr auto; /* three rows  */
}

where:

Line

grid lines can be referred to by:

The following code 23) names the lines in the grid with the sizing properties. (Some of the lines have multiple names)

#grid {
  display: grid;
  grid-template-columns: [first nav-start] 150px [main-start] 1fr [last];
  grid-template-rows: [first header-start] 50px [main-start] 1fr [footer-start] 50px [last];
}

Grid Named Lines

Alignement / Space Distribution

The sized grid is aligned 24) 25) according to the grid container’s:

Example:

.grid {
  display: grid;
  grid: 12rem 12rem 12rem 12rem / 10rem 10rem 10rem 10rem;
  justify-content: end;
  align-content: center;
}

gridgrid container

main {
  grid: auto-flow 1fr / repeat(auto-fill, 5em);
  min-height: 100vh;
  justify-content: space-between;
  align-content: safe center;
}

Overflow

The content may overflow if the cells in a grid are Block|.

One solution is to set the cell to a grid display with an autoflow to row.

display: grid;
grid-auto-flow: row;

Span

By default, a grid item has a span of 1. 26)

Different spans can be given explicitly:

.three {
  grid-row: 2 / span 5;
}
.four {
  grid-row: span 5 / 7;
}
.five {
  grid-column: first / middle;
}
.six {
  grid-row: text 5 / text 7;
}

Media Query

This example 27) shows how it's possible to change the layout with different media query.

Grid Portrait Layout
@media (orientation: portrait) {
  #grid {
    display: grid;
    grid-template-areas: "title stats"
                         "score stats"
                         "board board"
                         "ctrls ctrls";
    ...
  }
}
Grid Landscape Layout
@media (orientation: landscape) {
#grid {
    display: grid;
    /* Different positions in the layout */
    grid-template-areas: "title board"
                         "stats board"
                         "score ctrls";
    ...  
  }
}

Float

floating is ignored

Unit

Overflow

See How to solve a CSS grid overflow ?

Devtool visualization

The browser devtool have option to visualize the grid.

For instance, on chrome:

Devtool Chrome Layout Grid