DEV Community

Cover image for Introduction to CSS Grid: A Comprehensive Guide
joan?
joan?

Posted on

Introduction to CSS Grid: A Comprehensive Guide

CSS Grid is a layout system that allows you to create complex, grid-based designs with ease. It provides a two-dimensional grid structure, allowing you to arrange and position elements on both rows and columns. This makes it an excellent choice for creating responsive and flexible web layouts. In this guide, we'll explore the key concepts of CSS Grid and demonstrate how to use it with practical code examples.

Getting Started with CSS Grid

Before we dive into the details of CSS Grid, let's first understand the basic structure and terminology associated with it.

Grid Container and Grid Items

CSS Grid consists of two main components: the grid container and the grid items. The grid container is the parent element that holds all the grid items. It's defined by setting its display property to grid or inline-grid.

.grid-container {
  display: grid;
}
Enter fullscreen mode Exit fullscreen mode

Grid items, on the other hand, are the child elements of the grid container. They are the elements that will be positioned within the grid. To specify an element as a grid item, you need to place it directly inside the grid container.

<div class="grid-container">
  <div class="grid-item">Item 1</div>
  <div class="grid-item">Item 2</div>
  <div class="grid-item">Item 3</div>
</div>
Enter fullscreen mode Exit fullscreen mode

Grid Lines and Grid Tracks

CSS Grid introduces the concept of grid lines and grid tracks. Grid lines are the horizontal and vertical lines that define the boundaries of the grid. Grid tracks are the spaces between these grid lines.

Consider a simple grid with three rows and three columns:

In this example, we have three rows and three columns, each with a height or width of 100 pixels. The grid-template-rows and grid-template-columns properties define the size and number of rows and columns, respectively.

Grid Areas

Grid areas provide a convenient way to name and reference specific areas within the grid. You can assign a name to a grid area by using the grid-template-areas property on the grid container. Each grid item can then be positioned within these named areas.

.grid-container {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
}

.grid-item {
  grid-area: header;
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we have defined a grid with three rows and two columns. The grid-template-areas property defines the layout by specifying the names of the areas. Each grid item can then be positioned within its corresponding area using the grid-area property.

Grid Properties and Values

CSS Grid provides a wide range of properties and values that allow you to customize the layout and behavior of your grid. Let's explore some of the most commonly used ones:

grid-template-rows and grid-template-columns

The grid-template-rows and grid-template-columns properties are used to define the size and number of rows and columns in the grid. You can specify the sizes using different units like pixels, percentages, or the fr unit.

.grid-container {
  display: grid;
  grid-template-rows: 100px 200px;
  grid-template-columns: 1fr 2fr;
}
Enter fullscreen mode Exit fullscreen mode

grid-template-areas

The grid-template-areas property allows you to define the layout of the grid using named grid areas. You can specify the areas using a grid template syntax, where each line represents a row and each word represents a grid item.

.grid-container {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
}
Enter fullscreen mode Exit fullscreen mode

grid-gap

The grid-gap property sets the size of the gap between grid items. It accepts two values: the first one represents the gap between rows, and the second one represents the gap between columns.

.grid-container {
  display: grid;
  grid-gap: 10px 20px;
}
Enter fullscreen mode Exit fullscreen mode

grid-row and grid-column

The grid-row and grid-column properties allow you to position grid items on specific rows and columns. You can specify the starting and ending positions of the grid item using line numbers or named lines.

.grid-item {
  grid-row: 2 / 4; /* Starts at row line 2 and ends at row line 4 */
  grid-column: sidebar; /* Placed in the column named "sidebar" */
}
Enter fullscreen mode Exit fullscreen mode

justify-items and align-items

The justify-items and align-items properties define how grid items are positioned along the row and column axes, respectively. You can use different values like start, end, center, stretch, and baseline to control the alignment.

.grid-container {
  display: grid;
  justify-items: center; /* Aligns items along the row axis */
  align-items: center; /* Aligns items along the column axis */
}
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Now that we have covered the fundamental concepts and properties of CSS Grid, let's see some practical examples to demonstrate its usage.

Example 1: Simple Grid Layout

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

.grid-item {
  background-color: #eaeaea;
  padding: 20px;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we create a grid with three equally sized columns using grid-template-columns and a gap of 10 pixels between the grid items using grid-gap. Each grid item has a background color and padding applied to it.

Example 2: Responsive Grid Layout

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

In this example, we create a responsive grid layout using the repeat() and minmax() functions. The auto-fit value allows the grid to automatically adjust the number of columns based on the available space. The minmax() function sets a minimum and maximum size for each column, ensuring that they don't become too small or too large.

Conclusion

CSS Grid is a powerful layout system that provides flexibility and control over web page designs. It allows you to create responsive and complex layouts with ease. By understanding the key concepts and properties of CSS Grid, you can unlock its full potential and create visually stunning web layouts.

In this article, we have covered the basics of CSS Grid, including grid containers, grid items, grid lines, and grid tracks. We have also explored various properties and values that enable you to customize the layout and behavior of your grid. Finally, we provided practical examples to demonstrate the usage of CSS Grid in real-world scenarios.

CSS Grid opens up endless possibilities for designing modern and responsive web layouts.

I hope this article helps you understand the concepts of CSS Grid better. If you have any further questions, feel free to ask!

Connect with me on twitter and threads

Top comments (6)

Collapse
 
sharmi2020 profile image
Sharmila kannan

Nice work! Learnt more about grid

Collapse
 
joanayebola profile image
joan?

I'm glad you learnt from the article

Collapse
 
joanayebola profile image
joan?

Thank you

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
pama01 profile image
Shanel

have been reading different posts on css but htis one is easy to understand and i love it. wil implement some of them on my news site to make it look beautiful

Collapse
 
joanayebola profile image
joan?

Thank you so much for your comment! I'm glad you found my article on CSS Grid easy to understand and helpful. I'm always happy to hear that my work is making a difference and I'm especially excited to hear that you're planning to implement some of the techniques in your site.