Catalog / CSS Grid Layout Cheatsheet
CSS Grid Layout Cheatsheet
A comprehensive cheat sheet for CSS Grid Layout, covering fundamental concepts, properties for containers and items, and practical examples to help you master grid-based layouts.
Grid Container Properties
Display Property
Defines the element as a grid container, enabling grid layout for its direct children.
|
Example:
|
Grid Template Columns
Specifies the number and width of columns in the grid.
|
Examples:
|
Grid Template Rows
Specifies the number and height of rows in the grid.
|
Examples:
|
Grid Template Areas
Defines named grid areas, allowing you to place items using names rather than line numbers.
|
Example:
|
Gap Properties
Specifies the size of the gap between grid rows and columns.
|
Examples:
|
Justify Content
Aligns the grid along the inline (row) axis when the grid size is smaller than the container.
|
Grid Item Properties
Grid Column & Row
Specifies an item’s start and end lines/tracks within the grid.
|
Examples:
|
Grid Area
Assigns an item to a named grid area or defines its position based on line numbers.
|
Examples:
|
Justify Self
Aligns a grid item along the inline (column) axis within its grid area.
|
Example:
|
Align Self
Aligns a grid item along the block (row) axis within its grid area.
|
Example:
|
Place Self
Shorthand property for setting both
|
Example:
|
Advanced Grid Concepts
Implicit vs. Explicit Grid
The explicit grid is defined using
|
Examples:
|
Minmax Function
Defines a size range greater than or equal to min and less than or equal to max. Can be used in
|
Examples:
|
Repeat Function
Repeats a track list multiple times, useful for creating repetitive grid structures.
|
Examples:
|
Nesting Grids
A grid item can also be a grid container, creating nested grids. This allows for complex layouts where individual sections have their own grid structure. |
Example:
|
Practical Grid Examples
Basic Layout Structure
Creating a simple header, content, and footer layout. HTML:
|
CSS:
|
Sidebar Layout
Implementing a layout with a main content area and a sidebar. HTML:
|
CSS:
|
Responsive Image Gallery
Creating an image gallery that adapts to different screen sizes. HTML:
|
CSS:
|