Skip to main content

Introduction to CSS Grid

CSS Grid Layout is a two-dimensional layout system designed for the web. It allows you to create complex grid-based layouts with rows and columns, giving you precise control over how elements are sized and positioned on a webpage.
While Flexbox is one-dimensional and ideal for components, CSS Grid is two-dimensional and perfect for overall page layouts and complex interfaces.

Basic Concepts

CSS Grid involves two main components: the grid container (parent) and grid items (children).
Grid Container and Items

Grid Container and Items

Creating a Grid Container

To create a grid container, set the display property to grid or inline-grid:

Defining Grid Structure

Grid Template Columns and Rows

The grid-template-columns and grid-template-rows properties define the columns and rows of the grid with a space-separated list of values.
Grid Template Columns and Rows

Grid Template Columns and Rows

The fr Unit

The fr unit represents a fraction of the available space in the grid container.

The repeat() Function

The repeat() function is a shorthand for repeating patterns.

minmax() Function

The minmax() function defines a size range, setting minimum and maximum sizes.

auto-fill and auto-fit

The auto-fill and auto-fit keywords create as many tracks as will fit in the container.
auto-fill vs auto-fit

auto-fill vs auto-fit

Grid Template Areas

The grid-template-areas property defines named grid areas, providing a visual representation of the layout.
Grid Template Areas

Grid Template Areas

Grid Gap

The grid-gap property (or its longhand properties row-gap and column-gap) sets the spacing between grid cells.
The properties grid-gap, grid-row-gap, and grid-column-gap are deprecated in favor of gap, row-gap, and column-gap, which work in both Grid and Flexbox.

Grid Item Placement

Grid Lines

Grid lines are the horizontal and vertical dividing lines that define the grid structure. They are numbered starting from 1.
Grid Lines

Grid Lines

Grid Column and Row

The grid-column and grid-row properties determine a grid item’s location and span within the grid.

Spanning Cells

You can make items span multiple cells using the span keyword.
Grid Item Spanning

Grid Item Spanning

Grid Area

The grid-area property is a shorthand for grid-row-start, grid-column-start, grid-row-end, and grid-column-end.
It can also refer to a named area defined with grid-template-areas.

Alignment and Justification

Aligning Grid Items

The justify-items and align-items properties align grid items within their cells.
Justify Items and Align Items

Justify Items and Align Items

Aligning Grid Content

The justify-content and align-content properties align the entire grid within the container when the grid is smaller than the container.
Justify Content and Align Content

Justify Content and Align Content

Self Alignment

The justify-self and align-self properties allow individual grid items to override the container’s alignment settings.

Implicit Grid

Auto Rows and Columns

The grid-auto-rows and grid-auto-columns properties define the size of implicitly created grid tracks.

Grid Auto Flow

The grid-auto-flow property controls how the auto-placement algorithm works.
Grid Auto Flow

Grid Auto Flow

Common Grid Patterns

Holy Grail Layout

The classic “holy grail” layout (header, footer, and three columns) is simple with Grid:

Card Layout

Grid can create responsive card layouts:

Magazine Layout

Grid is perfect for creating magazine-style layouts:
Grid can create dynamic image galleries:

Responsive Grid Layouts

CSS Grid makes responsive layouts easier with features like minmax(), auto-fill, and auto-fit.

Grid Template Shorthand

The grid-template property is a shorthand for grid-template-rows, grid-template-columns, and grid-template-areas.

Grid Shorthand

The grid property is a shorthand for all grid properties.

Browser Support and Fallbacks

CSS Grid is supported in all modern browsers, but if you need to support older browsers, consider using feature detection or providing fallbacks:

Grid vs. Flexbox

While Grid and Flexbox can sometimes be used to achieve similar layouts, they have different strengths:
  • Grid is two-dimensional, handling both rows AND columns simultaneously
  • Flexbox is one-dimensional, focusing on either rows OR columns
Use Grid for:
  • Two-dimensional layouts (rows AND columns)
  • When you want the layout to determine the content
  • Overall page layout, complex grid-based interfaces
Use Flexbox for:
  • One-dimensional layouts (rows OR columns)
  • When you want content to determine the layout
  • Components, navigation, small-scale layouts
Grid and Flexbox work well together! Use Grid for the overall layout and Flexbox for components within the grid.

Conclusion

CSS Grid is a powerful layout system that allows for complex, two-dimensional layouts with relatively simple code. By understanding the properties of both grid containers and grid items, you can create sophisticated layouts that adapt to different screen sizes. Key takeaways:
  1. Grid is a two-dimensional layout system for creating complex layouts with rows and columns
  2. The parent element becomes a grid container with display: grid
  3. Define the grid structure with grid-template-columns and grid-template-rows
  4. Position items precisely with grid-column and grid-row
  5. Create responsive layouts with minmax(), auto-fill, and auto-fit
  6. Use named areas with grid-template-areas for more readable layouts
With these concepts in mind, you can leverage CSS Grid to create sophisticated, responsive layouts for your web projects.