Skip to content
Notes | Calvin Choy
Go back

CSS Grid Basics

While going through some CSS fundamentals I reviewed and noted down some basics of using CSS Grid.

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

  • grid-template-columns - each space separated entry is a column
  • grid-template-rows - each space separated entry is a row
  • grid-templates-areas - define grid layout using labeled areas that grid items can occupy using grid-area
  • grid-auto-flow - determines the direction items are flowed into the grid, defaults to row
  • repeat(n, size) - to create repeating columns/rows helper function
    • For intrinsic sizing we could use auto-fit or auto-fill for n and minmax() for size

Sizing

  • auto - shy, take only space it needs for it content
  • fr - greedy, take all the available space, a fraction
  • minmax(n, m) - specify the minimum and maximum size a track can take
  • min-content - intrinsic sizing by content, never smaller than content inside
  • max-content - intrinsic sizing by content, never larger than content inside

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.

  • justify-items - default stretch, set the alignment property on the row axis.
  • align-items - default stretch, set the alignment property on the column axis.

Content

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

  • justify-content - default stretch, set the alignment property on the row axis.
  • align-content - default stretch, set the alignment property on the column axis.
  • place-content - shorthand for setting align-content/justify-content, if only one is provided they are applied to both.

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

  • grid-column - specify the start / end column track number.
  • grid-row - specify the start / end column track number.
  • span n - instead of specify an end track number you can also specify the number of tracks to span.
    • For example: grid-column: 2 / span 2, start at gridline 2 spanning 2 tracks
  • grid-area - specify the name of the template area, without quotes. Name of the grid area is specified as part the grid container

Alignment

Setting alignment of the item within the cell it is in

  • justify-self - set the alignment property on the row axis.
  • align-self - set the alignment property on the column axis.
  • place-content - shorthand for setting align-self/justify-self, if only one is provided they are applied to both.

Resources



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