CSS Aspect Ratio
In this page:
selector {
aspect-ratio: width / height;
}
aspect-ratio का Basic Syntax
aspect-ratio property एक forward slash से अलग की गई width और height accept करती है, और browser जो भी एक set है उससे missing dimension compute कर लेता है। इसका मतलब है आपको सिर्फ कभी एक width या एक height set करने की ज़रूरत है, दोनों नहीं, और browser बाकी संभाल लेता है।
- आप एक perfect square के लिए shorthand के रूप में aspect-ratio: 1 लिख सकते हैं, जो aspect-ratio: 1 / 1 के बराबर है।
- Accepted values:
- ratio — width / height, जैसे 16 / 9
- number — single number जैसे square के लिए 1
- auto — element का natural ratio इस्तेमाल करता है (default)
- auto ratio — available होने पर natural ratio, वरना दी गई ratio
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Basic Syntax of aspect-ratio
<style>
.box {
width: 300px;
aspect-ratio: 16 / 9;
background: steelblue;
color: white;
}
</style>
<div class="box">Height computed automatically from the width</div>
16 by 9 Video Containers
16 by 9 standard widescreen video ratio है, और aspect-ratio बिना wrapper divs या padding tricks के responsive video players embed करना simple बना देता है। यह property मौजूद होने से पहले, developers इसी responsive effect को achieve करने के लिए padding-top percentage hack पर निर्भर थे।
- किसी iframe पर aspect-ratio: 16 / 9 के साथ width: 100% set करें ताकि video अपने parent की width भर दे और height automatically derive करे।
- Accepted values:
- aspect-ratio: 16 / 9 — widescreen video
- aspect-ratio: 4 / 3 — classic monitor
- width: 100% — height automatically follow करती है
- height: auto — ratio लागू होने के लिए चाहिए
उदाहरण: 16 by 9 Video Containers
<style>
iframe {
width: 100%;
aspect-ratio: 16 / 9;
}
</style>
<iframe src="about:blank"></iframe>
किसी Gallery में Square Images
1 by 1 ratio thumbnail grids को visually consistent रखती है यहाँ तक कि जब source photos अलग-अलग original sizes में आते हों। यह उस layout jump से बचाता है जो fixed-size grid cells में अलग-अलग dimensions वाली images load होने पर होता है।
- aspect-ratio: 1 को inner img पर object-fit: cover के साथ combine करें ताकि हर thumbnail square और uncropped-looking दोनों हो।
- Accepted values:
- aspect-ratio: 1 / 1 — square
- aspect-ratio: 1 — 1 / 1 जैसा ही
- object-fit: cover — बिना distortion के square भरता है
- width: 100% — height follow करती है
उदाहरण: Square Images in a Gallery
<style>
img {
aspect-ratio: 1;
object-fit: cover;
width: 150px;
}
</style>
<img src="https://placehold.co/300x200" alt="Cropped to a perfect square">
अपना Shape बनाए रखने वाले Responsive Boxes
aspect-ratio media तक सीमित नहीं है, कोई भी box जैसे card, banner, या map placeholder अलग-अलग screens पर अपनी width बदलने पर एक consistent shape बनाए रख सकता है। यह इसे एक general-purpose layout tool बनाता है, सिर्फ image-specific property नहीं।
- skeleton loading placeholders पर aspect-ratio इस्तेमाल करें ताकि असली content के replace होने पर page layout jump न करे।
- Accepted values:
- aspect-ratio: <ratio> — height width से derive होती है
- max-width: <length> — box को cap करता है
- width: <percentage> — flexible width
- min-height — अगर content taller है तो ratio को override करता है
उदाहरण: Responsive Boxes That Hold Their Shape
<style>
.card {
width: 100%;
aspect-ratio: 4 / 3;
background: steelblue;
color: white;
}
</style>
<div class="card">Holds its shape as width changes</div>
aspect-ratio मौजूद होने से पहले Fallback
पुराने codebases padding-bottom percentage trick इस्तेमाल करते थे, किसी खाली element पर width के percentage के रूप में padding-bottom लागू करते हुए एक ratio force करने के लिए, क्योंकि percentage padding हमेशा width से calculate होती है।
- बहुत पुराने browsers को support करते समय, एक modern aspect-ratio declaration के साथ padding-bottom hack को fallback के तौर पर रखें।
- Accepted values:
- aspect-ratio: 16 / 9 — modern approach
- padding-top: 56.25% — 16 / 9 के लिए पुराना padding hack
- position: relative — hack के लिए wrapper पर
- position: absolute — hack में inner content के लिए
उदाहरण: Falling Back Before aspect-ratio Existed
<style>
.ratio-box {
position: relative;
padding-bottom: 56.25%;
background: #ddd;
}
.ratio-box > * {
position: absolute;
inset: 0;
background: steelblue;
color: white;
}
</style>
<div class="ratio-box">
<div>The old padding-bottom percentage trick</div>
</div>
- aspect-ratio और एक explicit height दोनों set करना, एक explicit height हमेशा ratio को override करती है और property का कोई visible effect नहीं होता।
- ratio को उल्टा लिखना, aspect-ratio: 9 / 16 एक wide landscape box के बजाय एक tall portrait box produce करता है।
- यह भूल जाना कि aspect-ratio सिर्फ shape reserve करता है, यह media को automatically crop या fit नहीं करता, आपको अंदर की images के लिए फिर भी object-fit चाहिए।
- aspect-ratio: width / height किसी box के proportions को lock कर देता है ताकि height उसकी width से automatically derive हो।
- यह responsive video और image containers के लिए पुराने padding-bottom percentage hack को replace करता है।
- box shape और image proportions दोनों को सही रखने के लिए inner media पर aspect-ratio को object-fit: cover के साथ combine करें।
aspect-ratio 2021 से हर modern browser द्वारा supported है, पुराने browsers इसे चुपचाप नज़रअंदाज़ करके content की natural height पर fall back हो जाते हैं।
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep