Photo by Lukas Blazek on Unsplash
CSS Grid is a powerful layout system that allows web developers to create complex, responsive layouts with ease
CSS Grid is a powerful layout system
CSS Grid is a powerful layout system that allows web developers to create complex, responsive layouts with ease. It is a two-dimensional layout system that allows for precise control over the placement and alignment of elements in a web page. The grid system is made up of rows and columns, with each element placed within a cell defined by a combination of row and column values.
Here's an example of a simple CSS Grid layout:
htmlCopy code<div class="grid-container">
<div class="item-1">Item 1</div>
<div class="item-2">Item 2</div>
<div class="item-3">Item 3</div>
<div class="item-4">Item 4</div>
</div>
cssCopy code.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-gap: 10px;
}
.item-1 {
grid-row: 1 / 3;
grid-column: 1 / 2;
}
.item-2 {
grid-row: 1 / 2;
grid-column: 2 / 3;
}
.item-3 {
grid-row: 2 / 3;
grid-column: 2 / 3;
}
.item-4 {
grid-row: 2 / 3;
grid-column: 1 / 2;
}
In this example, we have a div
with a class of grid-container
that contains four child divs
, each with a unique class name. The grid-container
element has a display
property set to grid
, which tells the browser to create a grid layout.
The grid-template-columns
and grid-template-rows
properties define the number and size of the columns and rows in the grid. In this example, we have defined two columns and two rows, each with a size of 1 fraction unit (1fr
).
The grid-gap
property adds space between the cells in the grid.
Each child element is positioned within the grid using the grid-row
and grid-column
properties. The values for these properties indicate the starting and ending row and column for the element within the grid. For example, the first item has a grid-row
value of 1 / 3
, which means it spans two rows from row 1 to row 3, and a grid-column
value of 1 / 2
, which means it spans one column from column 1 to column 2.
Overall, CSS Grid provides web developers with a flexible and powerful layout system that can be used to create a wide variety of layouts, from simple two-column designs to complex, multi-column layouts with overlapping content.