🖼️
Responsive Design

Responsive Images (CSS)

max-width: 100%, object-fit
💡 max-width: 100% ek image ko "apne container se bade mat ho" bolne jaisa hai — jaise ek photo jo apne frame se bahar nahi nikal sakti, chahe original photo kitni bhi badi ho.

img { max-width: 100%; height: auto; } ek bahut common, almost universal CSS reset hai — images ko automatically apne container ke andar fit karta hai, kabhi overflow nahi hota, aspect ratio bhi maintain rehta hai (height: auto ki wajah se).

object-fit (pehle dekha) images ko container ke andar "crop" ya "fit" karne ke liye — especially jab image ka natural aspect ratio container se match nahi karta.

img {
  max-width: 100%;   /* kabhi container se bada nahi hoga */
  height: auto;         /* aspect ratio maintain, distort nahi hoga */
}

.thumbnail {
  width: 200px;
  height: 200px;
  object-fit: cover;   /* fixed size mein crop karke fit */
}
🖼️
max-width: 100% ek image ko "apne container se bade mat ho" bolne jaisa hai — jaise ek photo jo apne frame se bahar nahi nikal sakti, chahe original photo kitni bhi badi ho.
1 / 2
⚡ Quick Recap
  • img { max-width: 100%; height: auto; } = almost universal safe default
  • Images kabhi container se overflow nahi honge
  • object-fit = crop/fit control jab aspect ratios match na karein
On this page (2 subtopics)

aspect-ratio (modern CSS) element ka width:height ratio maintain karta hai — video embeds, image placeholders ke liye bahut useful, "layout shift" avoid karne ke liye (jab tak actual content load ho).

.video-wrapper {
  aspect-ratio: 16 / 9;   /* hamesha 16:9 ratio maintain */
  width: 100%;
}

.square-thumbnail {
  aspect-ratio: 1 / 1;   /* perfect square, kisi bhi width par */
}
💡Tip: aspect-ratio se pehle, "padding-hack" (padding-top: 56.25% jaisa trick) use hota tha 16:9 ratios maintain karne ke liye — modern CSS mein aspect-ratio se ye much simpler, readable hai.

background-image (unlike <img>) alt text support nahi karta (accessibility concern — purely decorative images ke liye theek hai) aur bandwidth-conscious loading (jaise srcset) bhi directly support nahi karta — content-critical images ke liye <img> better choice hai.

/* Decorative background — CSS theek hai: */
.hero-section {
  background-image: url('decorative-pattern.jpg');
}

/* Content-critical image — <img> better hai (alt text, srcset): */
/* <img src="product.jpg" alt="Product photo" srcset="..."> */
💡Tip: Rule of thumb: agar image CONTENT hai (product photo, informative), <img> tag use karo. Agar purely DECORATIVE hai (background pattern, texture), CSS background-image theek hai.