Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

CSS Grid — Complete Layout Guide with Examples

CSS Grid

CSS Grid Layout is a two-dimensional layout system for the web. It lets you control columns and rows at the same time, which makes it ideal for page layouts, dashboards, galleries, forms, tl-card collections, magazine layouts, and any design where items need to line up in both directions.

Grid is different from older layout techniques because the layout can be defined on the parent container. Instead of forcing every child element to manage its own position with floats, margins, or absolute positioning, Grid lets the tl-container describe the structure and then places items into that structure.

  • Use display: grid on the parent to create a grid container.
  • Define columns with grid-template-columns and rows with grid-template-rows.
  • Use gap for spacing between grid tracks.
  • Place items with line numbers, spans, or named grid areas.
  • Use repeat(), minmax(), auto-fit, and fr for responsive layouts.

Grid tl-container and Grid Items

A grid layout starts with a grid container. The direct children of that tl-container become grid items. Grid properties can be divided into tl-container properties and item properties.

Basic CSS Grid Setup
<div class="grid">
  <article class="tl-card">One</article>
  <article class="tl-card">Two</article>
  <article class="tl-card">Three</article>
  <article class="tl-card">Four</article>
</div>
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
}

.card {
  padding: 1rem;
  background: #e0f2fe;
  border: 1px solid #7dd3fc;
}

Grid Terminology

Before using Grid deeply, it helps to understand the words used in Grid documentation and browser DevTools.

Term Meaning
Grid containerThe parent element with display: grid or display: inline-grid.
Grid itemA direct child of the grid container.
Grid lineThe dividing line between rows or columns. Lines are numbered starting from 1.
Grid trackA tl-row or column between two grid lines.
Grid cellThe space where one tl-row and one column intersect.
Grid areaA rectangular area made from one or more grid cells.
Explicit gridRows and columns you define with template properties.
Implicit gridExtra rows or columns the browser creates when items do not fit the explicit grid.

Creating Columns and Rows

The most important Grid properties are grid-template-columns and grid-template-rows. They define the structure of the grid.

Grid Columns and Rows
.fixed-columns {
  display: grid;
  grid-template-columns: 200px 200px 200px;
}

.equal-columns {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.mixed-columns {
  display: grid;
  grid-template-columns: 220px 1fr 2fr;
}

.defined-rows {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 80px 200px 80px;
}

.template-shorthand {
  display: grid;
  grid-template: 80px 1fr 60px / 240px 1fr;
}

The fr Unit

The fr unit means a fraction of the available space. It is one of the main reasons Grid is pleasant to use. After fixed-width columns, gaps, and content constraints are calculated, the remaining space is divided among fr tracks.

Fractional Units
.three-equal-columns {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.sidebar-and-content {
  display: grid;
  grid-template-columns: 260px 1fr;
}

.weighted-columns {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  /* Middle column receives twice as much free space. */
}

repeat(), minmax(), auto-fit, and auto-fill

Grid has layout functions that reduce repetition and make responsive layouts easier. The most useful combination is repeat(auto-fit, minmax(...)), which creates as many columns as can fit without needing a media query.

Responsive Grid Tracks
<section class="product-grid">
  <article>Product 1</article>
  <article>Product 2</article>
  <article>Product 3</article>
  <article>Product 4</article>
</section>
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.25rem;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.75rem;
}
Function or Keyword Use
repeat(3, 1fr)Creates three equal tracks without writing 1fr three times.
minmax(240px, 1fr)Sets a track minimum and maximum size.
auto-fitCollapses empty tracks and stretches existing items.
auto-fillKeeps empty tracks in the tl-row when extra space exists.

Spacing with gap

Use gap to control space between rows and columns. It is cleaner than adding margins to grid items because it only affects the space between tracks.

Grid Gap
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.different-row-and-column-gaps {
  display: grid;
  row-gap: 1rem;
  column-gap: 2rem;
}

.gap-shorthand {
  display: grid;
  gap: 1rem 2rem;
  /* tl-row gap, then column gap */
}

Placing Grid Items by Line Numbers

Grid lines are numbered. A three-column grid has four vertical lines: line 1 before the first column, line 2 between the first and second columns, line 3 between the second and third columns, and line 4 after the third column.

Grid Item Placement
.layout {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 120px);
  gap: 1rem;
}

.featured {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}

.wide {
  grid-column: 2 / span 3;
}

.last-column {
  grid-column: 4;
  grid-row: 1 / span 2;
}

Named Grid Areas

Named grid areas let you describe a page layout visually in CSS. This is one of the clearest ways to build major page regions such as header, sidebar, main content, and footer.

Page Layout with Named Areas
<div class="page">
  <header class="site-header">Header</header>
  <aside class="sidebar">Sidebar</aside>
  <main class="content">Content</main>
  <footer class="site-footer">Footer</footer>
</div>
.page {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
}

.site-header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.site-footer { grid-area: footer; }

@media (max-width: 768px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "content"
      "sidebar"
      "footer";
  }
}

Alignment in CSS Grid

Grid has alignment properties for items inside their cells and for the whole grid inside its container. The place-* properties are shorthands that set both tl-row and column alignment.

Property Applies To Purpose
justify-itemsContainerAligns all items horizontally inside their cells.
align-itemsContainerAligns all items vertically inside their cells.
place-itemsContainerShorthand for align-items and justify-items.
justify-contentContainerAligns the whole grid horizontally when there is extra space.
align-contentContainerAligns the whole grid vertically when there is extra space.
justify-selfItemAligns one item horizontally inside its cell.
align-selfItemAligns one item vertically inside its cell.
Grid Alignment
.grid {
  display: grid;
  grid-template-columns: repeat(3, 120px);
  grid-template-rows: repeat(2, 100px);
  gap: 1rem;
  justify-content: center;
  align-content: center;
  place-items: center;
  min-height: 400px;
}

.special-item {
  justify-self: stretch;
  align-self: end;
}

Implicit Grid and Auto Placement

If you define three columns but have more items than fit in one row, the browser creates extra rows automatically. Those extra rows are part of the implicit grid. You can control their size with grid-auto-rows and the placement direction with grid-auto-flow.

Implicit Grid
.auto-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(120px, auto);
  gap: 1rem;
}

.column-flow {
  display: grid;
  grid-template-rows: repeat(3, 120px);
  grid-auto-flow: column;
  grid-auto-columns: 220px;
}

.dense-layout {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-flow: dense;
}

Responsive tl-card Grid

A responsive tl-card grid is one of the most common Grid patterns. It can usually be built without media queries by combining repeat(), auto-fit, and minmax().

Responsive Cards
<section class="cards">
  <article class="tl-card">
    <h4>Starter</h4>
    <p>For small projects.</p>
  </article>
  <article class="tl-card">
    <h4>Team</h4>
    <p>For growing teams.</p>
  </article>
  <article class="tl-card">
    <h4>Enterprise</h4>
    <p>For large organizations.</p>
  </article>
</section>
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

.card {
  display: grid;
  gap: 0.5rem;
  padding: 1rem;
  border: 1px solid #d1d5db;
  border-radius: 8px;
}

Dashboard Layout

Grid is very useful for dashboards because panels can span different rows and columns while still sharing one consistent layout system.

Dashboard Grid
<main class="dashboard">
  <section class="panel revenue">Revenue</section>
  <section class="panel users">Users</section>
  <section class="panel chart">Chart</section>
  <section class="panel activity">Activity</section>
</main>
.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 140px;
  gap: 1rem;
}

.revenue { grid-column: span 2; }
.users { grid-column: span 2; }
.chart {
  grid-column: span 3;
  grid-row: span 2;
}
.activity { grid-row: span 2; }

@media (max-width: 800px) {
  .dashboard {
    grid-template-columns: 1fr;
  }

  .revenue,
  .users,
  .chart,
  .activity {
    grid-column: auto;
    grid-row: auto;
  }
}

Holy Grail Layout

The classic holy grail layout has a header, footer, main content area, and sidebars. Grid can express this layout directly, including a mobile version.

Holy Grail Layout
.holy-grail {
  min-height: 100vh;
  display: grid;
  grid-template:
    "header header header" auto
    "left main right" 1fr
    "footer footer footer" auto
    / 220px minmax(0, 1fr) 220px;
}

.header { grid-area: header; }
.left-sidebar { grid-area: left; }
.main { grid-area: main; }
.right-sidebar { grid-area: right; }
.footer { grid-area: footer; }

@media (max-width: 900px) {
  .holy-grail {
    grid-template:
      "header" auto
      "main" 1fr
      "left" auto
      "right" auto
      "footer" auto
      / 1fr;
  }
}

Auto-fit vs Auto-fill

auto-fit and auto-fill are similar, but they behave differently when there is extra room. In most responsive tl-card layouts, auto-fit is the better starting choice because it collapses empty tracks and lets existing cards stretch.

Keyword Behavior Common Use
auto-fitEmpty tracks collapse and filled tracks stretch.Responsive cards, product grids, feature grids.
auto-fillEmpty tracks remain in the layout.Layouts where reserved empty columns are useful.

Grid vs Flexbox

Grid and Flexbox work very well together. Use Grid when the layout needs rows and columns. Use Flexbox when a component mainly flows in one direction.

Feature CSS Grid Flexbox
Layout typeTwo-dimensionalOne-dimensional
Best forPages, dashboards, galleries, full layoutsNavbars, toolbars, button groups, form rows
Control styleContainer defines rows and columnsItems flow along one main axis
Item placementItems can be placed in exact cells or areasItems usually follow source order

Subgrid

subgrid allows a nested grid to use the tl-row or column tracks of its parent grid. It is helpful when nested content must align with the outer layout. Browser support is now strong in modern browsers, but always check your target browser requirements before relying on it.

Subgrid Example
.pricing-table {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.plan {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 4;
}

Debugging CSS Grid

When a grid does not behave as expected, inspect the grid overlay in browser DevTools. It can show line numbers, track sizes, gaps, and named areas.

  • Make sure display: grid is on the parent container.
  • Check whether the item is a direct child of the grid container.
  • Use DevTools Grid overlay to inspect line numbers and named areas.
  • Use minmax(0, 1fr) or min-width: 0 when content overflows flexible columns.
  • Confirm that every tl-row in grid-template-areas has the same number of columns.
  • Use gap instead of margins when spacing grid tracks.

Conclusion

CSS Grid is the main layout tool for two-dimensional design on the web. It gives you direct control over rows, columns, gaps, placement, alignment, responsive tracks, and named regions. It removes many old layout workarounds and makes complex layouts easier to read and maintain.

To master Grid, start with display: grid, grid-template-columns, gap, and the fr unit. Then practice item placement, named grid areas, responsive minmax() patterns, and DevTools debugging. Once these pieces are comfortable, Grid becomes a reliable foundation for modern page layout.

Common Mistakes to Avoid
WRONG Apply grid-template-columns to a child item
RIGHT Apply grid-template-columns to the grid container
Template properties define the parent grid structure.
WRONG Use margins between grid items
RIGHT Use gap on the grid container
gap creates consistent spacing between tracks without edge-spacing problems.
WRONG Write uneven grid-template-areas rows
RIGHT Make every grid-template-areas tl-row contain the same number of cells
Named area templates must form a rectangle-based grid.
WRONG Use Grid for a simple one-direction button row
RIGHT Use Flexbox for simple one-dimensional component layouts
Grid is excellent for rows and columns together; Flexbox is often simpler for one axis.
WRONG Assume 1fr can always shrink below content width
RIGHT Use minmax(0, 1fr) or min-width: 0 when content overflows
Long words, tables, and images can force tracks wider than expected.
Key Takeaways
  • CSS Grid is a two-dimensional layout system for rows and columns.
  • display: grid creates a grid container, and its direct children become grid items.
  • grid-template-columns and grid-template-rows define the grid structure.
  • fr distributes available space across flexible tracks.
  • repeat(auto-fit, minmax(240px, 1fr)) is a powerful responsive grid pattern.
  • grid-column and grid-row place or span items using line numbers.
  • grid-template-areas creates readable named page regions.
  • gap is the best way to create space between grid tracks.

Frequently Asked Questions

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.