In CSS Grid Layout, creating a basic grid structure is a fundamental step towards building complex layouts. The two main properties we will be using are grid-template-columns
and grid-template-rows
. These properties allow us to define the number and size of grid tracks, which are the columns and rows that make up our grid.
To create a basic grid, we can specify the track sizes using different units such as pixels, percentages, or auto. For example, we can set three equal-sized columns by using the value repeat(3, 1fr)
. This means that the grid will have three tracks, each taking up one fraction of the available space.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 10px;
}
By default, the grid-template-rows
property is set to auto
, which means that the rows will automatically adjust their height based on the content. However, we can also specify fixed heights for specific rows if needed.
Remember, the grid tracks are numbered in order, starting from 1. We can refer to these numbers when placing items into the grid using the grid-column
and grid-row
properties. For example, to place an item spanning the first two columns, we can use grid-column: 1 / 3
.
Once you have defined the grid structure, you can start placing items inside the grid containers, which will be covered in the next post. Happy gridding!