How to css grid
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- CSS Grid Layout was officially published as a W3C Recommendation in March 2017.
- It enables the creation of grid containers and grid items, which can be explicitly placed.
- Key concepts include grid lines, grid tracks, grid areas, and grid cells.
- It supports responsive design by allowing grid properties to be changed based on screen size.
- CSS Grid works well with Flexbox, where Flexbox can be used for one-dimensional layout within a grid item.
What is CSS Grid Layout?
CSS Grid Layout, often referred to simply as CSS Grid, is a powerful and modern layout system for the web. It's a two-dimensional system, meaning it handles both rows and columns simultaneously, unlike Flexbox which is primarily one-dimensional (either row or column based). Grid Layout allows web designers and developers to create complex, responsive layouts with greater ease and control than ever before. It fundamentally changes how we approach page structure, moving away from older, more cumbersome methods like floats and tables for layout purposes.
The primary goal of CSS Grid is to provide a robust way to design the overall layout of a web page. This includes defining major sections like headers, footers, sidebars, and main content areas. It allows for precise alignment and sizing of elements within these defined areas. It's not intended to replace Flexbox entirely, but rather to complement it. Flexbox is excellent for aligning items within a container, such as navigation links in a header, while Grid is ideal for structuring the entire page.
Core Concepts of CSS Grid
Understanding the fundamental concepts is key to effectively using CSS Grid. Here are the most important ones:
- Grid Container: This is the parent element on which `display: grid;` or `display: inline-grid;` is applied. All direct children of the grid container become grid items.
- Grid Item: These are the direct children of the grid container. They can be explicitly placed within the grid.
- Grid Lines: These are the horizontal and vertical dividing lines that make up the structure of the grid. They are numbered starting from 1.
- Grid Tracks: The space between two adjacent grid lines, either horizontally (rows) or vertically (columns).
- Grid Cell: The smallest unit of the grid, formed by the intersection of a grid row and a grid column. It's the space where a single grid item can be placed.
- Grid Area: A rectangular area spanning one or more grid cells, defined by four grid lines. You can name grid areas to place items more intuitively.
Getting Started with CSS Grid
To begin using CSS Grid, you first need to designate a container element as a grid container. This is done by applying the `display: grid;` property to it in your CSS.
Example:
.container {display: grid;grid-template-columns: 1fr 1fr 1fr; /* Defines three equal-width columns */grid-template-rows: auto 1fr auto; /* Defines three rows: auto height, flexible height, auto height */gap: 20px; /* Sets a 20px gap between grid items */}In this example:
- `grid-template-columns: 1fr 1fr 1fr;` creates three columns, each taking up an equal fraction (`1fr`) of the available space. The `fr` unit is a powerful tool for flexible sizing.
- `grid-template-rows: auto 1fr auto;` sets up three rows. The first and third rows will take up only the space their content needs (`auto`), while the middle row will take up the remaining available space (`1fr`).
- `gap: 20px;` (or `grid-gap`) creates spacing between the rows and columns of the grid items. You can also use `row-gap` and `column-gap` for individual control.
Placing Grid Items
Once you have a grid container, you can place the grid items within it. Items are placed automatically into the next available cell by default, flowing from left to right, top to bottom. However, you can explicitly control their placement using properties like:
- `grid-column-start`, `grid-column-end`, `grid-row-start`, `grid-row-end`: These properties define where a grid item begins and ends along the grid lines.
- Shorthand properties: `grid-column` (combines start and end for columns) and `grid-row` (combines start and end for rows).
- `grid-area`: This property allows you to place an item into a named grid area or define its placement using line numbers.
Example of explicit placement:
.item-1 {grid-column: 1 / 3; /* Starts at line 1, ends at line 3 (spans 2 columns) */grid-row: 2; /* Placed in the second row */}.item-2 {grid-column: 3; /* Starts at line 3 */grid-row: 1 / 3; /* Starts at line 1, ends at line 3 (spans 2 rows) */}Advanced Grid Features
CSS Grid offers many advanced features for sophisticated layouts:
- `grid-template-areas`: This property allows you to define named grid areas, making it easier to visualize and assign items to specific regions of the layout. You can create a visual representation of your grid layout using names.
- `justify-items` / `align-items`: These properties control the alignment of grid items along the inline (row) and block (column) axes within their grid cells.
- `justify-content` / `align-content`: These properties align the entire grid within the grid container, especially useful when the grid itself doesn't fill the entire container.
- `minmax()` function: Allows you to define a size range for grid tracks, ensuring they are flexible but have minimum and maximum constraints.
- `repeat()` function: Simplifies the creation of repeating column or row patterns, e.g., `grid-template-columns: repeat(3, 1fr);`.
- Auto-placement: CSS Grid can automatically place items if you don't explicitly define their positions, which is useful for galleries or lists.
Responsiveness with CSS Grid
CSS Grid is inherently responsive. By using media queries, you can redefine your grid structure at different screen sizes. For example, you might have a three-column layout on a desktop that collapses into a single column on a mobile device.
Example:
.container {display: grid;grid-template-columns: 1fr;gap: 10px;}@media (min-width: 768px) {.container {grid-template-columns: 1fr 2fr 1fr; /* Three columns on larger screens */}}@media (min-width: 1200px) {.container {grid-template-columns: repeat(4, 1fr); /* Four columns on extra large screens */}}Browser Support
CSS Grid Layout enjoys widespread support across modern web browsers, including Chrome, Firefox, Safari, Edge, and Opera. It's important to check compatibility for older browsers, but for most current projects, it's a reliable choice. Fallbacks can be implemented for browsers that do not support Grid.
Conclusion
CSS Grid Layout is a fundamental tool for modern web design. Its two-dimensional nature, explicit control over placement, and powerful features for alignment and responsiveness make it indispensable for creating sophisticated and adaptable web page layouts. By understanding its core concepts and properties, developers can build more robust, maintainable, and visually appealing websites.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- CSS Grid Layout - MDN Web DocsCC-BY-SA-2.5
- CSS Grid Layout - WikipediaCC-BY-SA-3.0
- CSS Grid Layout Module Level 1fair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.