Skip to content
Go back

CSS Grid Basics

The display: grid property sets the container to grid layout. The container is configured in two dimensions using columns and row. By default there is one column and rows are added for each grid item, this results in a stack.

Some terms to remember:

TermDescription
Grid LinesThe dividing lines that make up the structure of the grid. Starts at 1 and can be referenced when placing items.
Grid TracksThe space between adjacent grid lines. Your rows and column.
Grid AreaAny rectangular space surrounded by four grid lines. Areas can be named and items can be put into the areas.
GapUse gap or row-gap and column-gap to specify consistent spacing between rows and or columns.
Fractional Unitfr unit only distributes available space, so it works after fixed-size tracks are accounted for.
Explicit GridThe grid defined by the user using grid-template columns and grid-template-rows.
Implicit GridGrid that is automatically created, or expanded to, when you put more items outside the explicit grid cells.

Approach

For general layout purposes using css grid the following approach can be considered:

  1. Think about the grid layout you want to achieve for desktop and small devices.
  2. Define the grid container an the corresponding columns and rows. Remember the implicit grid by default take care of extra rows when needed.
  3. Place the grid items in the grid areas within the created grid tracks. You will usually use a combination of track indices, spans and grid area.
  4. Adjust alignments where needed: start (default), end, centre, stretch etc. for items as well for content (tracks)
  5. Where needed add breakpoints to adjust the grid container behaviour, but think about intrinsic design first. Many layouts can be achieved without breakpoints.

Grid container

The main goal of the container is to setup the “environment” on which the child (grid) items will be placed on. There are different approaches on how you can define the grid composed of columns and rows. The most straightforward way is to define a explicit grid using grid-template-columns and grid-template-rows.

Defining

Sizing

Alignment

First make distinction between items and content variants of the justify-* and align-* properties.

Items

Configures the alignment and spacing of the grid-items in relation with their content.

Content

Configures the alignment and spacing of your grid tracks in relation with the grid container.

Grid items

Grid items are placed in order by the grid container. By default the grid-auto-flow is set to row and the grid-items are stretched to fill up the grid tracks it is in. Placement is based on starting and ending track index or number of tracks to span.

Placing

Alignment

Setting alignment of the item within the cell it is in

Resources


 

Previous Post
Improving Zsh Startup Time
Next Post
Making API Calls From The Terminal