Grid Axis
May 20, 2023
The CSS Grid Layout Module allows developers to create 2D grid-based layouts that can be used for various web page designs. The grid system works by dividing the available space into rows and columns, allowing for precise placement of content. To achieve this, the grid system uses a Cartesian coordinate plane where the x-axis runs horizontally and the y-axis runs vertically.
The Grid Axis refers to any of these two axes, the x-axis or y-axis, which are used to create the grid. Each axis has a start and an end point, and the grid lines run parallel to these axes. These grid lines are used to place the grid items or content, and they can be specified using the grid-template-rows
and grid-template-columns
properties.
In a grid system, the x-axis runs from left to right, and the y-axis runs from top to bottom. The start point of the x-axis is at the left edge of the grid, while the end point is at the right edge of the grid. The start point of the y-axis is at the top edge of the grid, while the end point is at the bottom edge of the grid. By default, the grid items are placed in the order in which they appear in the HTML markup, but they can be rearranged using the grid-row and grid-column properties.
Purpose of Grid Axis
The Grid Axis is a fundamental concept in CSS Grid Layout as it forms the basis for creating the grid system. By understanding the concept of Grid Axis, developers can create complex and sophisticated web page designs that are both responsive and flexible. The Grid Axis provides a coordinate system that enables developers to position content precisely, allowing for a more efficient use of space.
One of the main advantages of using the Grid Axis is the ability to create responsive designs that can adapt to different screen sizes and resolutions. By specifying the size and position of grid items relative to the Grid Axis, developers can ensure that their web page layouts remain consistent across different devices and screen sizes. For example, a grid item can be set to span multiple rows or columns, or it can be placed at a specific location on the grid.
Another advantage of using the Grid Axis is that it allows for easy reordering of grid items. By changing the grid-row
and grid-column
properties, developers can easily rearrange the position of grid items without having to modify the HTML markup. This can be particularly useful when creating more complex layouts, where the order of content may need to be adjusted to fit the design.
Usage of Grid Axis
To use the Grid Axis in CSS, developers need to define the grid container element by setting its display property to grid or inline-grid. Once the grid container has been defined, developers can specify the size and position of grid items using various CSS properties, such as grid-template-rows
, grid-template-columns
, grid-row
, grid-column
, and grid-area
.
grid-template-rows and grid-template-columns
The grid-template-rows and grid-template-columns properties are used to define the size and position of the grid rows and columns, respectively. They take a list of values, separated by whitespace, which represent the size and position of each row or column. These values can be specified in various units, such as pixels, percentages, or fractions of the available space.
For example, to create a grid with three rows of equal height, developers can use the following CSS code:
grid-template-rows: 1fr 1fr 1fr;
This defines three rows, each with a height of 1fr
, which stands for “fraction of available space”. This ensures that the rows take up equal amounts of space within the container.
Similarly, to create a grid with two columns, where the first column is twice as wide as the second column, developers can use the following CSS code:
grid-template-columns: 2fr 1fr;
This defines two columns, where the first column takes up twice as much space as the second column.
grid-row and grid-column
The grid-row
and grid-column
properties are used to specify the position of a grid item within the grid. They take a value that represents the starting and ending grid lines for the item. These values can be specified in various units, such as numbers, keywords, or fractions.
For example, to place an item in the first row and first column of the grid, developers can use the following CSS code:
grid-row: 1;
grid-column: 1;
This sets the starting and ending grid lines for the item to 1
, which corresponds to the first row and first column of the grid, respectively.
Similarly, to place an item in the second row and span two columns, developers can use the following CSS code:
grid-row: 2;
grid-column: 1 / span 2;
This sets the starting grid line for the item to 2
, which corresponds to the second row of the grid, and the ending grid line to 3
, which spans two columns from the starting grid line.
grid-area
The grid-area
property is a shorthand property that can be used to specify the position and size of a grid item in a single line of code. It takes four values, separated by whitespace, that represent the starting and ending grid lines for the row and column of the item, respectively.
For example, to place an item in the second row and span two columns, developers can use the following CSS code:
grid-area: 2 / 1 / 3 / 3;
This sets the starting grid line for the row to 2
, the ending grid line for the row to 3
, the starting grid line for the column to 1
, and the ending grid line for the column to 3
. This results in an item that spans two columns in the second row of the grid.