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

CSS RWD Media Queries

Media query syntax CSS का "if" statement है; RWD breakpoint strategy यह तय करने का ज़्यादा मुश्किल सवाल है कि अपने ifs कहाँ रखें ताकि layout बीच की हर width पर असल में अच्छा दिखे, सिर्फ उस exact pixel पर नहीं जिसे आपने test किया।
Syntax
css
@media (max-width: length) {
  selector {
    property: value;
  }
}

Syntax से Strategy तक

@media (max-width: 600px) { } syntax जानना काम का सिर्फ आधा हिस्सा है -- responsive breakpoint strategy में असली skill यह तय करना है कि कितने breakpoints इस्तेमाल करें और उन्हें कहाँ रखें, यह किसी generic device-size chart पर नहीं बल्कि इस बात पर आधारित होना चाहिए कि आपका content असल में कहाँ cramped दिखना शुरू होता है।

Note:
  • Accepted values:
  • @media (min-width: <length>) — किसी width पर या उससे ऊपर
  • @media (max-width: <length>) — किसी width पर या उससे नीचे
  • @media (orientation: portrait | landscape) — orientation
  • @media (prefers-color-scheme: dark) — user preference
  • and | not | only | , — queries combine करें (comma का मतलब or है)

उदाहरण: From Syntax to Strategy

css
<style>
.box {
  background: lightblue;
}
@media (max-width: 600px) {
  .box {
    background: lightgreen;
  }
}
</style>
<div class="box">Breakpoint chosen where this content looks cramped</div>

Mobile-First Breakpoint Order

एक mobile-first strategy पहले सबसे छोटी screen के लिए unprefixed styles लिखती है, फिर screen बढ़ने पर min-width media queries layer करती है, इसलिए हर breakpoint सिर्फ complexity ADD करता है (ज़्यादा columns, wider spacing) पहले के rules को override करने के बजाय।

Note:
  • Accepted values:
  • @media (min-width: 576px) — छोटी screens और ऊपर
  • @media (min-width: 768px) — tablets और ऊपर
  • @media (min-width: 992px) — desktops और ऊपर
  • @media (min-width: 1200px) — बड़े desktops
  • base styles — सबसे छोटी screens पर लागू होते हैं

उदाहरण: Mobile-First Breakpoint Order

css
<style>
.columns {
  display: block;
  background: #eee;
}
@media (min-width: 768px) {
  .columns {
    display: flex;
  }
}
.columns div {
  background: steelblue;
  color: white;
  padding: 10px;
}
</style>
<div class="columns">
  <div>Adds complexity as the screen grows</div>
</div>

Desktop-First Breakpoint Order

एक desktop-first strategy उल्टा करती है -- पूरी desktop styles default हैं, और max-width queries screen shrink होने पर complexity हटाती हैं। यह पुरानी desktop-only sites के retrofits में common है लेकिन आमतौर पर mobile-first से ज़्यादा CSS mobile users को भेजती है।

Note:
  • Accepted values:
  • @media (max-width: 1199px) — बड़े desktops से नीचे
  • @media (max-width: 991px) — desktops से नीचे
  • @media (max-width: 767px) — tablets से नीचे
  • @media (max-width: 575px) — phones
  • base styles — सबसे बड़ी screens पर लागू होते हैं

उदाहरण: Desktop-First Breakpoint Order

css
<style>
.columns {
  display: flex;
  background: #eee;
}
@media (max-width: 768px) {
  .columns {
    display: block;
  }
}
.columns div {
  background: steelblue;
  color: white;
  padding: 10px;
}
</style>
<div class="columns">
  <div>Strips complexity away as the screen shrinks</div>
</div>

Content-Driven बनाम Device-Driven Breakpoints

specific popular device widths (320px iPhone, 768px iPad, वगैरह) से match करने के लिए breakpoints चुनना fragile है क्योंकि नए devices लगातार आते रहते हैं।

ज़्यादा durable strategy है अपनी browser window resize करना और exactly वहाँ एक breakpoint जोड़ना जहाँ आपका layout टूटना शुरू करता है, चाहे वह किसी भी device से match करे।

Note:
  • Accepted values:
  • @media (min-width: <content breakpoint>) — जहाँ layout टूटता है
  • em — em में breakpoints user font size का सम्मान करते हैं
  • px — device-independent width
  • नोट: breakpoints ढूँढने के लिए browser resize करें

उदाहरण: Content-Driven vs Device-Driven Breakpoints

css
<style>
.text {
  font-size: 16px;
}
@media (max-width: 540px) {
  .text {
    font-size: 14px;
  }
}
</style>
<p class="text">Breakpoint chosen where this layout breaks, not a device width</p>

Common Breakpoint Conventions

ऊपर दी गई content-driven advice के बावजूद, ज़्यादातर teams अभी भी एक starting point के तौर पर round-number ranges के एक छोटे set पर converge होती हैं -- लगभग 576px, 768px, 992px, और 1200px -- क्योंकि वे phones, tablets, और desktops की practical range को इतनी अच्छी तरह cover करती हैं कि हर project में wheel दोबारा invent करने से सबको बचाती हैं।

Note:
  • Accepted values:
  • 576px — small breakpoint
  • 768px — medium breakpoint
  • 992px — large breakpoint
  • 1200px — extra large breakpoint
  • 1400px — extra extra large breakpoint

उदाहरण: Common Breakpoint Conventions

css
<style>
.container {
  width: 100%;
  background: steelblue;
  color: white;
  padding: 10px;
}
@media (min-width: 576px) { .container { width: 540px; background: seagreen; } }
@media (min-width: 768px) { .container { width: 720px; background: coral; } }
@media (min-width: 992px) { .container { width: 960px; background: darkorange; } }
</style>
<div class="container">Common round-number breakpoints</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. बिना unit के @media (max-width: 600) लिखना, जो invalid है।
  2. media queries को base rules से पहले रखना, इसलिए वे बराबर specificity वाली बाद की styles से override हो जाती हैं।
  3. overlapping ranges, जैसे max-width: 600px और min-width: 600px, इसलिए exactly 600px पर दोनों rules लागू होते हैं।
ब्राउज़र सपोर्ट

हर 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.