Academic Block

CSS Grid Get Certified

What is CSS Grid?

CSS Grid is a two-dimensional layout system used to create rows and columns on a webpage. It makes it easy to build responsive layouts by placing elements in a grid structure. Unlike Flexbox, which arranges items in one direction (row or column), CSS Grid controls both rows and columns at the same time.

Why Use CSS Grid?

CSS Grid simplifies complex webpage layouts without using floats or positioning. It allows you to create responsive page structures, dashboards, galleries, cards, and other layouts with clean and readable CSS code.

Basic Grid Syntax

What this code does: Turns a parent element into a grid container with display:grid, so its children become grid items that can be arranged in rows and columns.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-container {
    display: grid;
    gap: 10px;
}
.demo-container div {
    background: #e0f2fe;
    padding: 16px;
    border-radius: 6px;
    text-align: center;
}
</style>
</head>
<body>
<div class="demo-container">
    <div>Item 1</div>
    <div>Item 2</div>
    <div>Item 3</div>
</div>
</body>
</html>

Step 1: Create a Grid Container

Add display:grid; to the parent element. Then use the grid-template-columns property to define how many columns your grid should have.

What this code does: Creates a 3-column equal-width grid with a small gap between items and styles each cell as a blue rounded card.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}
.demo-item {
    background: #007BFF;
    color: white;
    padding: 20px;
    text-align: center;
    border-radius: 5px;
}
</style>
</head>
<body>
<div class="demo-container">
    <div class="demo-item">1</div>
    <div class="demo-item">2</div>
    <div class="demo-item">3</div>
    <div class="demo-item">4</div>
    <div class="demo-item">5</div>
    <div class="demo-item">6</div>
</div>
</body>
</html>

Step 2: Change the Number of Columns

The grid-template-columns property controls how many columns appear in the grid. Change the number of values to create different layouts.

What this code does: Builds a 2-column grid so four items flow into two rows of two, with green cards and spacing between them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}
.demo-item {
    background: #28a745;
    color: white;
    padding: 25px;
    text-align: center;
    border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-container">
    <div class="demo-item">A</div>
    <div class="demo-item">B</div>
    <div class="demo-item">C</div>
    <div class="demo-item">D</div>
</div>
</body>
</html>

Common CSS Grid Properties

CSS Grid provides many useful properties for creating flexible layouts. The following example demonstrates some of the most commonly used Grid properties.

What this code does: Shows a 3-column grid where one item spans two columns and another spans two rows, using grid-column and grid-row.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
}
.demo-item {
    background: #ff9800;
    color: white;
    padding: 25px;
    text-align: center;
    border-radius: 6px;
}
.demo-wide {
    grid-column: span 2;
}
.demo-tall {
    grid-row: span 2;
}
</style>
</head>
<body>
<div class="demo-container">
    <div class="demo-item demo-wide">Wide</div>
    <div class="demo-item">2</div>
    <div class="demo-item demo-tall">Tall</div>
    <div class="demo-item">4</div>
    <div class="demo-item">5</div>
    <div class="demo-item">6</div>
</div>
</body>
</html>

Advantages of CSS Grid

Advantage
Benefit
Description
Two-Dimensional Layout
Flexible Design
Controls rows and columns at the same time.
Responsive Layout
Mobile Friendly
Creates responsive layouts with less code.
Easy Positioning
Better Control
Place items exactly where you want them.
Cleaner Code
Easy Maintenance
Reduces the need for floats and complex positioning.
Powerful Layouts
Professional Design
Perfect for dashboards, galleries, and web page layouts.

Common Mistakes

Beginners often forget to add display:grid, define grid-template-columns, or confuse Grid with Flexbox. They may also use fixed column widths that don’t work well on smaller screens instead of responsive units like fr.

What this code does: Contrasts a broken rule set that only defines columns (without display:grid) against the correct version that activates the grid.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
/* Incorrect – missing display:grid */
/* .demo-container {
    grid-template-columns: 1fr 1fr;
} */
/* Correct */
.demo-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}
.demo-container div {
    background: #e0f2fe;
    padding: 20px;
    text-align: center;
    border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-container">
    <div>Item 1</div>
    <div>Item 2</div>
</div>
</body>
</html>

Best Practices

  • Always set display:grid on the parent container.
  • Use gap instead of margins to create spacing between grid items.
  • Use the fr unit for flexible column widths.
  • Prefer repeat() to avoid repeating the same values.
  • Use auto-fit or auto-fill for responsive grids.
  • Use CSS Grid for two-dimensional layouts.
  • Use Flexbox for one-dimensional layouts inside Grid items.
  • Test your grid layout on different screen sizes.

Web Resources on CSS Grid

1. MDN Web Docs : CSS grid layout
2. CSS Tricks.com : A Complete CSS Grid Layout Guide
3. Web.dev : Grid
4. Web Kit.org : Introducing CSS Grid Lanes

🧪 Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

📝 CSS Code
👁️ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.