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

CSS RWD Introduction

एक ऐसे single poster design की कल्पना करें जिसे postcard, billboard, और phone screen पर print होने पर बराबर अच्छा काम करना है -- responsive web design वही poster है जिसे browsers जिन endless screen sizes पर असल में चलते हैं उनके लिए फिर से सोचा गया है।
Syntax
css
selector {
  property: value;
}
@media (max-width: length) {
  selector {
    property: value;
  }
}

Responsive Design क्या Solve करता है

Responsive design से पहले, sites या तो एक अलग mobile version बनाती थीं या phone users को किसी desktop layout को pinch-zoom करने पर मजबूर करती थीं। इसके बजाय Responsive design एक ही HTML/CSS codebase ship करता है जो जिस भी viewport पर पहुँचे उसे fit करने के लिए खुद reflow हो जाता है, एक fixed pixel-perfect layout के बजाय relative sizing और conditional rules इस्तेमाल करते हुए।

उदाहरण: What Responsive Design Solves

css
<style>
.page {
  width: 100%;
  background: lightblue;
  padding: 10px;
}
</style>
<div class="page">One codebase reflows to fit any viewport</div>

Pillar One: Fluid Layouts

Fluid layouts containers को fixed pixel widths के बजाय percentages, fr units, या flex/grid ratios से size करते हैं, इसलिए अपने parent का 50% वाला एक column 50% ही रहता है चाहे parent 320px wide हो या 1920px। यह वह structural foundation है जिस पर RWD में बाकी सब कुछ बना है।

उदाहरण: Pillar One: Fluid Layouts

css
<style>
.column {
  width: 50%;
  background: lightblue;
  padding: 10px;
}
</style>
<div class="column">Always 50% of its parent, at any screen width</div>

Pillar Two: Flexible Media

Images और video को अपने खुद के rule की ज़रूरत होती है क्योंकि एक 1200px-wide photo अपने आप shrink नहीं होती -- max-width: 100% और height: auto set करना media को बिना कभी overflow या aspect ratio distort किए किसी fluid container के अंदर scale down होने देता है।

उदाहरण: Pillar Two: Flexible Media

css
<style>
img {
  max-width: 100%;
  height: auto;
}
</style>
<img src="https://placehold.co/1200x600" alt="Scales down without overflowing">

Pillar Three: Media Queries

Media queries आपको viewport width (या दूसरे features) के आधार पर conditionally CSS लागू करने देते हैं, इसलिए आप navigation, font sizes, या column counts को specific breakpoints पर restructure कर सकते हैं, सिर्फ fluid sizing पर हर layout change handle करने के लिए निर्भर रहने के बजाय।

उदाहरण: Pillar Three: Media Queries

css
<style>
.nav {
  font-size: 14px;
}
@media (min-width: 768px) {
  .nav {
    font-size: 18px;
  }
}
</style>
<nav class="nav">Responsive nav text size</nav>

तीनों Pillars कैसे Combine होते हैं

एक असली responsive page शायद ही कभी सिर्फ एक pillar इस्तेमाल करता है -- एक typical layout default रूप से fluid है, हर जगह flexible media इस्तेमाल करता है, और सिर्फ उन मुट्ठी भर points पर एक media query डालता है जहाँ अकेला fluid approach एक awkward layout produce करता है, जैसे किसी horizontal nav को stacked में बदलना।

उदाहरण: How the Three Pillars Combine

css
<style>
.container {
  width: 90%;
  background: #eef;
  padding: 10px;
}
img {
  max-width: 100%;
}
@media (min-width: 768px) {
  .container {
    width: 70%;
  }
}
</style>
<div class="container">
  <img src="https://placehold.co/600x300" alt="Fluid width, flexible media, and a media query together">
</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. viewport meta tag भूल जाना, इसलिए mobile browsers एक zoomed out desktop page दिखाते हैं।
  2. fixed pixel widths इस्तेमाल करना, इसलिए छोटे screens पर page को horizontal scrolling चाहिए होती है।
  3. सिर्फ desktop के लिए design करना और सिर्फ वहीं test करना।
ब्राउज़र सपोर्ट

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