DevLab
CSS

Flexbox vs CSS Grid: When to Use Each

Learn the fundamental difference between one-dimensional Flexbox and two-dimensional Grid, with practical examples for navbars, galleries, and page layouts.

The One-Sentence Rule

Flexbox is for rows or columns. Grid is for rows and columns simultaneously.

Laying out items in a single direction — a navbar, a row of cards, a vertical sidebar — use Flexbox. Controlling items in two dimensions at once — a dashboard, a photo gallery, a page layout with header, sidebar, main, and footer — use Grid.

Flexbox in Practice

/* Horizontal nav with items spread out */
.navbar {
  display: flex;
  justify-content: space-between;  /* horizontal distribution */
  align-items: center;             /* vertical alignment */
}

/* Equal-width columns that wrap on small screens */
.card-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card { flex: 1 1 280px; }

Grid in Practice

/* Dashboard layout with named areas */
.dashboard {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 40px;
}
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }

/* Auto-fill responsive gallery */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
}

They Are Complementary

The page layout uses Grid; content inside each grid cell often uses Flex. Grid defines macro structure (sidebar, main, header). Flex aligns items within each section (nav links, card contents, form rows).

Quick Decision Guide

ScenarioUse
Navigation barFlexbox
Card row that wrapsFlexbox
Photo gallery (auto-fill)Grid
Page layout with sidebarGrid
Form with aligned labels/inputsGrid
Center one elementEither (Flex simpler)

Frequently Asked Questions

When should I use Flexbox vs CSS Grid?

Use Flexbox for one-dimensional layouts — a single row or a single column of items. Use CSS Grid for two-dimensional layouts where you need to control both rows and columns simultaneously. Many real layouts combine both: Grid for the overall page structure, Flexbox for component-level alignment.

Can I use Flexbox and Grid together?

Yes, and this is the recommended approach. Use CSS Grid for the macro layout (page sections, card grids, dashboards) and Flexbox for micro layout within individual components (nav bars, button groups, card content alignment). They are complementary, not competing.

Is CSS Grid supported in all browsers?

Yes. CSS Grid has been supported in all major browsers since 2017 (Chrome 57, Firefox 52, Safari 10.1, Edge 16). As of 2026, global browser support exceeds 97%. There is no practical reason to avoid Grid for browser compatibility.

What is the difference between justify-content and align-items?

In Flexbox, justify-content aligns items along the main axis (horizontal by default) and align-items aligns them along the cross axis (vertical by default). In Grid, justify-content and align-content control the grid tracks, while justify-items and align-items control items within their cells.

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines