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

CSS Grid Gaps

Grid gaps किसी item में एक भी extra margin जोड़े बिना tracks के बीच साफ़, consistent space डालते हैं -- और margin के उलट, वह space grid के outer edge पर कभी नहीं दिखती।
Syntax
css
selector {
  gap: row-gap column-gap;
  row-gap: length;
  column-gap: length;
}

Grid में Gaps, Margins से बेहतर क्यों हैं

grid items में margin जोड़ना पूरी grid को wider या taller बना देता है और container edges पर uneven spacing बनाता है; row-gap और column-gap इसके बजाय सिर्फ tracks के बीच space insert करते हैं, container edges को flush छोड़ते हुए।

Note:
  • Accepted values:
  • gap — एक या दो values, पहले row फिर column
  • row-gap — length या percentage
  • column-gap — length या percentage
  • normal — default (grid और flex में 0 जैसा behave करता है)
  • नोट: grid-gap, grid-row-gap और grid-column-gap legacy aliases हैं
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: Why Gaps Beat Margins in a Grid

css
<style>
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  background: #eef;
  padding: 10px;
}
.container div {
  background: seagreen;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div>Flush edges</div>
  <div>Even gaps</div>
  <div>No margin needed</div>
</div>

Horizontal Spacing के लिए column-gap

column-gap सिर्फ columns के बीच की space set करता है, rows के बीच vertical spacing को untouched छोड़ते हुए -- उपयोगी जब आपको tight vertical rhythm चाहिए लेकिन side-by-side columns के बीच breathing room।

Note:
  • Accepted values:
  • column-gap: <length> — जैसे 20px
  • column-gap: <percentage> — container के relative
  • column-gap: normal — default, 0
  • grid-column-gap — legacy alias
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: column-gap for Horizontal Spacing

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

Vertical Spacing के लिए row-gap

row-gap column-gap का vertical counterpart है, सिर्फ rows के बीच की space control करते हुए और columns के बीच horizontal spacing को पूरी तरह untouched छोड़ते हुए। इसे independently set करें जब आपके layout को generous vertical breathing room चाहिए लेकिन tight horizontal grouping, या इसका उल्टा।

Note:
  • Accepted values:
  • row-gap: <length> — जैसे 20px
  • row-gap: <percentage> — container के relative
  • row-gap: normal — default, 0
  • grid-row-gap — legacy alias
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: row-gap for Vertical Spacing

css
<style>
.container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  row-gap: 30px;
  background: #eef;
  padding: 10px;
}
.container div {
  background: seagreen;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div>1</div><div>2</div><div>3</div><div>4</div>
</div>

gap Shorthand

gap: 20px 10px एक declaration में row-gap को 20px और column-gap को 10px set करता है, row value हमेशा पहले list की जाती है; अकेला gap: 16px दोनों axes पर एक साथ 16px लागू करता है।

shorthand आमतौर पर दोनों longhand properties से बेहतर माना जाता है जब तक आपको बाद में अपनी stylesheet में एक axis को independently बदलना न हो।

Note:
  • Accepted values:
  • gap: 20px — दोनों के लिए एक जैसा
  • gap: 10px 30px — पहले row फिर column
  • gap: 2% — percentage
  • gap: normal — default
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: The gap Shorthand

css
<style>
.container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px 10px;
  background: #eef;
  padding: 10px;
}
.container div {
  background: seagreen;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div>1</div><div>2</div><div>3</div><div>4</div>
</div>

Gaps minmax और fr Sizing के साथ Interact करते हैं

Gap space को fr और auto-fit/auto-fill calculations के काम करने वाली space में से घटाया जाता है, इसलिए किसी narrow container पर एक बड़ी gap value minmax() वाली tracks को आपकी उम्मीद से जल्दी wrap करा सकती है -- हमेशा gap और track sizing को साथ में test करें।

Note:
  • Accepted values:
  • gap: <length> — fr calculate होने से पहले घटाई जाती है
  • 1fr — gaps के बाद बची हुई space share करता है
  • minmax(<min>, 1fr) — tracks फिर भी minimum का सम्मान करते हैं
  • auto-fit — gaps बड़े होने पर कम tracks fit होती हैं

उदाहरण: Gaps Interact with minmax and fr Sizing

css
<style>
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 40px;
  background: #eef;
  padding: 10px;
}
.container div {
  background: seagreen;
  color: white;
  padding: 16px;
}
</style>
<div class="container">
  <div>Large gap can push tracks to wrap sooner</div>
  <div>Card</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. gaps बनाने के लिए items पर margin इस्तेमाल करना, जो double spacing या uneven edges बना सकता है।
  2. सिर्फ पुराना grid-gap नाम इस्तेमाल करना, जबकि gap current property है।
  3. यह उम्मीद करना कि gap grid के बाहर के आस-पास space जोड़ेगा, जबकि यह सिर्फ tracks के बीच लागू होता है।
ब्राउज़र सपोर्ट

CSS Grid Chrome 57+, Firefox 52+, Safari 10.1+ और Edge 16+ में 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.