CSS grid Media Feature

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Media Queries

What You’ll Learn

The grid media feature detects whether the browser supports CSS Grid layout. Use it to upgrade from flexbox or block fallbacks when Grid is available.

01

@media

Media query.

02

(grid)

Boolean test.

03

Fallback

Flex first.

04

Upgrade

Grid layout.

05

@supports

Modern alt.

06

vs display

Query vs prop.

Introduction

The @media (grid) feature lets you apply CSS only when the output device supports grid-based layout. In web development, that means the browser can use CSS Grid — a two-dimensional layout system for rows and columns.

Before Grid was universal, developers wrote flexbox or float-based layouts as defaults, then wrapped Grid rules inside @media (grid) to enhance the experience on capable browsers. That progressive enhancement pattern is still a great way to learn how media features work.

Definition and Usage

Write @media (grid) { ... } around rules that require Grid support. Outside the query, provide a layout that works everywhere — usually flexbox stacked vertically or a simple block flow.

💡
Beginner Tip

Do not confuse this with display: grid on an element. The media feature tests capability; the display property sets layout. Also see CSS display property for block, flex, and grid values.

📝 Syntax

Use the boolean form or explicit integer values inside an @media rule:

syntax.css
/* Boolean — Grid is supported */
@media (grid) {
  /* Styles when CSS Grid is available */
}

/* Explicit values */
@media (grid: 1) {
  /* Same as (grid) — supported */
}

@media (grid: 0) {
  /* Grid not supported — fallback styles */
}

Accepted Values

  • (grid) — Boolean form; matches when Grid layout is supported (equivalent to grid: 1).
  • grid: 1 — Explicit match for Grid-capable devices.
  • grid: 0 — Matches when Grid is not supported; use for fallback-only rules.

grid Media Feature vs display: grid

@media (grid) Capability query — “Does this browser support Grid?” Wraps a block of conditional rules.
display: grid; Layout declaration — turns one element into a grid formatting context with rows and columns.

Basic Example

grid-basic.css
.container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (grid) {
  .container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.25rem;
  }
}

Modern Alternative: @supports

supports-grid.css
/* Preferred in modern CSS */
@supports (display: grid) {
  .container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }
}

Default Value

There is no CSS default. The browser reports whether Grid layout is supported at runtime. On current browsers, (grid) typically matches.

Syntax Rules

  • Always define a non-Grid fallback outside the media query.
  • Boolean (grid) is the most common form in tutorials and legacy code.
  • Combine with viewport queries: @media (grid) and (min-width: 48rem).
  • For new projects, consider @supports (display: grid) instead.
  • Test in DevTools by temporarily disabling Grid support if available.

Related Topics

@media (grid) @media (grid: 0) @supports (display: grid)

⚡ Quick Reference

QuestionAnswer
Feature namegrid
Boolean form@media (grid)
Explicit valuesgrid: 0 (no), grid: 1 (yes)
What it detectsWhether CSS Grid layout is supported
Typical patternFlexbox fallback + Grid inside (grid)
Modern alternative@supports (display: grid)
Browser supportAll modern browsers; Grid widely available

When to Use grid

Reach for the grid media feature when layout capability matters:

  • Progressive enhancement — Start with flexbox; upgrade to multi-column Grid when supported.
  • Legacy fallbacks — Target very old browsers that lack Grid (rare today).
  • Learning media queries — Understand boolean capability features alongside width/height queries.
  • Combined queries — Apply Grid only on wide screens and Grid-capable browsers.
  • Maintenance of older codebases — Many projects still use @media (grid) patterns.

👀 Live Preview

This demo stacks boxes in a column by default. When Grid is supported — which includes your browser right now — they appear in a two-column grid with green styling:

Grid Capability Demo
Box 1
Box 2
Box 3
Box 4
@media (grid) active — two-column layout applied

Examples Gallery

Practice the grid media feature with two-column layouts, card galleries, dashboard sidebars, and @supports comparisons.

📜 Core Patterns

Upgrade layouts from flexbox to Grid when the browser supports it.

Example 1 — Two-column layout with flex fallback

Stack items vertically by default; switch to a two-column Grid when supported:

grid-two-column.css
.container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (grid) {
  .container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.25rem;
  }
}
Try It Yourself

How It Works

Flexbox keeps content readable everywhere. Grid unlocks side-by-side columns when the browser supports it.

Example 2 — Responsive card gallery

Use a wrapping flex row as fallback; switch to an auto-fit Grid for equal card columns:

grid-cards.css
.gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.card {
  flex: 1 1 12rem;
  padding: 1rem;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
}

@media (grid) {
  .gallery {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  }
  .card {
    flex: unset;
  }
}
Try It Yourself

How It Works

Flex wrap handles older browsers; Grid’s auto-fit and minmax() create cleaner equal columns when available.

Example 3 — Dashboard with sidebar

Stack sidebar and main content on small fallbacks; use Grid areas when Grid is supported:

grid-dashboard.css
.dashboard {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (grid) and (min-width: 48rem) {
  .dashboard {
    display: grid;
    grid-template-columns: 14rem 1fr;
    grid-template-areas: "sidebar main";
    gap: 1.25rem;
  }
  .sidebar { grid-area: sidebar; }
  .main { grid-area: main; }
}
Try It Yourself

How It Works

Combining (grid) with min-width avoids sidebar layouts on narrow screens even when Grid is supported.

Example 4 — @supports as a modern alternative

Compare the legacy media feature with today’s recommended feature detection:

grid-vs-supports.css
/* Legacy media feature */
@media (grid) {
  .legacy-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

/* Modern @supports */
@supports (display: grid) {
  .modern-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }
}
Try It Yourself

How It Works

Both approaches enable Grid layouts on capable browsers. @supports tests the exact property you use, which is clearer for maintainers.

💬 Usage Tips

  • Flexbox first — Write a solid single-column or wrap fallback before Grid enhancements.
  • Combine with width — Use (grid) and (min-width: 48rem) for desktop-only Grid layouts.
  • Prefer @supports — For new code, @supports (display: grid) is more explicit.
  • Reset flex properties — Inside Grid rules, unset flex values on child items when needed.
  • Test fallbacks — Verify the non-Grid layout still looks acceptable.

⚠️ Common Pitfalls

  • Confusing with display: grid — Media (grid) is a query; display: grid is a declaration.
  • Assuming it still matters everywhere — Modern browsers almost always match (grid).
  • No fallback layout — Never rely solely on Grid without a flex or block baseline.
  • Overusing legacy patterns — New projects can use Grid directly with @supports or baseline Grid.
  • Ignoring viewport size — Grid support does not mean “use multi-column on mobile.”

♿ Accessibility

  • Logical reading order — Grid reordering must not break screen-reader flow; use source order or order carefully.
  • Focus visibility — Grid gaps and overlapping areas should not hide focus outlines.
  • Responsive text — Multi-column Grid on small screens can shrink text blocks too narrow.
  • Keyboard navigation — Tab order should follow visual layout intent.
  • Fallback parity — Ensure flex fallback exposes the same content and controls.

🧠 How grid Works

1

Browser loads CSS

Your stylesheet defines a flexbox fallback and Grid rules inside @media (grid).

Parse
2

Capability check

The user agent evaluates whether Grid layout is supported on the device.

Evaluate
3

Matching rules apply

If true, Grid declarations inside the query override the flex fallback.

Apply
=

Enhanced layout

Users on Grid-capable browsers get richer multi-column layouts.

🖥 Browser Compatibility

The grid media feature is supported in all modern browsers. CSS Grid itself is also widely available, so (grid) typically matches.

Baseline · Modern browsers

Grid capability queries

Works in Chrome, Firefox, Safari, and Edge.

98% Global support
grid media feature 98% supported

Bottom line: Treat (grid) as always-on in modern projects, but keep fallbacks for robustness and learning. Use @supports for new feature detection.

🎉 Conclusion

The grid media feature detects CSS Grid support so you can progressively enhance layouts from flexbox to multi-column Grid. It is a classic pattern for capability-based responsive design.

On today’s browsers Grid is nearly universal, so you may use Grid directly or prefer @supports (display: grid). Understanding @media (grid) still helps when reading legacy code and learning how media features differ from layout properties.

💡 Best Practices

✅ Do

  • Provide flexbox fallbacks
  • Combine with min-width queries
  • Use @supports in new code
  • Test non-Grid layout
  • Unset flex on Grid children

❌ Don’t

  • Confuse with display: grid
  • Skip mobile-first layout
  • Assume Grid means desktop
  • Break reading order in Grid
  • Rely on (grid) alone today

Key Takeaways

Knowledge Unlocked

Five things to remember about grid

Use these points when writing progressive Grid layouts.

5
Core concepts
FB 02

Fallback

Flex first.

Pattern
G 03

Upgrade

display: grid.

Enhance
@ 04

@supports

Modern alt.

Prefer
🌐 05

98% support

Grid everywhere.

Compat

❓ Frequently Asked Questions

The grid media feature checks whether the output device supports grid-based layout — in practice, whether CSS Grid can be used. Write @media (grid) { ... } to apply styles only when Grid layout is available.
Use the boolean form (grid) when Grid is supported, or explicit values (grid: 1) for supported and (grid: 0) for not supported. The boolean (grid) is equivalent to (min-grid: 1).
The media feature is a capability query — it asks whether Grid is supported before applying a block of rules. The display property sets layout on a specific element. You often combine them: @media (grid) { .container { display: grid; } }.
For modern feature detection, @supports (display: grid) is the preferred approach. The grid media feature is still valid and useful for teaching progressive enhancement, but @supports directly tests the property you plan to use.
On current browsers, (grid) almost always matches because CSS Grid is widely supported. The pattern remains valuable for understanding fallbacks and for rare legacy environments. Always provide a sensible flexbox or block fallback.

Practice in the Live Editor

Open the HTML editor and experiment with @media (grid) and flexbox-to-Grid progressive enhancement.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful