CSS Grid

CSS Grid Layout is a powerful two-dimensional layout system that allows developers to arrange elements into rows and columns with ease.

1. Introduction to CSS Grid

CSS Grid is a layout module that makes it easy to design responsive web pages. Unlike Flexbox, which works in one direction, Grid works in both rows and columns.

Advantages of CSS Grid
  • Create two-dimensional layouts.
  • Easy row and column management.
  • Responsive web design.
  • Less CSS code for complex layouts.
  • Precise control over element placement.

.container{

    display:grid;

}
CSS Grid is ideal for building complete webpage layouts.

2. Grid Container (display: grid)

To create a Grid layout, apply display:grid; to the parent element. All direct child elements become grid items.


.container{

    display:grid;

}
Property Description
display:grid Creates a Grid container.
Grid Item Every direct child becomes a grid item.
Without display:grid;, Grid properties will not work.

3. Grid Columns (grid-template-columns)

The grid-template-columns property defines the number and width of columns in a Grid container.


.container{

    display:grid;

    grid-template-columns:1fr 1fr 1fr;

}
Example Description
1fr 1fr Two equal columns.
1fr 2fr Second column is twice as wide.
200px 1fr Fixed and flexible column.
The fr unit represents a fraction of the available space.

4. Grid Rows (grid-template-rows)

The grid-template-rows property defines the height and number of rows in a Grid layout.


.container{

    display:grid;

    grid-template-rows:100px 200px;

}
Example Description
100px 100px Two rows of equal height.
100px auto First row fixed, second adjusts automatically.
1fr 2fr Rows share available height proportionally.
Use grid-template-rows to organize content vertically.

5. Gap Property (gap)

The gap property adds spacing between rows and columns without using margins.


.container{

    display:grid;

    gap:20px;

}
Property Description
gap Adds space between rows and columns.
row-gap Controls vertical spacing.
column-gap Controls horizontal spacing.

.gallery{

    display:grid;

    grid-template-columns:repeat(3,1fr);

    gap:15px;

}
The gap property makes Grid layouts cleaner and easier to maintain.

6. Grid Items

Every direct child of a Grid container is called a Grid Item. Grid items can be positioned anywhere inside the grid.


Item 1
Item 2
Item 3

.container{

    display:grid;

    grid-template-columns:repeat(3,1fr);

}
Every direct child automatically becomes a Grid Item.

7. Grid Column & Grid Row

The grid-column and grid-row properties allow an item to span across multiple columns or rows.


.item{

    grid-column:1 / 3;

    grid-row:1 / 2;

}
Property Purpose
grid-column Controls column span.
grid-row Controls row span.
These properties help create magazine-style layouts.

8. Grid Area

The grid-area property assigns a name to a grid item so it can be placed easily inside a layout.


.header{

    grid-area:header;

}

.container{

    display:grid;

    grid-template-areas:

    "header header"

    "menu content"

    "footer footer";

}
Area Purpose
header Top section
menu Navigation
content Main content
footer Bottom section
Named grid areas improve the readability of your layouts.

9. Practical CSS Grid Examples

Example 1: Three Column Layout

.container{

    display:grid;

    grid-template-columns:1fr 1fr 1fr;

    gap:20px;

}

Example 2: Responsive Gallery

.gallery{

    display:grid;

    grid-template-columns:repeat(auto-fit,minmax(200px,1fr));

    gap:15px;

}

Example 3: Website Layout

.container{

    display:grid;

    grid-template-areas:

    "header header"

    "sidebar content"

    "footer footer";

}
CSS Grid is widely used for dashboards, image galleries, landing pages, and complete website layouts.

10. CSS Grid Properties Comparison Table

Property Purpose
display:grid Creates a Grid container.
grid-template-columns Defines columns.
grid-template-rows Defines rows.
gap Adds spacing.
grid-column Spans columns.
grid-row Spans rows.
grid-area Assigns named areas.

.container{

    display:grid;

    grid-template-columns:repeat(3,1fr);

    gap:20px;

}
CSS Grid provides powerful tools for creating responsive two-dimensional webpage layouts with minimal CSS.

11. CSS Grid Best Practices

CSS Grid is a powerful layout system. Following best practices helps create clean, responsive, and maintainable webpages.

  • Use Grid for two-dimensional layouts.
  • Use repeat() to reduce repetitive code.
  • Use the fr unit for flexible columns.
  • Use gap instead of margins for spacing.
  • Use grid-template-areas for readable layouts.
  • Combine Grid with media queries for responsiveness.
  • Keep layouts simple and organized.
  • Test your layout on different screen sizes.
CSS Grid makes complex webpage layouts easy to build and maintain.

12. Common CSS Grid Mistakes

Mistake Correct Practice
Using Grid without display:grid. Apply display:grid; first.
Using too many fixed pixel values. Use fr, auto, or percentages.
Ignoring responsive design. Use media queries or auto-fit.
Using margins instead of gap. Use the gap property.
Creating overly complex grids. Keep layouts simple and readable.

❌ Incorrect

.container{

    grid-template-columns:1fr 1fr;

}

✔ Correct

.container{

    display:grid;

    grid-template-columns:1fr 1fr;

}
Most Grid errors occur because the parent container is missing display:grid;.

13. Browser Support

CSS Grid is supported by all modern web browsers and is widely used for responsive website layouts.

Browser Support
Google Chrome ✔ Supported
Mozilla Firefox ✔ Supported
Microsoft Edge ✔ Supported
Safari ✔ Supported
Opera ✔ Supported
CSS Grid has excellent browser support for modern web development.

14. CSS Grid Interview Questions

  1. What is CSS Grid?
  2. What is the purpose of display:grid;?
  3. What is the difference between Grid and Flexbox?
  4. What does grid-template-columns do?
  5. What does the fr unit represent?
  6. What is the purpose of the gap property?
  7. What is grid-column used for?
  8. What is grid-row used for?
  9. What are grid-template-areas?
  10. When should you use CSS Grid?
These questions are frequently asked in Front-End Developer interviews.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Grid.
  • ✔ display:grid.
  • ✔ grid-template-columns.
  • ✔ grid-template-rows.
  • ✔ gap.
  • ✔ Grid Items.
  • ✔ grid-column and grid-row.
  • ✔ grid-area.
  • ✔ Practical Grid examples.
  • ✔ Best practices and common mistakes.
Congratulations! You have successfully completed the CSS Grid lesson. You can now create responsive two-dimensional layouts using CSS Grid.

Quick Quiz

Which CSS property creates a Grid container?