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

CSS Responsive Design

अपनी shop items दिखाने के लिए एक खूबसूरत, widescreen display cabinet बनाने की कल्पना करें। यह किसी विशाल showroom wall पर perfect दिखता है। लेकिन अब, आपसे कहा जाता है कि उसी exact cabinet को एक छोटे telephone booth के अंदर fit करने के लिए squeeze करें। Responsive Design इस design challenge को solve करता है। यह ऐसे web pages बनाने की कला है जो automatically scale, resize, और अपने content को wrap करते हैं ताकि wide desktop monitors से लेकर compact mobile phone screens तक हर size की screens पर खूबसूरत दिखें।
Syntax
css
@media (max-width: length) {
  selector {
    property: value;
  }
}

Mobile-First Workflow

Mobile-first design industry standard workflow है। आप पहले छोटे mobile screens के लिए अपनी default styles लिखते हैं, फिर screen sizes बढ़ने पर layout columns और spacing जोड़ने के लिए min-width queries इस्तेमाल करते हैं।

Note: अपने layouts mobile-first शुरू करना आपकी stylesheets को साफ़, simple, और mobile connections पर तेज़ रखता है।
Warning: mobile-first design न करना mobile पर desktop styles reset करने के लिए complex, भारी code overrides लिखने की ज़रूरत डाल सकता है।

उदाहरण: The Mobile-First Workflow

css
<style>
.box {
  background: lightblue;
}
@media (min-width: 768px) {
  .box {
    background: lightgreen;
  }
}
</style>
<div class="box">Mobile-first: default style, then a min-width override</div>

Fluid Container Layout Structures

Container widths को fixed pixels में define करने के बजाय (जो छोटे mobile screens पर overflow हो सकता है), fluid layouts relative percentage values इस्तेमाल करते हैं। यह आपके columns और boxes को किसी भी screen size में fit होने के लिए dynamically shrink और expand करने देता है।

Note: अपने content को विशाल screens पर बहुत wide stretch होने से रोकने के लिए एक percentage width को एक max-width setting के साथ combine करें।
Warning: बड़े containers पर fixed pixel widths set करने से बचें ताकि छोटे screens पर layout cutoff और horizontal scrolling issues रोके जा सकें।

उदाहरण: Fluid Container Layout structures

css
<style>
.container {
  width: 90%;
  max-width: 1200px;
  margin: 0 auto;
  background: lightblue;
  padding: 10px;
}
</style>
<div class="container">Fluid container</div>

Scalable Responsive Media Images

बड़ी images display के side से बाहर चलकर mobile screens पर आसानी से आपका page layout तोड़ सकती हैं। CSS इस्तेमाल करके relative widths और height controls लागू करके, आप अपनी images को किसी भी device size में fit होने के लिए automatically scale down करा सकते हैं।

Note:
  • अपनी images को साफ़ तरीके से scale और proportional बनाए रखने के लिए style rules max-width: 100% और height: auto इस्तेमाल करें।
  • Accepted values:
  • max-width: 100% — parent से कभी wider नहीं
  • height: auto — aspect ratio बनाए रखता है
  • display: block — inline gap हटाता है
  • width: 100% — हमेशा parent भरता है
Warning: images पर fixed pixel width attributes set करने से बचें जब तक आप न चाहें कि वे सभी screen dimensions में एक static size बनी रहें।

उदाहरण: Scalable Responsive Media Images

css
<style>
img {
  max-width: 100%;
  height: auto;
}
</style>
<img src="https://placehold.co/600x300" alt="Responsive image">

Fluid Typography (rem और em)

Fixed pixel font sizes (जैसे font-size: 16px) अलग-अलग screens में adjust नहीं होते, जो text को पढ़ना मुश्किल बना सकता है। relative CSS units (जैसे em, rem, या viewport units) इस्तेमाल करना आपकी text sizes को user के browser या screen dimensions के relative dynamically scale होने देता है।

Note: सभी screen sizes में consistent, accessible text scaling के लिए rem units इस्तेमाल करें।
Warning: font sizes के लिए viewport width units (vw) इस्तेमाल करते समय सावधान रहें, क्योंकि mobile devices पर text tiny हो सकता है।

उदाहरण: Fluid typography (rem and em)

css
<style>
h1 {
  font-size: 2rem;
}
p {
  font-size: 1em;
}
</style>
<h1>Scales with root font size</h1>
<p>Scales with parent font size</p>

Responsive Navigation Menus

Desktops के लिए design किए गए navigation bars space limits की वजह से mobile screens पर आसानी से टूट सकते हैं। एक responsive menu बनाने के लिए, media queries इस्तेमाल करें ताकि छोटे screens पर आपके navigation links vertically stack हों या एक interactive toggle button के पीछे छुपें।

Note: साफ़, responsive navigation menus आसानी से बनाने के लिए mobile-first Flexbox column wraps इस्तेमाल करें।
Warning: सुनिश्चित करें कि आपके mobile menus में touch navigation के लिए आसानी से clickable targets हों।

उदाहरण: Responsive Navigation Menus

css
<style>
.nav {
  display: flex;
  background: #eef;
  padding: 10px;
  gap: 10px;
}
@media (max-width: 600px) {
  .nav {
    flex-direction: column;
  }
}
</style>
<nav class="nav">
  <a href="#">Home</a>
  <a href="#">About</a>
</nav>
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. अपने head section के अंदर viewport meta tag शामिल करना भूल जाना, जो mobile layout rendering तोड़ देता है।
  2. container elements पर hard-coded, fixed pixel widths set करना, layout cutoff और horizontal scrolling issues की ओर ले जाते हुए।
  3. images पर responsive scaling settings को नज़रअंदाज़ करना, जिससे वे अपने containers से overflow हो जाती हैं।
चैप्टर सारांश
  • Responsive design सुनिश्चित करता है कि आपके webpages सभी sizes की screens पर automatically खूबसूरत दिखने के लिए adapt हों।
  • पहले छोटे mobile screens के लिए अपनी default styles लिखें, फिर अपने layouts को scale up करने के लिए min-width queries इस्तेमाल करें।
  • layout breaks रोकने के लिए fluid percentages, relative font units (rem), और responsive image configurations इस्तेमाल करें।
ब्राउज़र सपोर्ट

Modern responsive design attributes, media queries, और relative units हर web browser द्वारा natively supported हैं।

🔒

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.