Grid Tracks

May 20, 2023

Grid tracks refer to the columns and rows that make up the grid in CSS Grid layout. In a CSS Grid, the grid tracks are the basic building blocks that define the structure of the layout. A grid track can be defined as a horizontal or vertical line that runs the length or height of the grid, respectively.

Grid tracks are defined using the grid-template-columns and grid-template-rows properties. These properties take a list of values that specify the size and position of each track. The values can be a length, a percentage, or a keyword that represents a fraction of the available space.

Purpose

The purpose of grid tracks is to provide a way to create complex layouts that are responsive and flexible. By defining the grid tracks, you can create a grid that adapts to different screen sizes and device orientations. You can also create grids with irregular shapes and sizes, which is not possible with other layout methods.

Grid tracks make it easy to create consistent and readable layouts that can be easily maintained and updated. They allow you to specify the size and position of each element in the layout, and ensure that the elements are aligned and spaced correctly. Grid tracks also provide a way to create complex nested grids, which can be used to create more sophisticated layouts.

Usage

Grid tracks are used to define the structure of the grid in CSS Grid layout. They are specified using the grid-template-columns and grid-template-rows properties.

Defining Grid Tracks

To define the grid tracks, you can use a list of values separated by spaces. Each value represents a track, and can be a length, a percentage, or a keyword that represents a fraction of the available space. For example, the following code defines a grid with three columns of equal width:

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

In this example, 1fr represents a fraction of the available space. Since there are three columns, each column takes up one-third of the available space.

You can also specify different sizes for each track. For example, the following code defines a grid with two columns of different widths:

.grid {
  display: grid;
  grid-template-columns: 100px 1fr;
}

In this example, the first column is 100 pixels wide, and the second column takes up the remaining available space.

Naming Grid Tracks

You can also name the grid tracks for easier referencing in your CSS. To name a track, you can use the [] syntax and give it a name. For example, the following code defines a grid with two columns named sidebar and content:

.grid {
  display: grid;
  grid-template-columns: [sidebar] 200px [content] 1fr;
}

In this example, the first column is named sidebar and is 200 pixels wide, and the second column is named content and takes up the remaining available space.

Grid Line Numbers

Grid tracks can also be referenced by their line numbers. Line numbers start at 1 and increase as you move down and to the right in the grid. To reference a line number, you can use the span keyword to specify the number of tracks to span. For example, the following code creates a grid with a header that spans the first row, and a sidebar that spans the first two columns:

.grid {
  display: grid;
  grid-template-rows: 100px 1fr;
  grid-template-columns: 200px 1fr;
}

.header {
  grid-row: 1 / span 1;
  grid-column: 1 / span 2;
}

.sidebar {
  grid-row: 2 / span 1;
  grid-column: 1 / span 1;
}

In this example, the header spans one row (from line 1 to line 2), and two columns (from line 1 to line 3). The sidebar spans one row (from line 2 to line 3) and one column (from line 1 to line 2).

Grid Areas

You can also use grid areas to define the layout of your grid. Grid areas are rectangular regions of the grid that can span multiple tracks. To define a grid area, you can use the grid-template-areas property. For example, the following code defines a grid with three rows and three columns, and defines the layout using grid areas:

.grid {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
  grid-template-areas:
    "header header header"
    "sidebar content content"
    "footer footer footer";
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.content {
  grid-area: content;
}

.footer {
  grid-area: footer;
}

In this example, the grid has three rows and three columns, and the layout is defined using grid areas. The grid-template-areas property defines the layout by specifying the name of each area. The areas are defined using a grid of strings. Each row in the grid represents a row in the layout, and each column represents a column in the layout. The name of each area is specified using a string that represents the name of the area.