Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 4: Layouts, Positioning & Modern Flexbox
CSS ⏱ 14 min read

CSS Grid Layout: Grid Template, Areas & Gaps

While Flexbox handles one dimension (rows OR columns), CSS Grid is a full two-dimensional layout system (rows AND columns simultaneously).

What is it?

CSS Grid Layout is the most powerful layout system available in CSS, enabling developers to build complex magazine layouts, dashboards, and photo galleries with zero layout math.

Why is it used?

CSS Grid handles true 2D grids, overlapping elements, named grid areas, and responsive auto-fitting cards without writing media queries.

Detailed Explanation

Key Grid Properties: • `display: grid`: Converts element into a grid container. • `grid-template-columns`: Defines column sizes (e.g. `200px 1fr 1fr`). • The `fr` (Fractional) Unit: Represents a fraction of the available space inside the grid container. • `repeat(count, size)`: Helper function (e.g. `repeat(3, 1fr)` for 3 equal columns). • `grid-template-areas`: Allows naming areas of the layout (e.g. 'header header', 'sidebar main', 'footer footer'). • `grid-template-columns: repeat(auto-fit, minmax(280px, 1fr))`: The ultimate responsive grid trick—cards automatically wrap cleanly without a single media query!

Syntax Structure

Syntax Blueprint
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;
}

Code Example

CSS Code
/* Ultimate Auto-Responsive Card Grid (No Media Queries Required!) */
.courses-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  padding: 24px 0;
}

.course-card {
  background: #ffffff;
  border: 1px solid #eceae4;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.course-card .btn {
  margin-top: auto; /* Pushes CTA button cleanly to bottom of all cards */
}

Explanation of the Code

• `repeat(auto-fit, minmax(280px, 1fr))` creates as many 280px columns as fit in the screen, expanding equally to fill remaining width. • On mobile, it automatically collapses to 1 column. On 1080p, it displays 3-4 columns smoothly.

Expected Output

A multi-card grid that fluidly wraps from 4 columns on desktop, to 2 on tablets, to 1 on mobile with uniform card heights.

Real-World Application

Used across course catalogs (Udemy, IGM Computers), photo galleries (Pinterest), and product listings (Amazon).

Common Mistakes to Avoid

• Using CSS Grid for simple horizontal navbars (Flexbox is much simpler for 1D navigation). • Forgetting `gap` and attempting to use margins on grid items.

Quick Practice Challenge

Create a 3-column photo gallery with 16px gaps between cards using `display: grid` and `repeat(3, 1fr)`.

Key Takeaways

• CSS Grid is a 2-dimensional layout system (rows & columns). • The `fr` unit represents flexible fractions of available container space. • `repeat(auto-fit, minmax(280px, 1fr))` creates responsive grids without media queries.