Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Grid – Fundamentals | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 27 of 50

·css

CSS Grid – Fundamentals

15 mins

Course progress54%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Implicit grid tracks are created automatically for items placed outside the defined grid.
  • `fr` unit distributes free space proportionally.
  • Combine Grid with Flexbox inside grid items for complex UI.

CSS Grid is a two‑dimensional layout system that gives you rows and columns at once – perfect for complex page skeletons.

Creating a Grid Container

.container {
  display:grid;                     /* turns on grid layout */
  grid-template-columns: repeat(12, 1fr); /* 12 equal tracks */
  gap:1rem;                         /* space between cells */
}
  • grid-template-columns defines track sizes (columns). 1fr distributes free space equally.
  • grid-template-rows works the same for vertical tracks.
  • gap (formerly grid-gap) adds a gutter; works for both axes.

Placing Items

.item {
  grid-column: 2 / span 4;   /* start at column‑2, span 4 columns */
  grid-row: 1 / 3;           /* from row‑1 to row‑3 (exclusive) */
}

You can also use the shorthand grid-area to define both column and row at once.

Example – Simple 12‑Column Layout

<div class="container">
  <aside class="sidebar">Sidebar</aside>
  <main class="main">Main content</main>
</div>
.container { display:grid; grid-template-columns: repeat(12,1fr); gap:1rem; }
.sidebar { grid-column:1 / span 3; }
.main    { grid-column:4 / span 9; }

Tips & Gotchas

  • Implicit grid – If you place an item outside the defined tracks, the browser automatically creates additional tracks. Use grid-auto-rows/grid-auto-columns to control their size.
  • fr unit – Unlike percentages, fr units are relative to the free space after deducting non‑flexible tracks (e.g., fixed‑width columns).
  • Avoid nesting too many grids – each grid creates a new formatting context; keep nesting shallow for performance.

Accessibility Note

When you rearrange DOM order with grid-column/grid-row, screen readers still read the source order. Be mindful of the logical flow; avoid reordering critical navigation without ARIA adjustments.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonLogical Properties & Writing ModesContinue to lesson 28Advanced Grid – Named Areas & Sub‑GridsBack to CSS Basics to Advanced curriculum