Summarize this article with:

Every website you visit has a layout system holding it together. Most of the time, that system is a grid layout.

A grid layout organizes content into rows and columns, giving you control over both dimensions at once. It replaced years of float-based workarounds and became the standard CSS layout model for building structured, responsive web pages.

This article breaks down how CSS Grid works, what each component does, and how to use it in production. You will also learn the difference between grid and Flexbox, the most common grid patterns, and the mistakes that trip up developers regularly.

What is a Grid Layout

YouTube player

A grid layout is a two-dimensional layout system that organizes content into rows and columns on a web page. It splits any given space into a structured set of horizontal and vertical lines, creating cells where elements sit in precise positions.

Unlike older methods that relied on floats or table-based hacks, the CSS Grid Layout Module gives you direct control over both axes at the same time. You define columns. You define rows. Then you place items exactly where they belong.

The W3C introduced CSS Grid as a dedicated specification, and browsers like Google Chrome, Firefox, Safari, and Microsoft Edge all support it fully. It became a W3C Candidate Recommendation, and since then it has changed how developers approach responsive design entirely.

Grid layout works at the container level. You set display: grid on a parent element, and every direct child becomes a grid item automatically. From there, you control sizing with fractional units, fixed pixel values, or percentage-based widths.

The real strength here is predictability. A grid gives you a reliable structure that holds up across screen sizes without relying on workarounds. That is why it became the standard layout model for modern web design.

How Does a Grid Layout Work

YouTube player

A CSS grid layout works by dividing a container into a series of intersecting grid lines, both horizontal and vertical. These lines form tracks (columns and rows), and the spaces between them become cells where content lives.

CSS Grid has been supported in all major browsers since 2017. According to caniuse data, it now reaches approximately 95% of global users, making it as safe to use as Flexbox. The 2024 State of CSS Survey found 78% of developers use Grid regularly, up from 62% three years prior.

You start by declaring display: grid on any block-level container. That single line of CSS activates the grid formatting context for all direct children inside it.

Then you define the column and row structure using grid-template-columns and grid-template-rows. A declaration like grid-template-columns: 1fr 1fr 1fr creates three equal columns using the fr unit, which distributes available space proportionally.

Where is web design headed next?

Discover the latest web design statistics: industry growth, design trends, technology adoption, and insights defining the future of the web.

Explore the Data →

Grid items fill cells in source order by default. But you can override that completely. The grid-column and grid-row properties let you place any item at any intersection of lines, spanning multiple tracks if needed.

Here is a basic working example:

.container {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 16px;
}

That code creates a three-column layout where the first column is fixed at 200px and the remaining two share the leftover space equally. The gap property adds 16px of spacing between every cell.

The browser handles the math. It calculates available space, subtracts gaps, resolves fractional units, and positions each grid item accordingly. No floats. No clearfixes. No margin hacks.

Grid also supports auto-placement. If you don’t specify where an item goes, the grid algorithm places it in the next available cell following the grid flow direction. This is useful for card-based layouts where items just need to fill available spots in order.

Research from Smashing Magazine’s 2024 survey found that using named grid areas over numerical tracks cuts development time during layout adjustments by 23%. Grid’s auto-fit and auto-fill with minmax() also reduces layout shift issues by 27% compared to fixed fractions, according to a 2024 Frontend Masters report.

What Are the Components of a CSS Grid Layout

Every CSS grid layout is built from six core components. Each one has a specific role in how the grid calculates sizing, positions elements, and manages the overall visual hierarchy of the page.

What is a Grid Container

The grid container is the parent element with display: grid applied. It establishes the grid formatting context and controls the column layout, row alignment, and spacing for every direct child inside it.

What Are Grid Items

Grid items are the direct children of a grid container. Each one occupies at least one grid cell by default but can span multiple columns or rows using placement properties like grid-column: 1 / 3.

What Are Grid Lines

Grid lines are the invisible dividing lines that form the structure of the grid. A three-column grid has four vertical lines (numbered 1 through 4) and each row boundary adds horizontal lines.

You reference these line numbers when placing items manually. Named grid lines are also supported for more readable code.

What Are Grid Tracks

A grid track is the space between two adjacent grid lines. Column tracks run vertically; row tracks run horizontally. You size them using grid-template-columns and grid-template-rows with values like 200px, 1fr, or minmax(100px, 1fr).

What Are Grid Cells

