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

CSS RWD Videos

बिना responsive rules वाला एक embedded video एक fixed size पर wall में bolted किसी TV जैसा है -- responsive video techniques उसी screen को बिना कभी stretched या cropped हुए जिस भी room में यह पहुँचे उसमें fit होने के लिए खुद resize होने देती हैं।
Syntax
css
video {
  max-width: 100%;
  height: auto;
}

Video Aspect Ratio Problem

किसी image के उलट, एक <video> या embedded <iframe> (जैसे कोई YouTube embed) CSS से उसकी width constrain होने पर automatically अपना aspect ratio preserve नहीं करता -- naive resizing या तो video को distort कर देता है या भद्दे black bars छोड़ता है जब तक aspect ratio explicitly maintain न किया जाए।

उदाहरण: The Video Aspect Ratio Problem

css
<style>
iframe {
  width: 100%;
}
</style>
<iframe src="about:blank" height="200"></iframe>

Padding-Top Percentage Trick

Native CSS support मौजूद होने से पहले, standard technique video को padding-top: 56.25% (16:9 ratio के लिए percentage) वाले container में wrap करती थी और उस padded box को भरने के लिए video को absolute position करती थी, इस fact का फ़ायदा उठाते हुए कि percentage padding top/bottom sides के लिए भी container की WIDTH से calculate होती है।

Note:
  • Accepted values:
  • padding-top: 56.25% — 16 / 9 (9 / 16 = 0.5625)
  • padding-top: 75% — 4 / 3
  • position: relative — wrapper पर
  • position: absolute; inset: 0 — अंदर का iframe

उदाहरण: The Padding-Top Percentage Trick

css
<style>
.wrapper {
  position: relative;
  padding-top: 56.25%;
  background: #ddd;
}
.wrapper iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>
<div class="wrapper">
  <iframe src="about:blank"></iframe>
</div>

Modern aspect-ratio Property

aspect-ratio CSS property (जैसे aspect-ratio: 16 / 9) अब natively वही करती है जो padding-top hack simulate करता था, किसी video या उसके wrapper को बिना किसी absolute positioning wrapper trick के सीधे एक fixed width-to-height ratio बनाए रखने देते हुए।

Note:
  • Accepted values:
  • aspect-ratio: 16 / 9 — widescreen video
  • aspect-ratio: 4 / 3 — classic monitor
  • aspect-ratio: auto — natural ratio इस्तेमाल करता है (initial value)
  • width: 100% — height automatically follow करती है
  • height: auto — ratio लागू होने के लिए चाहिए
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: The Modern aspect-ratio Property

css
<style>
iframe {
  width: 100%;
  aspect-ratio: 16 / 9;
}
</style>
<iframe src="about:blank"></iframe>

Native Video Elements पर max-width

एक native <video> element (कोई iframe embed नहीं) images जैसे ही max-width: 100%; height: auto; rule को respond करता है, क्योंकि browser को पहले से पता है और यह उसका intrinsic aspect ratio preserve करता है।

padding-trick/aspect-ratio techniques मुख्य रूप से उन iframe embeds के लिए चाहिए जो अपना intrinsic ratio CSS को नहीं दिखाते।

Note:
  • Accepted values:
  • max-width: 100% — parent से कभी wider नहीं
  • height: auto — ratio बनाए रखता है
  • width: 100% — parent भरता है
  • नोट: iframe embeds अपना ratio नहीं दिखाते

उदाहरण: max-width on Native Video Elements

css
<style>
video {
  max-width: 100%;
  height: auto;
}
</style>
<video controls></video>

Responsive Video के लिए Performance Considerations

Autoplaying background video mobile data और battery पर खासतौर पर costly है -- responsive video strategy में अक्सर एक certain breakpoint से नीचे पूरे video के बजाय एक static poster image या lower-resolution source serve करना शामिल होता है।

Note:
  • Accepted values:
  • preload="none | metadata | auto" — video का कितना हिस्सा download करना है (HTML attribute)
  • loading="lazy" — offscreen iframes को defer करता है
  • poster="<url>" — एक preview image
  • src — छोटी screens के लिए छोटे encodes

उदाहरण: Performance Considerations for Responsive Video

css
<style>
video {
  width: 100%;
}
@media (max-width: 600px) {
  video {
    display: none;
  }
}
</style>
<video poster="https://placehold.co/600x300" controls></video>
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. किसी video पर एक fixed width और height set करना, जो scale नहीं होता।
  2. padding trick wrapper के अंदर iframe पर position: absolute भूल जाना।
  3. गलत side से padding percentage इस्तेमाल करना, क्योंकि 56.25% सिर्फ 16:9 देता है।
ब्राउज़र सपोर्ट

aspect-ratio property Chrome 88+, Firefox 89+ और Safari 15+ में supported है; padding-top technique हर modern browser में काम करती है।

🔒

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.