Web Styles

Advanced Grid

Grid areas, auto-fit, minmax, subgrid, and complex layouts.

Advertisement

Advanced Grid

Grid areas, auto-fit, minmax, subgrid, and complex layouts.

Overview

CSS Grid enables complex, two-dimensional layouts.

Key Concepts

  • Grid Areas — Name grid regions
  • Auto-fit/Minmax — Responsive columns
  • Subgrid — Inherit parent grid
  • Grid Template — Shorthand properties
  • Implicit Grid — Auto-generated tracks

Code Examples

/* Named grid areas */
.layout {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  min-height: 100vh;
}

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

/* Responsive columns */
.auto-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;
}

/* Card grid */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
}

/* Subgrid */
.card {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3;
}

/* Complex dashboard */
.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: auto repeat(3, 200px);
  gap: 1rem;
}

.widget-1 { grid-column: 1 / 3; grid-row: 1 / 2; }
.widget-2 { grid-column: 3 / 5; grid-row: 1 / 2; }
.widget-3 { grid-column: 1 / 2; grid-row: 2 / 4; }
.widget-4 { grid-column: 2 / 4; grid-row: 2 / 3; }
.widget-5 { grid-column: 4 / 5; grid-row: 2 / 4; }
.widget-6 { grid-column: 2 / 4; grid-row: 3 / 4; }

/* Masonry-like layout */
.masonry {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 10px;
}

.item-1 { grid-row: span 5; }
.item-2 { grid-row: span 8; }
.item-3 { grid-row: span 3; }

Practice

Build a complex dashboard layout using CSS Grid.

Advertisement