← Back to CSS Course | Chapter 12: Responsive Design | Lesson 4 of 8

CSS RWD Grid View

एक responsive grid view adjustable shelves के एक set जैसा है -- वही shelving unit किसी wide wall पर चार narrow columns रख सकता है या किसी narrow wall पर एक column में collapse हो सकता है, बिना आपको हर wall के लिए एक अलग shelf बनानी पड़े।
Syntax
css
* {
  box-sizing: border-box;
}
.column {
  width: percentage;
  float: left;
}

Responsive Grid View क्या है

एक responsive grid view content को fluid columns की एक निश्चित संख्या में arrange करता है जो viewport बदलने पर reflow होते हैं -- stack, wrap, या resize होते हुए -- एक rigid pixel grid के बजाय जो screen size चाहे जो भी हो एक जैसी width बनाए रखता है।

उदाहरण: What a Responsive Grid View Is

css
<style>
.row {
  display: flex;
  flex-wrap: wrap;
}
.row div {
  background: steelblue;
  color: white;
  padding: 10px;
  margin: 4px;
}
</style>
<div class="row">
  <div>Column</div>
  <div>Column</div>
  <div>Column</div>
</div>

Percentage Based Columns

Classic approach float या inline-block इस्तेमाल करके किसी row को percentage-width columns में बाँटता है (जैसे तीन columns हर एक 33.33% पर), इसलिए container के shrink होने पर columns automatically साथ में shrink होते हैं, हालांकि उन्हें फिर भी बहुत छोटे screens पर stack होने के लिए एक breakpoint चाहिए होता है।

Note:
  • Accepted values:
  • width: 25% — चार columns
  • width: 33.33% — तीन columns
  • width: 50% — दो columns
  • width: 100% — एक column
  • box-sizing: border-box — padding को width के अंदर रखता है

उदाहरण: Percentage Based Columns

css
<style>
.row {
  overflow: hidden;
}
.col {
  float: left;
  width: 33.33%;
  box-sizing: border-box;
  padding: 10px;
  color: white;
}
.col:nth-child(1) { background: coral; }
.col:nth-child(2) { background: seagreen; }
.col:nth-child(3) { background: steelblue; }
</style>
<div class="row">
  <div class="col">33.33%</div>
  <div class="col">33.33%</div>
  <div class="col">33.33%</div>
</div>

Flexbox Based Grid View

flex-wrap: wrap वाला एक flex container और items को दिया गया एक flex-basis percentage या fixed minimum width एक self-adjusting grid produce करता है जहाँ columns fit न होने पर automatically नई rows में wrap हो जाते हैं, बिना किसी explicit breakpoint math के।

Note:
  • Accepted values:
  • display: flex — एक row बनाता है
  • flex-wrap: wrap — wrapping की अनुमति देता है
  • flex: 1 1 <basis> — flexible columns
  • gap: <length> — gutters

उदाहरण: Flexbox Based Grid View

css
<style>
.row {
  display: flex;
  flex-wrap: wrap;
}
.col {
  flex-basis: 200px;
  background: steelblue;
  color: white;
  padding: 10px;
  margin: 4px;
}
</style>
<div class="row">
  <div class="col">Wraps automatically</div>
  <div class="col" style="background: seagreen;">Wraps automatically</div>
</div>

CSS Grid Based Grid View

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) एक grid view बनाता है जो row में जितने भी 200px-minimum columns fit हो सकें उतने रखता है और बची हुई space भरने के लिए उन्हें evenly stretch करता है -- अक्सर बिल्कुल भी media queries की ज़रूरत नहीं पड़ती।

Note:
  • Accepted values:
  • display: grid — एक grid बनाता है
  • grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) — responsive columns
  • gap: <length> — gutters
  • grid-column: span <n> — column spans

उदाहरण: CSS Grid Based Grid View

css
<style>
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 8px;
}
.grid div {
  background: steelblue;
  color: white;
  padding: 12px;
}
</style>
<div class="grid">
  <div>Card</div>
  <div style="background: seagreen;">Card</div>
</div>

Approaches के बीच चुनना

Percentage floats अब काफ़ी हद तक legacy हैं; flexbox card grids और toolbars जैसे single-row या single-column flows के लिए उपयुक्त है, जबकि CSS Grid genuinely two-dimensional layouts (rows AND columns दोनों को explicit control चाहिए) के लिए उपयुक्त है जैसे कोई पूरा page shell या dashboard।

उदाहरण: Choosing Between the Approaches

css
<style>
.cards {
  display: flex;
  flex-wrap: wrap;
}
.cards div {
  background: steelblue;
  color: white;
  padding: 10px;
  margin: 4px;
}
.dashboard {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}
.dashboard div {
  background: seagreen;
  color: white;
  padding: 10px;
}
</style>
<div class="cards">
  <div>Card row: Flexbox</div>
</div>
<div class="dashboard">
  <div>Two-dimensional shell: 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. padding और border शामिल होने के बाद 100% से ज़्यादा जुड़ने वाली widths इस्तेमाल करना, जिससे columns wrap हो जाते हैं; box-sizing: border-box इस्तेमाल करें।
  2. किसी float based grid में floats clear करना भूल जाना, जिससे parent collapse हो जाता है।
  3. percentages या fr units के बजाय fixed pixel column widths इस्तेमाल करना।
ब्राउज़र सपोर्ट

हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.