1.2: Grids

There is more to grids than flexbox, so let's pause here to learn more about grids.

Along with containers(parents) and items(children), grids have cells, tracks and areas.

Cell: one(1) unit

Track: range bordered by two(2) lines

Area: range bordered by four(4) lines

grid specs

Grid Components

Property

Value

grid-template-columns

fr

grid-template-rows

auto

grid-template-areas

repeat

grid-area

minmax

Using grid-templates allows developer to, not only have consistency between sections and pages or their site, but also allows more precise placement of items that will function as you want them to do even with a change in viewport size. Additionally, it allows for easier item placement when using order and media queries.

Grid columns and rows can be delineated by

Numbered lines

.

numbered lines

Below are layout examples using all three methods.

When defining a grid it is essential that you define the box size. (asterisk(*) represents "universal" and will apply to all)

*{box-sizing: border-box;}

Numbered lines

numbered lines

Named lines

With some minor adjustments, using grid-column/row-start/end, the above CSS could be written as

Grid

Named areas

Look closely at where the columns and rows layout.

grid layout

NOTE:

"." is used as a placeholder. Instead of the footer spanning the entire width, it only occupies three(3) of the nine(9) columns that we designated in our template.

EXPLORE MORE

We will be using one(1) container with three(3) columns and two(2) rows to look at the capabilities of the grid system.

In section 1.1 we saw a container with five(5) items within a container comprised of one(1) column and five(5) rows.

Value focus: span, relative and absolute values,

span

We covered lengths at the beginning of the section. Columns and rows can use relative or absolute lengths. Above we see that the grid columns are set to an absolute length of 200px and rows set to auto.

columns/rows can be set the following ways:

  • 200px 200px 200px

  • 1fr 1fr 1fr ( is the same as repeat(3, 1fr) )

  • 1fr 1fr 200px

  • 20px 1fr 200px

  • 2fr 1fr 1fr

  • 1fr auto 10%

Challenges

Write code for the following grids

fr unit
mixed values
fr repeat

Last updated