CSS Grid Layout: A Modern Approach to Web Layouts
CSS Grid Layout is a powerful two-dimensional layout system for the web. It lets you lay out content in rows and columns, offering unprecedented control over the structure of your web pages. Unlike Flexbox, which is primarily for one-dimensional layouts, Grid excels at complex, multi-directional arrangements.
What is CSS Grid?
CSS Grid Layout enables you to create sophisticated layouts with ease. It divides a page into major sections or smaller components, and defines the relationship between elements on the page. It's designed to solve layout problems that were previously difficult or impossible to achieve with traditional CSS methods like floats or even Flexbox alone.
Core Concepts
Understanding Grid involves a few key terms:
- Grid Container: The parent element that has
display: grid;ordisplay: inline-grid;applied to it. - Grid Item: Direct children of the Grid Container.
- Grid Lines: The horizontal and vertical dividing lines that make up the structure of the grid.
- Grid Tracks: The space between two adjacent grid lines (these can be columns or rows).
- Grid Cell: The smallest unit of the grid, defined by the intersection of a grid row and a grid column.
- Grid Area: A rectangular space defined by four grid lines, which can be used to place one or more grid items.
Creating a Grid
To create a grid, you simply apply display: grid; to a container element:
.grid-container {
display: grid;
}
By default, this creates a single column grid. To define the structure, you'll use specific Grid properties.
Key Grid Container Properties
grid-template-columns: Defines the number and width of columns.grid-template-rows: Defines the number and height of rows.gap(orgrid-gap): Sets the space between rows and columns.grid-auto-columns: Defines the width of implicitly created columns.grid-auto-rows: Defines the height of implicitly created rows.grid-auto-flow: Controls how auto-placed items fill the grid.grid-template-areas: Lets you name grid areas for easier item placement.
Key Grid Item Properties
grid-column-start/grid-column-end: Defines an item's placement on the column axis.grid-row-start/grid-row-end: Defines an item's placement on the row axis.grid-column/grid-row: Shorthands for the start/end properties.grid-area: A shorthand for placing an item within a named grid area or by line numbers.justify-self: Aligns an item inside its grid area along the inline (row) axis.align-self: Aligns an item inside its grid area along the block (column) axis.
The fr Unit
The fr unit is a flexible unit representing a fraction of the available space in the grid container. It's incredibly useful for creating responsive layouts. For example, grid-template-columns: 1fr 2fr 1fr; creates three columns where the second column takes up twice as much space as the first and third.
Practical Example
Let's create a simple responsive grid layout:
.grid-demo {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
grid-template-rows: auto auto; /* 2 rows, height determined by content */
gap: 10px; /* Space between items */
}
.grid-item {
/* Styling for individual items */
}
.grid-item-span-2 {
grid-column: span 2; /* Makes this item span 2 columns */
}
.grid-item-row-span-2 {
grid-row: span 2; /* Makes this item span 2 rows */
}
Try It Yourself!
Enter a number to define the columns in the demo grid: