CSSCSS

CSS Grid Example

Use CSS Grid when the layout needs row and column structure at the same time. This example creates a compact card gallery with equal columns and predictable gaps that scale well across screen sizes.

Concise explanation

  • Grid defines both columns and rows, so it is ideal for dashboards, galleries, and multi-card layouts.
  • Use `repeat(..., minmax(0, 1fr))` to create equal-width columns that can shrink safely.
  • Handle spacing with `gap` instead of padding tricks or manual margins.
  • Responsive column changes are straightforward with a media query or auto-fit pattern.

Code snippet

CSS

.gallery {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}

.gallery__card {
  min-height: 96px;
  padding: 1rem;
  border: 1px solid #27272a;
  border-radius: 1rem;
  background: #111827;
}

@media (min-width: 768px) {
  .gallery {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

Quick result

Cards snap into a clean grid with equal columns, even spacing, and a structure that adapts neatly between small and large screens.

Docs
API
Logs
Deploy

Grid is the better fit for galleries, pricing tables, dashboard panels, and card collections.

Related examples

Related Tools

Why Developers Reach for Grid

Grid gives you explicit control over two-dimensional layout. Instead of forcing items to wrap and hoping the structure still reads well, you define the columns up front and let the browser place items into that framework.

That makes Grid a strong choice for app shells, analytics panels, galleries, settings pages, and any surface where consistency matters more than free-flowing content.

Responsive Grid Tips

When the design changes between breakpoints, Grid is often easier to maintain than deeply nested flex rows. You can redefine `grid-template-columns`, adjust gaps, or move items to named areas without rewriting the whole layout.

For content-heavy layouts, combine Grid for the outer structure with Flexbox for the small components inside each card.