Grid
Grid
system in CSS, designed to create complex web designs with rows and
columns. It allows you to design web layouts more easily and with greater
precision, as it handles both horizontal (row) and vertical (column)
placements simultaneously.
Display:
➔ We write display : grid
➔ This property is used to turn an element into a grid container
grid-template-columns :
.grid-container { display: grid;
grid-template-columns: 100px 100px 100px;
}
grid-template-rows:
The grid-template-rows property in CSS Grid is used to define the size of
the rows in a grid container. Similar to grid-template-columns,
.grid-container {
display: grid;
grid-template-rows: 200px 200px 200px;
}
Grid Gap:
1. Single gap value for both rows and columns
If you want the same gap size between both rows and columns, you can
use just one value:
.container{
display: grid;
grid-gap: 30px;
}
repeat()
The repeat() function in CSS is a powerful tool used within CSS Grid and
Flexbox to simplify the process of repeating a pattern of columns, rows, or
items. It's especially useful when you want to create a grid with multiple
identical columns or rows without writing repetitive CSS code.
.container{
display: grid;
grid-template-columns: repeat(3, 250px);
}
fr unit
The fr unit in CSS Grid is a flexible and powerful way to distribute space
within a grid container. The abbreviation fr stands for "fraction of
available space". It is used to allocate portions of the grid container's
space, making it perfect for responsive and fluid layouts.
How fr Works:
• The fr unit divides the available space into fractional units, meaning
that the total space left in the container (after accounting for fixed
sizes like pixels or other non-flexible units) is divided into fractions,
and griditems are assigned a certain number of these
fractions.
• 1fr represents 1 fraction of the available space. If multiple items are
assigned fr values, the space will be divided proportionally.
.container{
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
In this example:
• The first column takes 1fr of the available space.
• The second column takes 2fr, so it will be twice as wide as the first
column.
• The third column takes 1fr, equal to the first column.
. container { display: grid; grid-template-columns: 200px 1fr 2fr; /* First
column fixed at 200px, second and third flexible */ gap: 10px; }
.items-1{
grid-column-start: 1;
grid-column-end: 3;
}
grid-row-start / grid-row-end :
.items-2{
grid-column-start: 2 ;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
}
grid-column
Use the grid-column property to specify where to place an item.
.items-1{
grid-column: 1/4;
}
grid-row
Use the grid-row property to specify where to place an item.
.items-1{
grid-row: 1/4;
}