Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is Grid in CSS?

In this blog post, we will explore the concept of Grid in CSS. As a beginner in programming, you might have come across the need to design a responsive and organized layout for your website. You may have tried using other layout methods such as Flexbox, but let's dive into the world of CSS Grid and see how it can help you create an efficient and aesthetic design.

What is CSS Grid?

CSS Grid is a two-dimensional layout system that allows you to arrange elements on your webpage in columns and rows. It is one of the most powerful layout techniques available in CSS, making it easier to design complex and responsive web designs.

Before we go further, let's understand some essential terms related to Grid:

  • Grid Container: The HTML element on which you apply display: grid; is known as the Grid Container. It is the parent element that contains all the grid items.
  • Grid Item: The child elements of the Grid Container are known as Grid Items.
  • Grid Line: The horizontal and vertical lines that divide the grid into rows and columns are called Grid Lines.
  • Grid Track: The space between two consecutive grid lines, either horizontal or vertical, is called a Grid Track.
  • Grid Cell: The intersection of a grid row and a grid column forms a Grid Cell.

Now that we have a basic understanding of the grid-related terms, let's look at how to create a simple grid layout.

Creating a Basic Grid Layout

Here's a simple HTML structure with a parent div element and four child div elements:

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

To create a grid layout, we need to define the grid container in our CSS file:

.grid-container {
  display: grid;
}

By using display: grid;, we have turned the parent div element into a grid container. Now, let's define the grid template columns and rows to specify the size and number of columns and rows in our grid.

.grid-container {
  display: grid;
  grid-template-columns: 200px 200px;
  grid-template-rows: 100px 100px;
}

In this example, we've created a grid with two columns, each 200px wide, and two rows, each 100px tall. The grid items will now be placed inside the grid cells.

Positioning Grid Items

By default, grid items are placed in the grid cells from left to right and from top to bottom. However, you can control the position of the grid items using the grid-column and grid-row properties.

For example, let's say we want to place the first grid item in the second column of the second row. We can do this by adding the following CSS:

.grid-item:nth-child(1) {
  grid-column: 2;
  grid-row: 2;
}

Here, we used the :nth-child() selector to target the first grid item and specified its position using the grid-column and grid-row properties.

Grid Gaps

You might want to add some space between your grid items to make your layout more appealing. This can be done using the grid-gap, grid-row-gap, and grid-column-gap properties.

For example, let's add a 10px gap between the grid items:

.grid-container {
  display: grid;
  grid-template-columns: 200px 200px;
  grid-template-rows: 100px 100px;
  grid-gap: 10px;
}

Using grid-gap: 10px;, we've added a 10px gap between both rows and columns. If you want to add different gaps for rows and columns, you can use grid-row-gap and grid-column-gap properties.

The fr Unit

In the previous examples, we used fixed sizes for our grid columns and rows. However, this might not be ideal for responsive designs. To create a more flexible grid layout, we can use the fr unit, which stands for "fraction."

The fr unit represents a fraction of the available space in the grid container. For example, if we want to divide our grid container into three equal-width columns, we can use the following CSS:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

By using 1fr for each column, we've divided the grid container's width into three equal parts.

Responsive Grid Layout

To make our grid layout responsive, we can use the repeat() function and the auto-fit or auto-fill keywords with the minmax() function.

For example, let's create a grid layout with responsive columns:

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  grid-gap: 10px;
}

Here, we've used the repeat() function to create as many columns as can fit in the grid container. The minmax() function sets the minimum width of the column to 200px and the maximum width to 1fr, which means it can grow to fill the available space.

Now, when you resize your browser window, the grid layout will adjust accordingly, making it fully responsive.

Conclusion

In this blog post, we've learned about the CSS Grid layout and how to create a simple and responsive grid layout. We've covered essential grid-related terms and properties, positioning of grid items, grid gaps, the fr unit, and making a grid layout responsive.

CSS Grid is a powerful tool for creating complex and responsive web designs. As you continue your programming journey, we encourage you to experiment with CSS Grid and explore its potential to create stunning and efficient layouts.