CSS Grid Layout, introduced in 2017, has revolutionized the way we design and layout web pages. It's a powerful tool that allows us to create two-dimensional layouts with ease, making it a favorite among web developers. In this article, we'll explore various CSS Grid Layout examples to help you understand and implement this fantastic feature.
Understanding CSS Grid Layout
Before diving into examples, let's briefly understand CSS Grid Layout. It's a two-dimensional layout system that allows us to position elements in both row and column directions. Unlike Flexbox, which is a one-dimensional layout system, CSS Grid is designed for two-dimensional layouts like grids.
Key Concepts
- Container and Items: The element with
display: grid;is the container, and its direct children are the grid items. - Grid Lines: Imagine a grid drawn over the container. These lines are where you can place your items.
- Grid Tracks and Cells: The space between grid lines is a grid track, and the intersection of horizontal and vertical tracks is a grid cell.
Setting Up a CSS Grid Layout
To start using CSS Grid, you need to set the display property of an element to grid or inline-grid. Here's a simple example:

```css .container { display: grid; grid-template-columns: 200px 100px; grid-gap: 10px; } ```
Grid Template Areas
One of the most powerful features of CSS Grid is the ability to define grid areas using named lines. Here's an example:
```css .container { display: grid; grid-template-areas: "header header" "sidebar content" "footer footer"; } ```
Responsive CSS Grid Layout
CSS Grid is responsive by default, meaning it adjusts its layout based on the available space. Here's how you can create a responsive grid:
```css .container { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); grid-gap: 10px; } ```
Media Queries
You can also use media queries to change the grid layout at different screen sizes:

```css .container { display: grid; grid-template-columns: 1fr 1fr; } @media (max-width: 600px) { .container { grid-template-columns: 1fr; } } ```
CSS Grid Layout Examples
Now that we've covered the basics, let's look at some practical CSS Grid Layout examples.
Masonic Layout
CSS Grid makes creating a masonic layout, where items of different sizes are arranged in a grid, easy:
```css .container { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); grid-gap: 10px; } ```
Responsive Header and Footer
You can use CSS Grid to create a responsive header and footer that always stay at the top and bottom of the page:

```css body { display: grid; grid-template-rows: auto 1fr auto; min-height: 100vh; } ```
Equal Height Columns
CSS Grid allows you to create equal height columns, even if their content is of different heights:
```css .container { display: grid; grid-template-columns: 1fr 1fr; grid-gap: 10px; } .container > div { min-height: 100px; } ```
Browser Support
CSS Grid is well-supported across modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. However, it's always a good idea to check the caniuse.com website for the latest browser support data.
CSS Grid Layout is a powerful tool that simplifies web layout. With these examples, you should now be able to create complex, responsive layouts with ease. Happy coding!





















