Skip to main content

Grid in CSS – What Is CSS Grid Layout Module?

The CSS Grid Layout Module makes browsers display the selected HTML elements as grid box models.

Grid allows easy resizing and repositioning of a grid container and its items two-dimensionally.

info
  • "Two-dimensionally" means grid modules allow simultaneous laying out of box models in rows and columns.
  • Use flexbox if you only need to resize and reposition elements one-dimensionally.
Buy your CSS Flexbox Explained Visually book on Amazon

Grid Container vs. Grid Item: What's the Difference?

A grid container is an HTML element whose display property's value is grid or inline-grid.

A grid item is any of the direct children of a grid container.

Illustration of a grid container and a grid item

What Is a grid Value in CSS?

grid tells browsers to display the selected HTML element as a block-level grid box model.

In other words, setting an element's display property's value to grid turns the box model into a block-level grid layout module.

Here's an example:

section {
display: grid;
background-color: orange;
margin: 10px;
padding: 7px;
}

div {
background-color: purple;
color: white;
margin: 5px;
padding: 10px;
border-radius: 5px;
}

Try it on StackBlitz

The snippet above used the grid value to convert the HTML document's <section> elements from regular <section> nodes to block-level grid box models.

note

Let's now discuss the inline-grid value.

What Is an inline-grid Value in CSS?

inline-grid tells browsers to display the selected HTML element as an inline-level grid box model.

In other words, setting an element's display property's value to inline-grid turns the box model into an inline-level grid layout module.

Here's an example:

section {
display: inline-grid;
background-color: orange;
margin: 10px;
padding: 7px;
}

div {
background-color: purple;
color: white;
margin: 5px;
padding: 10px;
border-radius: 5px;
}

Try it on StackBlitz

The snippet above used the inline-grid value to convert the HTML document's <section> elements from regular <section> nodes to inline-level grid box models.

Properties for Controlling a Grid's Layout

On converting a regular HTML element to a grid (or inline-grid) box model, the grid layout module provides two types of properties for controlling the grid box and its direct children:

  • Grid container's properties
  • Grid item's properties

What Are the Grid Container's Properties?

A grid container's properties specify how browsers should layout items within the grid box model.

note

We define a grid container's property on the container, not its items.

The eight (8) types of grid container properties are:

Explicit vs. Implicit Grids

Explicit grids are the grids (rows or columns) you explicitly define with the grid-template-columns or grid-template-rows property.

Implicit grids are the grids browsers create automatically. We use the grid-auto-columns and grid-auto-rows properties to specify track sizes for implicit grids.

What Are the Grid Item's Properties?

A grid item's properties specify how browsers should layout a specified item within the grid box model.

note

We define a grid item's property on the item, not its container.

The ten (10) types of grid item properties are:

Buy your CSS Flexbox Explained Visually book on Amazon

Overview

This article discussed what a CSS grid is. We also discussed the differences between a grid container and grid items.