CSS Grid Layout Mastery

Dec 10, 2023
5 min read
642 reads
3.0 (1 rating)
web-development

CSS Grid Layout Mastery

CSS Grid Layout is a powerful 2-dimensional layout system that allows you to create complex layouts with ease.

Grid Container

To create a grid container, use display: grid:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 100px 200px;
  gap: 20px;
}

Grid Items

Grid items are the direct children of the grid container:

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

Responsive Grids

Use repeat() and minmax() for responsive designs:

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

Grid Areas

Named grid areas make layouts more semantic:

.container {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
}

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

CSS Grid is perfect for creating modern, responsive layouts!

Rate this tutorial

Sign In to rate this tutorial

Comments (0)

Sign In to join the discussion

Scroll down to load comments and ratings