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
Creating a Grid Container
To create a grid container, set thedisplay property to grid or inline-grid:
Defining Grid Structure
Grid Template Columns and Rows
Thegrid-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
The fr Unit
Thefr unit represents a fraction of the available space in the grid container.
The repeat() Function
Therepeat() function is a shorthand for repeating patterns.
minmax() Function
Theminmax() function defines a size range, setting minimum and maximum sizes.
auto-fill and auto-fit
Theauto-fill and auto-fit keywords create as many tracks as will fit in the container.

auto-fill vs auto-fit
Grid Template Areas
Thegrid-template-areas property defines named grid areas, providing a visual representation of the layout.

Grid Template Areas
Grid Gap
Thegrid-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 Column and Row
Thegrid-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 thespan keyword.

Grid Item Spanning
Grid Area
Thegrid-area property is a shorthand for grid-row-start, grid-column-start, grid-row-end, and grid-column-end.
grid-template-areas.
Alignment and Justification
Aligning Grid Items
Thejustify-items and align-items properties align grid items within their cells.

Justify Items and Align Items
Aligning Grid Content
Thejustify-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
Self Alignment
Thejustify-self and align-self properties allow individual grid items to override the container’s alignment settings.
Implicit Grid
Auto Rows and Columns
Thegrid-auto-rows and grid-auto-columns properties define the size of implicitly created grid tracks.
Grid Auto Flow
Thegrid-auto-flow property controls how the auto-placement algorithm works.

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:Image Gallery
Grid can create dynamic image galleries:Responsive Grid Layouts
CSS Grid makes responsive layouts easier with features likeminmax(), auto-fill, and auto-fit.
Grid Template Shorthand
Thegrid-template property is a shorthand for grid-template-rows, grid-template-columns, and grid-template-areas.
Grid Shorthand
Thegrid 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
- Two-dimensional layouts (rows AND columns)
- When you want the layout to determine the content
- Overall page layout, complex grid-based interfaces
- 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:- Grid is a two-dimensional layout system for creating complex layouts with rows and columns
- The parent element becomes a grid container with
display: grid - Define the grid structure with
grid-template-columnsandgrid-template-rows - Position items precisely with
grid-columnandgrid-row - Create responsive layouts with
minmax(),auto-fill, andauto-fit - Use named areas with
grid-template-areasfor more readable layouts