CSS Portal

CSS grid Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The grid property is a shorthand that lets authors configure an element as a grid container and set a number of its core layout characteristics in a single declaration. By combining several longhand properties (for example grid-template-columns, grid-template-rows and grid-template-areas) it defines the explicit grid — the named lines, tracks, and areas that items can be placed into — so you can express both the structure and initial placement intent without repeating multiple declarations. Using the shorthand changes the relationship between the container and its children: it creates the grid coordinate system that grid items use for sizing and placement.

Beyond the explicit grid, the grid shorthand also interacts with the creation of implicit tracks and the flow of items when there are more items than explicit slots. It influences how the auto-placement algorithm operates when items are not manually positioned (see grid-auto-flow) and how new tracks are generated when content overflows the explicit grid. This behavior is important for responsive and dynamic layouts because the shorthand can either fully specify the grid you want or leave certain aspects to be inferred, which affects both the visual result and how content reflows as the container changes size.

Spacing and alignment inside the grid are commonly coordinated with the grid definition made by the shorthand. The shorthand can be used in combination with gap-related settings to manage gutters between tracks (for example gap), and alignment properties control how items and the grid as a whole are positioned inside available space — properties such as align-items and justify-items determine how contents of each grid cell are placed, while container-level alignment can be further tuned by other alignment properties.

In practice, the power of grid is in its ability to reduce repetition and express complex two-dimensional layouts cleanly; however, it also has a precedence behavior to be aware of: using the shorthand will reset and overwrite corresponding longhand properties on the same element. Because it defines the container-level grid, it should be used together with a grid-creating declaration such as display (i.e., making the element a grid container) and with item-level positioning when you need explicit control over individual children. Understanding that distinction—container definition versus item placement—helps you combine the shorthand with longhands and item properties to build robust, maintainable grid layouts.

Definition

Initial value
See individual properties
Applies to
Grid containers
Inherited
No
Computed value
See individual properties
Animatable
Yes
JavaScript syntax
object.style.grid

Interactive Demo

One
Two
Three

Syntax

grid: <grid-template> | <grid-template-rows> / [ auto-flow && dense? ] <grid-auto-columns>? | [ auto-flow && dense? ] <grid-auto-rows>? / <grid-template-columns>

Values

  • <grid-template>Defines the grid-template including grid-template-columns, grid-template-rows and grid-template-areas.
  • <grid-template-rows> / [ auto-flow && dense? ] <grid-auto-columns>?Sets up an auto-flow by setting the row tracks explicitly via the grid-template-rows property (and the grid-template-columns property to none) and specifying how to auto-repeat the column tracks via grid-auto-columns (and setting grid-auto-rows to auto). grid-auto-flow is also set to column accordingly, with dense if it’s specified. All other grid sub-properties are reset to their initial values.
  • [ auto-flow && dense? ] <grid-auto-rows>? / <grid-template-columns>Sets up an auto-flow by setting the column tracks explicitly via the grid-template-columns property (and the grid-template-rows property to none) and specifying how to auto-repeat the row tracks via grid-auto-rows (and setting grid-auto-columns to auto). grid-auto-flow is also set to row accordingly, with dense if it’s specified. All other grid sub-properties are reset to their initial values.

Example

<div class="grid-layout">
<header class="item header">Header</header>
<nav class="item nav">Navigation</nav>
<aside class="item sidebar">Sidebar</aside>
<main class="item main">
<h2>Main Content</h2>
<p>This area expands to fill available space.</p>
</main>
<section class="item gallery">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</section>
<footer class="item footer">Footer</footer>
</div>
.grid-layout {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header header"
    "nav main sidebar"
    "gallery gallery gallery"
    "footer footer footer";
  gap: 16px;
  padding: 16px;
  height: 100vh;
  box-sizing: border-box;
  background: #f5f7fb;
}

.item {
  background: #ffffff;
  padding: 16px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.sidebar { grid-area: sidebar; }
.gallery { grid-area: gallery; display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.footer { grid-area: footer; text-align: center; }

.gallery > div {
  background: #e8eef8;
  padding: 12px;
  border-radius: 6px;
  text-align: center;
}

/* Responsive: switch to single-column layout on small screens */
@media (max-width: 700px) {
  .grid-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    grid-template-areas:
      "header"
      "nav"
      "main"
      "gallery"
      "sidebar"
      "footer";
    height: auto;
  }
  .gallery { grid-template-columns: repeat(2, 1fr); }
}

Browser Support

The following information will show you the current browser support for the CSS grid property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!