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.
Fundamentals
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.
Foundation
📝 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.
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.
Preview
👀 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
Hands-On
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:
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);}}
Both approaches enable Grid layouts on capable browsers. @supports tests the exact property you use, which is clearer for maintainers.
Tips
💬 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.
Watch Out
⚠️ 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.”
A11y
♿ 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.
Compatibility
🖥 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 feature98% supported
Bottom line: Treat (grid) as always-on in modern projects, but keep fallbacks for robustness and learning. Use @supports for new feature detection.
Wrap Up
🎉 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.
Use these points when writing progressive Grid layouts.
5
Core concepts
?01
(grid)
Capability query.
Boolean
FB02
Fallback
Flex first.
Pattern
G03
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.