Grid in CSS: A Comprehensive Guide

arsalanmee

Arsalan Mlaik

Posted on August 7, 2023

Grid in CSS: A Comprehensive Guide

Online designers can now easily create flexible, adaptable, and complicated grid-based layouts. Thanks to CSS Grid, a powerful layout technology that revolutionized web design. In this guide, We ll discuss basic to reach information about CSS grid.

1. Introduction to CSS Grid

CSS Grid is a two-dimensional layout system that allows you to create grid-based layouts for your web pages. Unlike the traditional one-dimensional layout methods, CSS Grid enables precise control over rows, columns, and their intersections, giving you the freedom to design complex and dynamic layouts.

2. Understanding Grid Terminology

Before diving into CSS Grid, let's familiarize ourselves with some key terms related to this layout system:

2.1 Grid Container

The element on which the grid is defined is known as the Grid Container. It serves as the parent element that holds all the grid items within the layout.

2.2 Grid Items

Grid Items are the individual children elements inside the Grid Container that occupy the cells of the grid.

2.3 Grid Lines

Grid Lines are the dividing lines that form the vertical and horizontal boundaries of each cell in the grid. They separate the rows and columns.

2.4 Grid Tracks

The spaces between two adjacent grid lines are called Grid Tracks. These can be rows or columns and define the size of each cell in the grid.

2.5 Grid Areas

A Grid Area is a rectangular area surrounded by four grid lines. It can contain one or more cells, defining the placement of multiple grid items simultaneously.

3. Creating a Basic Grid Layout

Let's start with a simple example to understand how CSS Grid works. We'll create a basic two-column layout.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <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>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr; /* Two equal columns */
  grid-gap: 10px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}

.grid-item {
  padding: 20px;
  text-align: center;
  font-size: 18px;
  background-color: #fff;
  border: 1px solid #ccc;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have an HTML file with a "grid-container" containing four "grid-item" elements. The CSS code uses display: grid; to convert the container into a grid container with two equal columns using grid-template-columns: 1fr 1fr;. Each grid item has a 10-pixel gap between them with grid-gap: 10px;, and we've added some styling for visual appeal.

When you open this HTML file in your browser, you'll see a simple two-column layout with four grid items arranged in a grid, each occupying an equal width of the container. The grid items are separated by a 10-pixel gap, and they have a clean, minimalistic appearance.

4. Defining Grid Rows and Columns

CSS Grid allows you to define the number and size of rows and columns within the grid.

4.1 Explicit Grid

Explicit grid definition involves specifying the exact size of each row and column.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Explicit Grid</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-explicit">
    <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>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-explicit {
  display: grid;
  grid-template-columns: 100px 200px 150px; /* Three explicit columns */
  grid-template-rows: 50px 100px 75px; /* Three explicit rows */
  grid-gap: 10px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've defined three explicit columns and three explicit rows, each with a specified size. The grid items will be placed within this defined grid.

4.2 Implicit Grid

When the number of grid items exceeds the number of explicitly defined rows and columns, the implicit grid comes into play. It automatically generates additional rows and columns to accommodate the excess items.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Implicit Grid</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-implicit">
    <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 class="grid-item">5</div>
  </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-implicit {
  display: grid;
  grid-template-columns: 100px 200px 150px; /* Three explicit columns */
  grid-gap: 10px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've defined three explicit columns, but we have five grid items. As a result, the implicit grid will create additional rows to accommodate the extra items.

5. Grid Lines and Gaps

CSS Grid allows you to adjust the size of grid lines and define gaps between grid items to achieve the desired layout.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Grid Lines and Gaps

</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-lines-gaps">
    <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>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-lines-gaps {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Three equal columns */
  grid-gap: 20px 10px; /* Vertical and horizontal gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've used grid-template-columns: repeat(3, 1fr); to create three equal columns. We've set grid-gap to 20px 10px, which creates a vertical gap of 20 pixels and a horizontal gap of 10 pixels between grid items.

6. Placing Grid Items

CSS Grid provides multiple ways to place grid items within the grid container.

6.1 Using Line-Based Placement

You can place items based on grid line numbers.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Line-Based Placement</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-line-placement">
    <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>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-line-placement {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Three equal columns */
  grid-gap: 20px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}

.grid-item:nth-child(2) {
  grid-column: 1 / span 2; /* Place the second item in columns 1 and 2 */
}

.grid-item:nth-child(4) {
  grid-row: 2 / span 2; /* Place the fourth item in rows 2 and 3 */
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've created three equal columns using grid-template-columns: repeat(3, 1fr);. We've then used the grid-column property to place the second grid item in columns 1 and 2, and the grid-row property to place the fourth grid item in rows 2 and 3.

6.2 Using Grid Area Names

Assigning grid items to specific grid areas defined in the grid template.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Grid Area Names</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-area-names">
    <div class="grid-item item1">1</div>
    <div class="grid-item item2">2</div>
    <div class="grid-item item3">3</div>
    <div class="grid-item item4">4</div>
  </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-area-names {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* Two equal columns */
  grid-gap: 20px; /* Gap between grid items */
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer"; /* Define grid areas */
  background-color: #f0f0f0;
  padding: 20px;
}

.item1 {
  grid-area: header; /* Place item1 in the 'header' area */
}

.item2 {
  grid-area: sidebar; /* Place item2 in the 'sidebar' area */
}

.item3 {
  grid-area: main; /* Place item3 in the 'main' area */
}

.item4 {
  grid-area: footer; /* Place item4 in the 'footer' area */
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've created a grid with two equal columns using grid-template-columns: repeat(2, 1fr);. We've then defined the grid areas using grid-template-areas with area names: "header," "sidebar," "main," and "footer." The grid items are placed in their respective areas using grid-area property.

7. Grid Template Areas

Grid Template Areas allow for a more visual way to define the layout using ASCII art.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Grid Template Areas</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-template-areas">
    <div class="grid-item item1">Header</div>
    <div class="grid-item item2">Sidebar</div>
    <div class="grid-item item3">Main Content</div>
    <div class="grid-item item4">Footer</div>
  </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-template-areas {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer"; /* Define grid areas */
  grid-template-columns: repeat(3, 1fr); /* Three equal columns */
  grid-gap: 20px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}

.item1 {
  grid-area: header; /* Place item1 in the 'header' area */
}

.item2 {
  grid-area: sidebar; /* Place item2 in the 'sidebar' area */
}

.item3 {
  grid-area: main; /* Place item3 in the 'main' area */
}

.item4 {
  grid-area: footer; /* Place item4 in the 'footer' area */
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've used grid-template-areas to visually define the layout of the grid using ASCII art. Each line of the template string represents a row, and each character represents a grid area. The grid items are placed in their respective areas using the grid-area property.

8. CSS Grid Alignment

CSS Grid provides various alignment options for both grid items and grid containers.

8.1 Aligning along the Row Axis

Adjusting the horizontal alignment of grid items.

<!DOCTYPE html>
<html lang="en

">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Row Alignment</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-row-alignment">
    <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>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-row-alignment {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Three equal columns */
  grid-gap: 20px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
  justify-items: center; /* Horizontally center grid items */
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've used justify-items: center; to horizontally center the grid items along the row axis.

8.2 Aligning along the Column Axis

Adjusting the vertical alignment of grid items.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Column Alignment</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-column-alignment">
    <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>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-column-alignment {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Three equal columns */
  grid-gap: 20px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
  align-items: center; /* Vertically center grid items */
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've used align-items: center; to vertically center the grid items along the column axis.

9. Nesting Grids

CSS Grid allows you to nest grids, enabling you to create intricate layouts within larger grid areas.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Nested Grids</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-nesting">
    <div class="grid-item outer-item">
      <div class="inner-grid">
        <div class="grid-item inner-item">A</div>
        <div class="grid-item inner-item">B</div>
      </div>
    </div>
    <div class="grid-item outer-item">
      <div class="inner-grid">
        <div class="grid-item inner-item">C</div>
        <div class="grid-item inner-item">D</div>
      </div>
    </div>
  </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-nesting {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* Two equal columns */
  grid-gap: 20px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}

.outer-item {
  padding: 20px;
  background-color: #fff;
  border: 1px solid #ccc;
}

.inner-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* Two equal columns */
  grid-gap: 10px; /* Gap between grid items */
}

.inner-item {
  padding: 20px;
  text-align: center;
  font-size: 18px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've created an outer grid container with two equal columns using grid-template-columns: repeat(2, 1fr);. Inside each outer item, we've placed an inner grid container with two equal columns using grid-template-columns: repeat(2, 1fr);. This results in a nested grid layout with smaller grids within each outer item.

10. CSS Grid and Responsive Design

CSS Grid plays a significant role in building responsive websites, adapting well to different screen sizes.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Grid Example - Responsive Design</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="grid-container-responsive">
    <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>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
/* styles.css */
.grid-container-responsive {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* Responsive grid with minimum 200px and maximum equal columns */
  grid-gap: 20px; /* Gap between grid items */
  background-color: #f0f0f0;
  padding: 20px;
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've used grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); to create a responsive grid. The auto-fit keyword allows the grid to automatically adjust the number of columns based on the available space, ensuring that the grid items maintain a minimum width of 200 pixels and equal width for maximum responsiveness.

11. Browser Support for CSS Grid

CSS Grid is widely supported by modern browsers, making it an excellent choice for creating versatile layouts.

12. Advantages of Using CSS Grid

Let's explore the advantages of using CSS Grid over other layout methods.

12.1 Simplified Layout Structure

CSS Grid simplifies the layout structure, reducing the need for complex CSS hacks and nested HTML elements.

12.2 Flexible and Responsive

With CSS Grid, layouts automatically adjust to different screen sizes and devices, making them highly responsive.

12.3 Grid Items Control

CSS Grid gives you precise control over the placement and alignment of grid items, ensuring consistent and visually appealing layouts.

12.4 Intuitive Development Process

The visual nature of CSS Grid makes it intuitive and easier to develop complex layouts efficiently.

13. CSS Grid vs. Flexbox

CSS Grid and Flexbox are both powerful layout tools, but they

serve different purposes.

13.1 CSS Grid

  • Two-dimensional layout system for creating grid-based layouts.
  • Ideal for creating complex, grid-like structures with rows and columns.
  • Best suited for overall page layout.

13.2 Flexbox

  • One-dimensional layout system for arranging items in a single direction (row or column).
  • Perfect for creating flexible and dynamic content within a container.
  • Best suited for creating responsive components and alignments.

14. Common CSS Grid Mistakes to Avoid

Let's go over some common pitfalls and mistakes when using CSS Grid.

14.1 Overcomplicating Grids

Avoid overcomplicating grids by creating too many tracks or using unnecessary properties.

14.2 Ignoring Browser Compatibility

Check for browser compatibility to ensure consistent behavior across different browsers.

14.3 Not Considering Responsive Design

Always design with responsiveness in mind to adapt the layout to various screen sizes.

15. Conclusion

In conclusion, CSS Grid is a game-changer for web layout design, providing a versatile and intuitive way to structure web pages. By mastering CSS Grid, you can create visually stunning and responsive websites with ease.

FAQs

  1. Is CSS Grid supported by all modern browsers?
    Yes, CSS Grid is supported by all major modern browsers, including Chrome, Firefox, Safari, and Edge.

  2. Can I use CSS Grid and Flexbox together?
    Absolutely! CSS Grid and Flexbox are complementary layout systems and can be used together to achieve more complex layouts.

  3. Is CSS Grid mobile-friendly?
    Yes, CSS Grid is highly responsive and adapts well to various screen sizes, making it ideal for mobile-friendly designs.

  4. Are there any performance concerns with using CSS Grid?
    No, CSS Grid is performant and generally outperforms traditional layout methods like floats.

  5. Where can I learn more about advanced CSS Grid techniques?
    You can find plenty of tutorials and resources online to explore more advanced CSS Grid techniques and examples.

💖 💪 🙅 🚩
arsalanmee
Arsalan Mlaik

Posted on August 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related