A grid cell is the smallest unit in the grid, sitting at the intersection of one column track and one row track. Think of it like a single cell in a spreadsheet. Each grid item fills one or more cells.

What is a Grid Area

A grid area is a rectangular region made up of one or more adjacent grid cells. You define areas with the grid-template-areas property using a visual string syntax:

grid-template-areas:
  "header header header"
  "sidebar main main"
  "footer footer footer";

This lets you assign items to named regions, which makes complex page layouts easier to read and maintain. It is one of the most practical features for building landing page structures.

Benchmarking by CSS-Tricks found that grid layouts using repeat() cut selector complexity by over 35% in production UI libraries, and named grid areas help new team members locate layout sections at a glance, directly cutting onboarding time.

What is the Difference Between Grid Layout and Flexbox Layout

YouTube player

Grid and Flexbox are both CSS layout modules, but they solve different problems. Grid handles two-dimensional layouts (rows and columns simultaneously). Flexbox handles one-dimensional layouts (a single row or a single column at a time).

That distinction matters more than most people realize.

According to the 2024 State of CSS Survey, 78% of developers now use Grid regularly, up from 62% just three years ago. The shift reflects a growing recognition that the two tools serve genuinely different purposes, not just different preferences.

Flexbox is built for distributing space along one axis. It works well for navigation bars, button groups, and small-scale user interface components where items sit in a line and need flexible sizing.

Grid is built for full page structure. When you need to control where a sidebar sits relative to a header and a content area at the same time, grid handles that in a few lines of code. Flexbox would require nesting multiple flex containers to achieve the same result.

Practical breakdown:

  • Use Grid when defining the overall page layout, building card grids with consistent row heights, or creating any design where items must align on both axes
  • Use Flexbox when aligning items inside a component, distributing space in a toolbar, or handling single-direction content flow

According to HTTP Archive and Web Almanac data, the most common pattern is using both: Grid for page structure and Flexbox for components inside grid items. That combination covers nearly every layout scenario you will run into.

Took me a while to stop reaching for Flexbox for everything. But once I started using Grid for page-level structure and Flexbox for component-level alignment, the CSS got significantly cleaner. Your mileage may vary, but that split works well in practice.

What Are the Properties of a CSS Grid Container

YouTube player

The grid container is where all layout decisions start. Its properties define the column and row structure, control spacing, and determine how items are placed and aligned within the grid system.

What Does grid-template-columns Do

The grid-template-columns property defines the number and size of columns in the grid. Each value creates one column track.

grid-template-columns: 250px 1fr 1fr;

That creates three columns: one fixed at 250px, two splitting the remaining space. You can also use the repeat() function: repeat(4, 1fr) creates four equal columns without writing the value four times.

What Does grid-template-rows Do

The grid-template-rows property works the same way but for horizontal tracks. Setting grid-template-rows: 80px 1fr 60px creates a fixed header row, a flexible content row, and a fixed footer row.

Rows you don’t explicitly define get sized by the implicit grid, which uses grid-auto-rows to set their default height.

What Does grid-gap Do

The grid-gap property (now officially gap) sets the white space between grid cells. You can set a single value for uniform spacing or two values for row and column gaps separately.

gap: 20px 16px; /* 20px between rows, 16px between columns */

This replaced the old approach of using margins on individual items, which always caused alignment headaches at the edges of the grid.

What Does grid-template-areas Do

The grid-template-areas property lets you define named regions using a visual string map. Each quoted string represents one row, and repeated names merge cells into a single area.

.container {
  grid-template-areas:
    "nav nav nav"
    "side content content"
    "foot foot foot";
}

Then you assign items to areas: grid-area: content; on an element places it into the “content” region.

Named areas are self-documenting. As MDN’s documentation notes, a developer reading your CSS can understand the full page structure at a glance, without deciphering line numbers or span values. This is especially useful for frontend teams working on complex templates, where rearranging layout sections later requires only updating the string map, not touching the HTML structure at all.

What Are the Properties of CSS Grid Items

YouTube player

Grid container properties define the structure. Grid item properties control where each element sits inside that structure and how it aligns within its assigned cell.

What Does grid-column Do

The grid-column property sets which vertical grid lines an item starts and ends at. Writing grid-column: 1 / 3 makes that item span from line 1 to line 3, covering two column tracks.

You can also use span syntax: grid-column: span 2 tells the item to occupy two columns from wherever the auto-placement algorithm puts it.

Two ways to place items with grid-column:

/* Line-based placement */
.item { grid-column: 1 / 3; }

/* Span syntax */
.item { grid-column: span 2; }

Line-based is precise. Span is flexible. Use line-based when the item must start in a specific column, and span when auto-placement handles the starting position.

What Does grid-row Do

Same logic, different axis. grid-row: 2 / 4 places an item across row lines 2 through 4, spanning two row tracks vertically.

Combining grid-column and grid-row gives you precise grid positioning over both dimensions at once. According to MDN’s grid documentation, this two-axis control is what separates Grid from Flexbox at the layout level. Flexbox can’t do this without nesting.

What Does grid-area Do

The grid-area property is a shorthand that sets row start, column start, row end, and column end in a single declaration. It also accepts named area values when you are using grid-template-areas on the container.

.item { grid-area: 1 / 2 / 3 / 4; }

/* or with named areas */
.item { grid-area: sidebar; }

The named area approach is the more readable option for most production layouts. CSS-Tricks’ complete grid guide confirms that using grid-area with named regions makes layout code easier to scan and update compared to line numbers, especially when multiple developers are working on the same templates.

What Does justify-self and align-self Do

justify-self controls horizontal alignment of a single item inside its grid cell. align-self controls vertical alignment. Both accept start, end, center, and stretch.

These override the container-level justify-items and align-items settings for that one specific element. Useful when a single call-to-action button needs different alignment than the rest of the grid items.

You can also use the place-self shorthand to set both in one line: place-self: center; handles both axes at once and is the fastest way to center a single item inside its cell.

One important note on accessibility: as DigitalOcean’s CSS alignment guide points out, visual reordering with grid properties does not change the HTML source order. Screen readers and keyboard navigation still follow the document structure. If justify-self or grid placement moves an element far from its logical position in the DOM, keyboard users may experience a confusing tab sequence. Always verify that visual order matches reading order when using item-level alignment.

 

How to Create a Grid Layout in CSS

Building a grid layout from scratch takes about five lines of CSS. Here is a complete working setup for a standard page with a header, sidebar, main content area, and footer.

The HTML structure:

<div class="page">
  <header class="header">Header</header>
  <aside class="sidebar">Sidebar</aside>
  <main class="content">Main Content</main>
  <footer class="footer">Footer</footer>
</div>

The CSS:

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 80px 1fr 60px;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
  gap: 16px;
  min-height: 100vh;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer  { grid-area: footer; }

That is the whole thing. The sidebar stays fixed at 240px. The content area fills the remaining horizontal space using 1fr. The header and footer stretch across both columns because their named areas repeat twice in the template string.

The min-height: 100vh makes the grid fill the full viewport height, which pushes the footer to the bottom even when content is short.

No floats, no absolute positioning, no clearfix hacks. The grid handles all of it.

Note from DigitalOcean’s CSS Grid guide: always keep your HTML source order logical when using named areas. Screen readers and keyboard users follow the DOM, not the visual layout. The structure above already does this correctly, with header, sidebar, main, and footer in a natural reading sequence.

What Are the Types of Grid Layouts

CSS Grid supports several layout types depending on how you define tracks and how the browser resolves sizing. Knowing the difference between them affects how your layout behaves across screen sizes.

What is an Explicit Grid

An explicit grid is one where you manually define every column and row using grid-template-columns and grid-template-rows. You control exactly how many tracks exist and how wide or tall each one is.

What is an Implicit Grid

When grid items overflow beyond the tracks you defined, the browser creates additional tracks automatically. These belong to the implicit grid. You control their sizing with grid-auto-rows and grid-auto-columns. According to MDN, auto-sized implicit rows grow to contain their content by default, which can cause uneven row heights if you’re not managing this explicitly.

What is a Fixed Grid Layout

A fixed grid layout uses absolute values like px or rem for track sizes. The columns and rows stay the same width regardless of screen size. Common for print-style layouts or admin dashboards with predictable content.

What is a Fluid Grid Layout

A fluid grid layout uses relative units like fr, percentages, or the minmax() function. Tracks resize proportionally based on the available space, which makes the layout adapt to different viewport widths without media queries.

How Does Grid Layout Handle Responsive Design

Grid has built-in features that handle viewport adaptation without writing a single media query. The two keywords that make this work are auto-fill and auto-fit, combined with the minmax() function.

grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));

That single line creates a responsive grid where each column is at least 280px wide and grows to fill remaining space. The browser adds or removes columns as the viewport width changes. Web.dev’s layout patterns documentation calls this the RAM pattern (Repeat, Auto, Minmax), noting it eliminates the need for breakpoints in most card and gallery layouts.

auto-fill vs auto-fit:

  • auto-fill generates as many tracks as will fit, even if some are empty. Good for grids where you want to reserve space for future items
  • auto-fit does the same but collapses empty tracks to zero, letting filled items stretch wider. Better for layouts where fewer items should expand to fill the row

A 2024 Frontend Masters report found that using auto-fit and auto-fill with minmax() reduces layout shift issues by 27% compared to fixed fractions.

For more specific breakpoint control, you can still combine grid with media queries:

.grid { grid-template-columns: 1fr; }

@media (min-width: 768px) {
  .grid { grid-template-columns: 1fr 1fr; }
}

@media (min-width: 1200px) {
  .grid { grid-template-columns: repeat(3, 1fr); }
}

That pattern starts with a single column on mobile, shifts to two columns on tablets, and expands to three on desktop. It follows a mobile-first design approach. With mobile traffic now accounting for over 50% of global web traffic in 2024, starting from the smallest viewport is not a preference but a baseline requirement for most production sites.

The minmax() function also pairs well with responsive typography, since both respond to available space rather than fixed breakpoints.

What Are Common Grid Layout Patterns

Certain grid configurations come up over and over in production work. These three patterns cover the majority of real layout needs.

What is a Holy Grail Layout

YouTube player

The holy grail layout is a classic page structure: header on top, footer on bottom, and a three-column middle section with a sidebar on each side of the main content.

CSS Grid solves this in about 10 lines using grid-template-areas.

Before Grid, getting this right with floats was genuinely painful. Equal-height columns, source order independence, and a sticky footer all required separate workarounds.

Grid handles all three by default. The 2024 State of CSS Survey found that 78% of developers now use Grid regularly, up from 62% just three years ago.

What is a Card Grid Layout

A card layout arranges content blocks (products, articles, profiles) into a uniform grid where each card has consistent dimensions.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
}

Standard use cases:

  • E-commerce product card pages
  • Blog post archives
  • Profile or team listings

Each card fills a cell, the grid wraps automatically, and the gap property keeps spacing consistent without margin math.

LambdaTest data shows CSS Grids are detected in more than 75% of all page loads, making this pattern one of the most widely deployed layouts on the web.

What is a Masonry-Style Grid Layout

A masonry layout staggers items vertically so they pack tightly without equal row heights. Think Pinterest.

The production situation right now:

  • Firefox supports grid-template-rows: masonry natively
  • Chrome 140 has experimental support behind a flag
  • Safari Technology Preview 234 shipped display: grid-lanes in December 2025

The CSS Working Group reached consensus in January 2025 to unify browsers around the display: grid-lanes syntax, with mid-2026 cited as a realistic production timeline.

Until broad support lands, JavaScript libraries or CSS column-based workarounds are still the production-safe path.

What Browsers Support CSS Grid Layout

CSS Grid Layout has strong global browser support across all modern browsers. Every modern browser handles Grid Layout Level 1 without prefixes or polyfills.

BrowserVersionRelease
Google Chrome57+March 2017
Firefox52+March 2017
Safari10.1+March 2017
Microsoft Edge16+October 2017
Opera44+March 2017

Internet Explorer 11 has partial support for an older version of the grid spec using the -ms- prefix. The syntax is completely different from the current standard, and most teams no longer target it.

For cross-browser compatibility, test your grid layouts in the DevTools Grid Inspector available in Chrome and Firefox. It visually overlays grid lines and areas, which makes debugging track sizes and item placement significantly faster.

CSS Subgrid (Grid Layout Level 2) has narrower support. Firefox added it in version 71, and Chrome followed in version 117 (September 2023). Safari supports it from version 16.

According to FrontendTools, CSS Subgrid achieved universal browser support in 2024 and now sits at 97% global coverage in 2025, making it production-ready for most projects. Check caniuse.com before using subgrid in production.

What Are Common Mistakes When Using Grid Layout

YouTube player

These are the problems I see come up most often, and a few of them tripped me up early on too.

Forgetting display: grid. Grid properties on child elements do nothing unless the parent has display: grid set. Sounds obvious, but it is the number one reason grid code “doesn’t work.”

Confusing line numbers with track numbers. A three-column grid has four column lines (1, 2, 3, 4), not three. Off-by-one errors in grid-column values cause items to land in the wrong cell or overlap unexpectedly.

Using grid-gap with margins. Adding margins to grid items on top of the gap property creates uneven spacing. Pick one approach and stick with it. The gap property handles spacing between cells; margins add space outside the grid’s control.

Ignoring the implicit grid. If you have more items than explicit tracks, the browser creates implicit rows with auto sizing. Set grid-auto-rows: minmax(100px, auto) to prevent inconsistent row heights when content overflows your defined grid.

Overcomplicating simple layouts. Not everything needs grid. A single row of buttons or a sticky navigation bar is a better fit for Flexbox. Using grid for one-dimensional content just adds unnecessary complexity to your stylesheet. Web.dev analysis of the State of CSS 2024 survey found that developer complaints about Grid frequently come down to it being hard to learn, not actual browser bugs.

Not testing on real devices. The DevTools Grid Inspector is great for development, but grid behavior can differ subtly on mobile browsers, especially with auto-fill and minmax() values. Test on actual phones, not just resized browser windows. According to a 2024 responsive design report, more than 70% of users abandon websites that are not optimized for their devices.

FAQ on Grid Layouts

What is a grid layout in CSS?

A CSS grid layout is a two-dimensional layout system that organizes content into rows and columns. You activate it with display: grid on a container element, then define column and row tracks to position child elements precisely.

What is the difference between grid and Flexbox?

Grid controls both rows and columns at the same time. Flexbox handles one direction only. Use grid for full page structure and Flexbox for aligning items inside smaller components like navigation bars or button groups.

Is CSS Grid supported in all browsers?

CSS Grid Layout has over 97% global browser support. Google Chrome, Firefox, Safari, Microsoft Edge, and Opera all support it fully. Internet Explorer 11 only supports an older, prefixed version of the specification.

What is a grid container?

A grid container is any HTML element with display: grid applied. It establishes the grid formatting context. Every direct child of that container automatically becomes a grid item placed within the defined tracks.

What does the fr unit do in CSS Grid?

The fr unit represents a fraction of the available space inside the grid container. Writing grid-template-columns: 1fr 2fr creates two columns where the second is twice as wide as the first.

How do you make a grid layout responsive?

Use auto-fill or auto-fit with the minmax() function. The declaration repeat(auto-fill, minmax(280px, 1fr)) creates a fluid grid that adds or removes columns automatically based on viewport width.

What is grid-template-areas?

The grid-template-areas property lets you define named regions using a visual string syntax. You assign items to these named areas with grid-area, making complex page layouts readable and easy to maintain.

Can you nest grids inside other grids?

Yes. Any grid item can also be a grid container by applying display: grid to it. CSS Subgrid (Grid Layout Level 2) takes this further by letting nested grids inherit track sizing from the parent grid.

What is the implicit grid?

The implicit grid contains tracks the browser creates automatically when items overflow beyond explicitly defined rows or columns. You control implicit track sizing with grid-auto-rows and grid-auto-columns properties.

When should you use CSS Grid instead of other layout methods?

Use CSS Grid when you need control over both axes simultaneously. Page-level structures, card grids, and dashboard layouts are all strong use cases. For single-direction alignment inside components, Flexbox is the better fit.

Conclusion

Understanding what a grid layout is comes down to one thing: it is a two-dimensional CSS layout system that gives you precise control over content placement using rows, columns, and named areas.

The grid-template-columns and grid-template-rows properties define your structure. The fr unit and minmax() function handle fluid sizing. Auto-fill and auto-fit make the whole thing responsive without extra code.

Grid works alongside Flexbox, not against it. Use grid for page-level structure. Use Flexbox for component-level alignment.

Browser support sits above 97% globally. The W3C specification is stable. CSS Subgrid is catching up across Chrome, Firefox, and Safari.

Start with a container, set display: grid, define your tracks, and place your items. That is the whole process. The rest is just refinement.

Author

Bogdan Sandu specializes in web and graphic design, focusing on creating user-friendly websites, innovative UI kits, and unique fonts.Many of his resources are available on various design marketplaces. Over the years, he's worked with a range of clients and contributed to design publications like Designmodo, WebDesignerDepot, and Speckyboy, Slider Revolution among others.