CSS Grid Components

A grid always consists of:

Example

Result

CSS Grid Container

A grid container contains one or more grid items arranged in columns and rows.

All direct child elements of a grid container automatically become grid items.

An element becomes a grid container when its display property is set to grid or inline-grid.

 

Display Grid Property

The <div> element becomes a grid container when its display property is set to grid or inline-grid.

Example:

CSS Grid Tracks

CSS Grid Tracks (Rows and Colums)

Inside the grid container you define the number and size of the grid columns and rows.

For this, we will use the following properties:

The CSS grid-template-columns Property

The grid-template-columns property defines the number and width of the columns in the grid.

The value is a space-separated-list, where each value defines the width of the respective column.

Common values are:

CSS grid-template-columns: auto

To create a grid layout with 3 columns of equal width, use the “auto” keyword.

Example:

Result:

 

Grid Columns with Mixed Width

To create a grid layout with 3 columns: 2 columns with a fixed width, and 1 column with size “auto”.

Example:

Result:

Grid Columns with the fr Unit

The fr unit stands for “fraction”. This unit automatically divides the available space into fractions.

1fr will take 1 fraction of the available space, while 2fr will take 2 fractions of the available space.

Example:

Result:

Example:

Result:

Grid Columns with the repeat() Function

The CSS repeat() function is used to repeat a set of columns or rows in a grid.

Example:

Result:

 

Grid Columns with the minmax() Function

The CSS minmax() function is used to define a size-range greater than or equal to a min value and less than or equal to a max value.

Example:

Result:

The CSS grid-template-rows Property

The grid-template-rows property defines the number and height of the rows in the grid.

The value is a space-separated-list, where each value defines the height of the respective row.

Common values are:

Example:

Result:

 

 

CSS Grid Gaps

The space between the rows and columns in a grid container are called gaps (or gutters).

The gaps are created between the grid rows and columns, not on the outer edges of the grid container.

The default size of the gap is 0, which means that there are no spacing between grid items.

The size of the gap can be adjusted with the following properties:

 

The CSS column-gap Property

The column-gap property specifies the gap between the columns in a grid.

Example:

Result:

 

The CSS row-gap Property

The row-gap property specifies the gap between the rows in a grid.

Example:

Result:

The CSS gap Property

The gap property is a shorthand property for row-gap and column-gap.

If a single value is provided, it applies the same gap to both rows and columns.

If two values are provided, the first value sets the row-gap, and the second value sets the column-gap.

Example:

Result:

Using two values – Set gap between rows to 30px, and gap between columns to 100px:

Example:

Result:

 

CSS Grid Container – Align Grid Content

Inside the grid container you can define the alignment of the grid content with the following properties:


The CSS justify-content Property

The justify-content property is used to align the grid content when it does not use all available space on the main-axis (horizontally).

This property can have one of the following values:

Example:

Result:

The space-around value displays the grid items with space around them:

Example:

Result:

The space-between value displays the grid items with space between them:

Example:

Result:

 

The center value positions the grid items in the center of the container:

Example:

Result:

The start value positions the grid items at the start of the container:

Example:

Result:

The end value positions the grid items at the end of the container:

Example:

Result:

 

 

 

CSS Grid Items

A grid container contains one or more grid items.

All direct child elements of a grid container automatically become grid items.

Below is a grid container with five grid items:

Example:

Result:

CSS Sizing/Spanning Grid Items

A grid item can span over mulitiple columns or rows.

We can specify where to start and end a grid item by using the following properties:

The lines between the columns in a grid are called column-lines, and the lines between the rows in a grid are called row-lines.

We can refer to line numbers when placing a grid item in a grid container.


CSS grid-column-start and grid-column-end

The grid-column-start property specifies on which column-line the grid item will start.

The grid-column-end property specifies on which column-line the grid item will end.

Example:

Result:

The CSS grid-column Property

The grid-column property is a shorthand property for the grid-column-start and the grid-column-end properties.

Example:

Result:

CSS grid-row-start and grid-row-end

The grid-row-start property specifies on which row-line the grid item will start.

The grid-row-end property specifies on which row-line the grid item will end.

Example:

Result:

The CSS grid-row Property

The grid-row property is a shorthand property for the grid-row-start and the grid-row-end properties.

Example:

Result:

Combine grid-column and grid-row

Here we use both the grid-column and grid-row properties to let a grid item span both columns and rows.

Example:

Result:

Naming Grid Items with grid-area

The CSS grid-template-areas is a grid container property, and it specifies areas within the grid layout.

You can name grid items by using the CSS grid-area property, and then reference to the name in the grid-template-areas property.

Each area is defined within apostrophes. The named grid items in each area is defined inside the apostrophes, separated by a space.

Example:

Result:

Make a ready-to-use Webpage Template

Here, we will name all grid items to make a ready-to-use webpage template:

Example:

Result:

 

 

CSS Grid Items – Alignment

Grid items can be aligned within their respective grid cells using properties like:

These can also be applied to all grid items in a container using justify-items and align-items on the grid container.


The CSS justify-self Property

The justify-self property is used to align a grid item within its grid cell in the inline direction.

Tip: For pages in English, inline direction is left to right and block direction is top to bottom.

This property can have one of the following values:

Note: For this property to have any alignment effect, the grid item need available space around itself in the inline direction.

 

 

Result:

The CSS align-self Property

The align-self property is used to align a grid item within its grid cell in the block direction.

This property can have one of the following values:

Note: For this property to have any alignment effect, the grid item need available space around itself in the block direction.

Example:

Result:

 

Set the Visual Order of the Grid Items

The CSS order property can be used to define the visual order of the grid items.

The first grid item in the HTML source code does not have to appear as the first item in the grid container.

Example:

Result:

 

CSS 12-Column Grid Layout

A 12-column grid is a common and flexible method for structuring web page content, particularly for responsive web design.

The 12-column grid system divides the available horizontal space into 12 equal-width columns, allowing for precise placement and sizing of elements within the layout.


Benefits of a 12-Column Grid

Here is how to create a basic 12-column CSS Grid layout:


Define the Grid Container

Apply display: grid; to the grid container.


Create the 12 Columns

Use the grid-template-columns property to define the 12 columns.

The code repeat(12, [col-start] 1fr); creates a 12-column grid with equal-width, and names the start line of each column track col-start.


Place the Grid Items

Grid items can then be placed and sized across these 12 columns using grid-column.

Use Mediaqueries to Add Breakpoints

Always design for mobile first: Here we display the sections in the source order (from top to bottom) for screens less than 576 pixels wide.

Then, we go to a two-column layout for screens between 576 and 767 pixels wide.

Then, we go to a three-column layout for screens over 767 pixels wide.

complete example of a 12-column grid layout:


Example:

Result: