JSTAcademy
0 XP
Dashboard
Crash Courses
CSS Grid Layout
12 min
Masters+175 XP
Crash Courses · Masters

CSS Grid Layout

Build two-dimensional page layouts with rows and columns together.
12 min read+175 XP on completionCert: HTML & CSS Crash Course
Tap any word in the text below to start reading from there.

CSS Grid Layout

Grid is for two-dimensional layouts control over rows AND columns simultaneously.

Basic Grid

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

Responsive Grid Zero Media Queries

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}

On large screens: 4+ columns. On mobile: 1 column. Zero media queries.

Placing Items

.featured { grid-column: 1 / 3; }    /* spans columns 1-3 */
.wide     { grid-column: span 2; }    /* span 2 from current */
.full     { grid-column: 1 / -1; }    /* span ALL columns */
.tall     { grid-row: span 2; }

Named Grid Areas (Readable Layout)

.page {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 240px 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

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

The CSS reads like a visual diagram of the layout.

Grid vs Flexbox

Use Flexbox for one-dimensional flow (navbar, cards). Use Grid for two-dimensional structure (page layout, dashboard).

0%