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

CSS RWD Images

बिना किसी responsive rules वाली एक image एक fixed size पर wall से bolted किसी framed picture जैसी behave करती है -- RWD image techniques वे हैं जो उस picture को edges से लटकने के बजाय किसी छोटी wall में fit होने के लिए खुद resize होने देती हैं।
Syntax
css
img {
  max-width: 100%;
  height: auto;
}

Core Fluid Image Rule

किसी image पर max-width: 100% और height: auto set करना इसे अपने natural size से narrower किसी भी container में fit होने के लिए shrink होने देता है, अपना aspect ratio बनाए रखते हुए, बिना कभी अपनी असली pixel dimensions से आगे बढ़े और blurry हुए।

Note:
  • Accepted values:
  • max-width: 100% — parent से कभी wider नहीं
  • height: auto — aspect ratio बनाए रखता है
  • display: block — inline gap हटाता है
  • width: 100% — हमेशा parent भरता है

उदाहरण: The Core Fluid Image Rule

css
<style>
img {
  max-width: 100%;
  height: auto;
}
</style>
<img src="https://placehold.co/1200x800" alt="Shrinks to fit, keeps its aspect ratio">

height: auto क्यों मायने रखता है

height: auto के बिना सिर्फ max-width: 100% set करना height को image की natural pixel height पर fixed छोड़ सकता है भले ही width shrink हो, image को distort करते हुए -- height: auto browser को बताता है कि width बदलने पर height को proportionally recalculate करे।

Note:
  • Accepted values:
  • height: auto — default, height ratio को follow करती है
  • height: <length> — एक fixed height image को distort करती है
  • width और height attributes — browser aspect-ratio से जगह reserve करता है
  • max-width: 100% — height: auto के साथ जोड़ता है

उदाहरण: Why height: auto Matters

css
<style>
.distorted {
  max-width: 100%;
}
.correct {
  max-width: 100%;
  height: auto;
}
</style>
<img class="distorted" src="https://placehold.co/800x400" alt="Height stays fixed, can distort">
<img class="correct" src="https://placehold.co/800x400" alt="Height recalculates proportionally">

अलग Image Sizes Serve करना

CSS से scale down की गई एक single बड़ी image किसी छोटी phone screen पर भी अपनी पूरी file size download करती है, bandwidth बर्बाद करते हुए -- HTML srcset और sizes attributes (और art-direction cases के लिए <picture> element) browser को हर device के लिए एक उपयुक्त-size file चुनने देते हैं।

Note:
  • Accepted values:
  • srcset="a.jpg 480w, b.jpg 1200w" — widths वाले image candidates
  • sizes="(max-width: 600px) 480px, 1200px" — display width hints
  • srcset="a.jpg 1x, b.jpg 2x" — density descriptors
  • <picture> और <source media="..."> — art direction

उदाहरण: Serving Different Image Sizes

css
<style>
img { max-width: 100%; height: auto; border: 3px solid #1e6fd9; background: #cfe8ff; }
<img srcset="small.jpg 480w, large.jpg 1200w"
     sizes="(max-width: 600px) 480px, 1200px"
     src="large.jpg" alt="Browser picks the right file size per device">
</style>
<img srcset="small.jpg 480w, large.jpg 1200w"
     sizes="(max-width: 600px) 480px, 1200px"
     src="large.jpg" alt="Browser picks the right file size per device">

Background Images बनाम Inline Images

CSS में background-image <img> की तरह max-width/height: auto को respond नहीं करता -- responsive background images इसके बजाय background-size: cover या contain पर निर्भर करती हैं, ज़रूरत पड़ने पर खुद image file बदलने के लिए media queries के साथ combined।

Note:
  • Accepted values:
  • background-image: url(...) — image
  • background-size: cover | contain | <length> — sizing
  • background-position: <position> — placement
  • background-repeat: no-repeat — कोई tiling नहीं
  • image-set() — backgrounds के लिए resolution switching

उदाहरण: Background Images vs Inline Images

css
<style>
.hero {
  background-image: url('https://placehold.co/1200x400');
  background-size: cover;
  height: 200px;
}
</style>
<div class="hero">background-size handles responsiveness here, not max-width</div>

Cropped Responsive Images के लिए object-fit

जब किसी image को बिना distortion के exactly एक fixed-aspect-ratio box (जैसे एक square avatar या card thumbnail) भरना हो, तो object-fit: cover image को box भरने के लिए crop करता है जबकि object-fit: contain इसे इसके बजाय letterbox करता है -- दोनों किसी भी container size के साथ काम करते हैं।

Note:
  • Accepted values:
  • object-fit: cover — box भरता है, overflow crop करता है
  • object-fit: contain — अंदर fit होता है, letterbox हो सकता है
  • object-fit: fill — default, box में stretch होता है
  • object-fit: none | scale-down — no resize / none और contain में से छोटा
  • object-position: <position> — crop focus चुनता है
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: object-fit for Cropped Responsive Images

css
<style>
img {
  width: 150px;
  height: 150px;
  object-fit: cover;
}
</style>
<img src="https://placehold.co/400x200" alt="Cropped to fill the square without distortion">
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. height: auto भूल जाना, इसलिए height set होने पर image stretch हो जाती है।
  2. सिर्फ width: 100% इस्तेमाल करना, जो छोटी images को बड़ा और blurry बना सकता है, जबकि max-width: 100% इसे रोकता है।
  3. phones को एक बड़ी image serve करना, जो data बर्बाद करता है।
ब्राउज़र सपोर्ट

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