dev.rean.me
css

CSS Flex vs Grid Layout Explained with Examples & Tips

Compare CSS Flexbox vs CSS Grid fast. Simple broken English guide with clear comparison table, visual live examples, practical tips, and related posts that open in new tab.

2026-10-07 ยท 6 min read

Share:

CSS Flexbox and CSS Grid are two super powerful layout tools in modern CSS. Many developer ask: "When to use Flexbox? When to use Grid?"

Short answer: Flexbox is 1D (1 Direction). Grid is 2D (2 Directions).

CSS Flexbox Cheat Sheet Infographic

CSS Grid Layout cheat sheet

Here is a short, clear, visual guide to master CSS Flexbox vs Grid fast! ๐Ÿš€


โšก Flex vs Grid: Quick Comparison Table

FeatureCSS FlexboxCSS Grid
Dimension1D (Row OR Column)2D (Row AND Column together)
Best Use CaseSmall components (Navbar, buttons, items list)Big layout structure (Page template, card gallery, dashboard)
Content vs LayoutContent-first (Items grow/shrink based on text)Layout-first (Parent define columns/rows structure first)
Item OverlapHard (need position: absolute)Easy (just put items in same grid cell)
ResponsiveEasy with flex-wrap: wrapEasy with repeat(auto-fit, minmax(...))

๐Ÿ’ก Core Concept: 1D vs 2D

1. Flexbox (1D - Line Layout)

Flexbox arrange items in one line at a time (horizontal row OR vertical column).

Flex Row:   [ Item 1 ] [ Item 2 ] [ Item 3 ]

2. CSS Grid (2D - Grid Matrix)

Grid arrange items in rows AND columns at same time.

Grid 2x3:   [ Item 1 ] [ Item 2 ] [ Item 3 ]
            [ Item 4 ] [ Item 5 ] [ Item 6 ]

๐Ÿ“ฆ Practical Examples

Example 1: Navigation Bar (Flexbox is King ๐Ÿ‘‘)

When you build navbar with logo on left and buttons on right, Flexbox is best.

.navbar {
  display: flex;
  align-items: center;
  gap: 16px;
}

.login-btn {
  margin-left: auto; /* Push button to right side! */
}

โšก Live Result Preview:

MyLogo
Home
About
Login

When you want cards layout that auto resize without media query, CSS Grid is best.

.card-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

โšก Live Result Preview:

Card 1
Card 2
Card 3

Example 3: Sticky Sidebar + Main Content (Sidebar Stay on Scroll)

To make sidebar stay in place (not scroll along with content), add position: sticky; top: 16px; align-self: start;!

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

/* Sidebar stay fixed when page scroll! */
.sidebar {
  position: sticky;
  top: 16px;        /* Distance from top screen */
  align-self: start; /* Prevent grid item stretching */
}

โšก Live Result Preview:

๐Ÿ“Œ Sidebar (Stays Fixed!)
Main Content (Scroll Down ๐Ÿ“œ)
Line 1: Content scrolling...
Line 2: Content scrolling...
Line 3: Content scrolling...
Line 4: Content scrolling...
Line 5: End of main content!

๐ŸŽฏ 5 Golden Tips for Flexbox vs Grid

Tip 1: Simple Rule of Thumb

  • Use Flexbox for small UI components (navbar, tag lists, button groups, modal actions).
  • Use CSS Grid for overall page layout (header/sidebar/content, photo wall, product list).

Tip 2: Combine Both Flex and Grid!

No need to pick only one! Best websites use Grid for outer page, and Flexbox for inner components.

/* Outer layout with Grid */
.dashboard {
  display: grid;
  grid-template-columns: 240px 1fr;
}

/* Inner header with Flexbox */
.header {
  display: flex;
  justify-content: space-between;
}

Tip 3: Perfect Centering Trick

  • Grid (1 line):
    .container { display: grid; place-items: center; }
    
  • Flex (2 lines):
    .container { display: flex; justify-content: center; align-items: center; }
    

Tip 4: Layering Elements (Grid Wins!)

Want image with text overlay? Grid can stack elements inside same cell easily without position: absolute:

.hero-card {
  display: grid;
}

.hero-img, .hero-text {
  grid-column: 1;
  grid-row: 1; /* Both sit in same cell! */
}

Tip 5: Forget float and clearfix

Never use float: left or clearfix hacks in modern web design. Flexbox and Grid work in all modern browsers and much easier to maintain!


๐Ÿ“ฅ Cheat Sheet Downloads (High Quality 2K)


All links open in new tab:

๐Ÿ“š Official Docs & Interactive Tools (New Tab)


Happy layout building! Hope this short & clear CSS Flex vs Grid guide help you! ๐ŸŽจโšก

โ† Back to css
Share: