Codectionary / Developer documentation / CSS

CSS Grid

CSS Grid is a powerful two-dimensional layout system that provides precise control over both rows and columns simultaneously. Unlike Flexbox which is one-dimensional, Grid excels at creating complex layouts with explicit row and column structures. It's ideal for page layouts, image galleries, and any design requiring both horizontal and vertical alignment.

Syntax

display: grid;

Examples

Basic Grid

Creating a simple grid with defined columns and automatic rows.

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

.grid-item {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 30px;
  border-radius: 8px;
  text-align: center;
}

Responsive Grid

Using auto-fit and minmax for responsive grids that adapt to screen size.

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

/* Grid will automatically adjust number of columns based on available space */
.responsive-grid .item {
  background: #4CAF50;
  color: white;
  padding: 40px;
  border-radius: 10px;
}

Grid Areas

Defining named grid areas for intuitive layout structure.

.page-layout {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: 80px 1fr 60px;
  gap: 15px;
  height: 100vh;
}

.header { grid-area: header; background: #333; }
.sidebar { grid-area: sidebar; background: #555; }
.main { grid-area: main; background: #777; }
.footer { grid-area: footer; background: #333; }

Complex Layout

Creating asymmetric layouts with different sized grid items.

.gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 200px);
  gap: 10px;
}

/* Span multiple columns/rows */
.featured {
  grid-column: 1 / 3;  /* Span 2 columns */
  grid-row: 1 / 3;     /* Span 2 rows */
  background: #ff6b6b;
}

.wide {
  grid-column: span 2;  /* Span 2 columns */
  background: #4ecdc4;
}

.tall {
  grid-row: span 2;     /* Span 2 rows */
  background: #45b7d1;
}

Parameters and values

  • display (grid | inline-grid): Defines a grid container
  • grid-template-columns (track-size): Defines column tracks
  • grid-template-rows (track-size): Defines row tracks
  • grid-template-areas (string): Defines named grid areas
  • gap (length): Sets spacing between grid items
  • grid-column (start / end): Item column placement
  • grid-row (start / end): Item row placement

Best practices

  • Use Grid for two-dimensional layouts requiring both row and column control
  • Prefer fr units over percentages for flexible, responsive columns
  • Use repeat() and auto-fit/auto-fill for responsive grids without media queries
  • Name grid areas with grid-template-areas for readable, maintainable layouts
  • Combine minmax() with auto-fit for truly responsive designs
  • Use gap instead of margins for consistent spacing between grid items

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation

Flexbox
Flexbox (Flexible Box Layout) is a powerful one-dimensional layout system in CSS that makes it easy to design flexible and responsive layouts. It provides an efficient way to distribute space and align items in a container, even when their sizes are unknown or dynamic. Flexbox is particularly useful for creating navigation bars, card layouts, and centering content.
Flex Item Properties
While display: flex controls the container, individual flex items have their own properties for fine-grained control. flex-grow, flex-shrink, and flex-basis (usually combined via the flex shorthand) control how an item grows or shrinks to fill space. order changes visual order without touching the HTML. align-self overrides the container's align-items for a single item.
grid-template-areas
grid-template-areas lets you visually lay out a grid using named regions written directly in your CSS, making complex page layouts remarkably readable. Each string represents a row, each word represents a named area, and child elements are placed into an area using grid-area, matching the visual shape of your layout with the code itself.
Grid Alignment: place-items, place-content
Grid alignment properties control how content is positioned within grid cells and how the grid itself is positioned within its container. justify-items/align-items control individual item alignment along each axis, and their shorthand place-items sets both at once. justify-content/align-content position the whole grid when it is smaller than its container, with place-content as their shorthand.