A Complete Guide to CSS Grid

CSS Grid is a powerful tool for creating flexible and responsive layouts in web design. It allows you to create complex layouts with ease by defining rows and columns in a grid, and placing content within those cells. In this guide, we will cover the basics of CSS Grid and some common use cases.

Basic Grid Layout

To create a CSS Grid layout, we need to define a grid container and the grid items within it. Here’s an example of a basic grid layout:

html

<div class="container">
<div class="item item-1">1</div>
<div class="item item-2">2</div>
<div class="item item-3">3</div>
<div class="item item-4">4</div>
<div class="item item-5">5</div>
<div class="item item-6">6</div>
<div class="item item-7">7</div>
<div class="item item-8">8</div>
<div class="item item-9">9</div>
</div>

css

.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
gap: 10px;
}
.item {
background-color: #eee;
padding: 10px;
font-size: 24px;
}

In this example, we define a grid container with 3 columns and 3 rows, using the grid-template-columns and grid-template-rows properties. We also add some gap between the grid items using the gap property. Finally, we define the styling for the grid items.

Grid Lines and Areas

CSS Grid allows you to define grid lines and grid areas within the grid container, which makes it easier to place content in specific areas of the layout. Here’s an example:

html

<div class="container">
<div class="item item-1">1</div>
<div class="item item-2">2</div>
<div class="item item-3">3</div>
<div class="item item-4">4</div>
<div class="item item-5">5</div>
<div class="item item-6">6</div>
</div>

css

.container{
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 100px 100px 100px;
gap: 10px;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}
.item-1 {
grid-area: header;
background-color: #aaa;
}
.item-2 {
grid-area: sidebar;
background-color: #bbb;
}
.item-3 {
grid-area: main;
background-color: #ccc;
}
.item-4 {
grid-area: footer;
background-color: #ddd;
}

In this example, we define grid lines using the grid-template-areas property, which allows us to name specific areas of the grid container. We then assign each grid item to a specific area using the grid-area property. This makes it easy to create complex layouts with content in specific areas of the grid.

Auto-Fit and Auto-Fill

CSS Grid also provides the auto-fit and auto-fill keywords, which make it easier to create flexible and responsive layouts. Here’s an example:

html

<div class="container">
<div class="item item-1">1</div>
<div class="item item-2">2</div>
<div class="item item-3">3</div>
<div class="item item-4">4</div>
<div class="item item-5">5</div>
<div class="item item-6">6</div>
<div class="item item-7">7</div>
<div class="item item-8">8</div>
<div class="item item-9">9</div>
</div>

css

.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
}
.item {
background-color: #eee;
padding: 10px;
font-size: 24px;
}

In this example, we use the repeat function with the auto-fit keyword to create as many columns as possible while still maintaining a minimum width of 200 pixels for each column. This ensures that the grid items will always fit within the container and adjust their size as necessary. The minmax function specifies a range of values that the grid item can take, ensuring that the grid item is never smaller than 200 pixels and can grow to fill the available space.

CSS Grid is a powerful tool for creating flexible and responsive layouts in web design. It allows you to define complex layouts with ease by defining rows and columns in a grid, and placing content within those cells. With CSS Grid, you can create layouts that adapt to different screen sizes and orientations, making it easier to create a consistent user experience across devices.