← Back to CSS Course | Chapter 11: Grid | Lesson 1 of 10

CSS Grid

अपनी shop के लिए एक बड़ी display shelf बनाने की कल्पना करें। अगर आप सिर्फ Flexbox इस्तेमाल करते हैं, तो यह एक single लंबी shelf पर boxes stack करने जैसा है। यह horizontally अच्छा काम करता है, लेकिन अलग-अलग shelves में items को vertically align करना मुश्किल है। CSS Grid drawers का एक organized grid panel बनाने जैसा है। आप अपनी grid के exact columns और rows define करते हैं, और अपने items को specific grid boxes में drop करते हैं। यह CSS में सबसे powerful layout tool है, खासतौर पर precise column और row alignments के साथ complex, two-dimensional grids बनाने के लिए design किया गया है।
Syntax
css
selector {
  display: grid;
  grid-template-columns: track-sizes;
  grid-template-rows: track-sizes;
  gap: length;
}

Grid Container Set करना

CSS Grid enable करने के लिए, आप अपने parent container पर display: grid property लागू करते हैं, और grid-template-columns और grid-template-rows इस्तेमाल करके अपनी grid columns और rows define करते हैं।

Note:
  • percentages calculate किए बिना grid columns को proportionally divide करने के लिए fractional (fr) units इस्तेमाल करें।
  • Accepted values:
  • grid-template-columns — 1fr 2fr या repeat(3, 1fr) जैसी track sizes
  • fr — available space का fraction
  • minmax(min, max) — flexible track range
  • auto-fill | auto-fit — repeat() counts के रूप में
  • gap — rows और columns के बीच length
  • grid-column | grid-row — item placement जैसे 1 / 3
Warning: Grid properties को अपने child items का layout control करने के लिए parent container पर लागू किया जाना चाहिए।

उदाहरण: Setting the Grid Container

css
<style>
.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto auto;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.container div {
  background: seagreen;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div>1</div>
  <div>2</div>
</div>

Grid Columns और Row Spans

Default रूप से, grid items sequence में अगले available grid box में drop होते हैं। आप grid-column और grid-row properties इस्तेमाल करके individual grid items को कई columns या rows में फैला सकते हैं।

Note:
  • किसी element को आसानी से कई grid columns में फैलाने के लिए span keyword (जैसे grid-column: span 2) इस्तेमाल करें।
  • Accepted values:
  • span <n> — n tracks span करता है
  • grid-column: span 2 — दो columns wide
  • grid-row: span 3 — तीन rows tall
  • grid-column: 1 / span 2 — line 1 से शुरू होता है
Warning: सुनिश्चित करें कि आपकी column span values आपकी grid में columns की total संख्या से ज़्यादा न हों, वरना यह आपके layout को distort कर देगा।

उदाहरण: Grid Columns and Row Spans

css
<style>
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  background: #eef;
  padding: 10px;
}
.wide {
  grid-column: span 2;
  background: seagreen;
  color: white;
  padding: 10px;
}
.container > div:not(.wide) {
  background: mediumpurple;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="wide">Wide</div>
  <div>Normal</div>
</div>

auto-fit और minmax के साथ Fluid Grids

media queries के साथ responsive layouts बनाना tedious हो सकता है। CSS Grid auto-fit और minmax functions provide करता है जो आपको सिर्फ एक line code से पूरी तरह responsive, fluid grids बनाने देते हैं जो screen size के आधार पर automatically अपने cards को scale और wrap करते हैं।

Note:
  • बिना किसी media queries की ज़रूरत के self-responsive card grids बनाने के लिए repeat(), auto-fit, और minmax() को combine करें।
  • Accepted values:
  • repeat(3, 1fr) — तीन बराबर tracks
  • repeat(auto-fill, minmax(200px, 1fr)) — जितने fit हो सकें
  • repeat(auto-fit, minmax(200px, 1fr)) — fit होता है और खाली tracks collapse करता है
  • repeat(2, 100px 200px) — एक pattern repeat करता है
Warning: अगर आपकी minmax minimum value बहुत बड़ी है, तो आपके columns फिर भी छोटे mobile screens पर overflow हो सकते हैं।

उदाहरण: Fluid Grids with auto-fit and minmax

css
<style>
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  background: #eef;
  padding: 10px;
}
.container div {
  background: mediumseagreen;
  color: white;
  padding: 16px;
}
</style>
<div class="container">
  <div>Card</div>
  <div>Card</div>
  <div>Card</div>
</div>

grid-template-areas से Layouts Structure करना

grid-template-areas property website layouts बनाने का एक intuitive, visual तरीका देती है। आप अपनी page layout का एक text-based map define करते हैं (जैसे header या sidebar जैसे names इस्तेमाल करके), और grid-area property इस्तेमाल करके अपने elements को उन areas में assign करते हैं।

Note:
  • अपनी CSS में अपने strings को indent करके अपने grid area map को readable रखें।
  • Accepted values:
  • grid-template-areas: "header header" "nav main" — strings rows define करते हैं
  • <name> — cells span करने के लिए repeated
  • . — एक खाली cell
  • none — default
Warning: सुनिश्चित करें कि आपके grid area names पूरे, rectangular blocks के रूप में लिखे गए हैं, वरना browser layout map parse करने में fail हो जाएगा।

उदाहरण: Structuring Layouts with grid-template-areas

css
<style>
.container {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar content";
  gap: 6px;
}
.header {
  grid-area: header;
  background: steelblue;
  color: white;
  padding: 10px;
}
.sidebar {
  grid-area: sidebar;
  background: seagreen;
  color: white;
  padding: 10px;
}
.content {
  grid-area: content;
  background: goldenrod;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="header">Header</div>
  <div class="sidebar">Sidebar</div>
  <div class="content">Content</div>
</div>

Grid बनाम Flexbox: कौन बेहतर है?

जबकि दोनों layout systems बेहद उपयोगी हैं, इनके अलग-अलग काम हैं। Flexbox one-dimensional layouts के लिए design किया गया है (items को एक single horizontal row या vertical column के साथ align करना)। CSS Grid two-dimensional layouts के लिए design किया गया है (columns और rows को एक साथ align करना)।

Note: एक rule of thumb के तौर पर, अपने major website layouts और columns बनाने के लिए CSS Grid, और छोटे, single-line menus के लिए Flexbox इस्तेमाल करें।
Warning: Flexbox इस्तेमाल करके complex, two-dimensional layout grids बनाने की कोशिश न करें, क्योंकि wrap offsets manage करना आसानी से टूट सकता है।

उदाहरण: Grid vs. Flexbox: Which is Best?

css
<style>
.flex-row {
  display: flex;
  background: #eef;
  padding: 10px;
  margin-bottom: 10px;
}
.grid-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: #efe;
  padding: 10px;
  gap: 8px;
}
.flex-row div, .grid-layout div {
  background: steelblue;
  color: white;
  padding: 10px;
}
</style>
<div class="flex-row">
  <div>One-dimensional row</div>
</div>
<div class="grid-layout">
  <div>Two-dimensional grid</div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. parent grid container के बजाय child elements पर grid template column properties लागू करना।
  2. grid-template-areas के अंदर invalid area names लिखना, जैसे non-rectangular या overlapping areas बनाना।
  3. simple, single-line menus बनाने के लिए CSS Grid इस्तेमाल करना जब Flexbox कहीं ज़्यादा उपयुक्त है।
चैप्टर सारांश
  • अपने parent container पर display: grid लागू करके CSS Grid enable करें।
  • grid-template-columns और grid-template-rows इस्तेमाल करके columns और rows define करें, और grid-column व grid-row इस्तेमाल करके element spans adjust करें।
  • self-responsive grids बनाने के लिए auto-fit और minmax() इस्तेमाल करें, और visually mapped page layouts आसानी से बनाने के लिए grid-template-areas।
ब्राउज़र सपोर्ट

Standard CSS Grid layout properties और named area systems हर modern web browser द्वारा natively supported हैं।

🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.