CSS Grid
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;
}
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. |
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. |
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. |
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;
}
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);
}
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. |
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 |
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";
}
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;
}
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
frunit for flexible columns. - Use
gapinstead of margins for spacing. - Use
grid-template-areasfor readable layouts. - Combine Grid with media queries for responsiveness.
- Keep layouts simple and organized.
- Test your layout on different screen sizes.
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;
}
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 |
14. CSS Grid Interview Questions
- What is CSS Grid?
- What is the purpose of
display:grid;? - What is the difference between Grid and Flexbox?
- What does
grid-template-columnsdo? - What does the
frunit represent? - What is the purpose of the
gapproperty? - What is
grid-columnused for? - What is
grid-rowused for? - What are
grid-template-areas? - When should you use CSS Grid?
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.
Quick Quiz
Which CSS property creates a Grid container?