Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / Advanced Grid

2,087 people viewed this

Advanced Grid

Advanced CSS Grid

Learning Objectives

  • Build layouts with named grid areas.
  • Understand automatic placement and dense packing.
  • Use advanced track sizing functions.
  • Understand subgrid and alignment properties.

1. Named Grid Areas

.page {
  display: grid;
  grid-template-columns: 220px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  gap: 16px;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

Named areas provide a readable way to describe a page layout. Each row in the template is a string of area names. Use dots to represent empty cells, and ensure the named areas form valid rectangular regions.

2. Automatic Placement

.grid {
  display: grid;
  grid-auto-flow: row;
}
.dense-grid {
  grid-auto-flow: row dense;
}

Grid auto-placement positions items that do not have explicit placement. Dense packing can fill earlier holes but may cause visual order to differ from source order.

3. Advanced Track Sizing

.content {
  display: grid;
  grid-template-columns:
    minmax(0, 2fr)
    minmax(240px, 1fr);
  gap: 24px;
}

minmax(0, 2fr) can help prevent a track's minimum size from being determined by an item's automatic minimum contribution. Choose minimums carefully to prevent content overflow.

4. fit-content()

.grid {
  display: grid;
  grid-template-columns: fit-content(250px) 1fr;
}

fit-content() sizes a track within a specified limit, taking available space and content contributions into account.

5. Alignment

.grid {
  display: grid;
  place-items: center;
  place-content: center;
  gap: 16px;
}

Align-items and justify-items control item alignment within their areas. Align-content and justify-content align the grid tracks within the container when extra space is available. Place-items and place-content are shorthand properties for the corresponding alignment pairs.

6. Subgrid

.parent {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.child {
  display: grid;
  grid-column: span 3;
  grid-template-columns: subgrid;
}

Subgrid allows a nested grid to use the parent grid's tracks along an axis. It is useful for aligning content across repeated cards or nested layouts. Verify browser support for your target audience.

7. Grid vs Flexbox

  • Use Grid when rows and columns must work together.
  • Use Flexbox when distributing items mainly along one axis.
  • Combine both when a page uses a Grid structure and individual components use Flexbox.

Practice Exercise

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars