CSS object-position
In this page:
selector {
object-fit: cover;
object-position: x y;
}
object-position क्या Control करता है
object-position किसी replaced element के content (एक img या video) का alignment उसके box के अंदर set करता है जब object-fit ने scaling behavior तय कर दिया हो -- यह जवाब देता है कि cropping या letterboxing होने पर 'media का कौन-सा हिस्सा visible रहता है', background-position जैसा ही two-value syntax इस्तेमाल करते हुए।
- Accepted values:
- keywords — left, right, top, bottom, center
- percentage — जैसे 50% 50%
- length — जैसे 10px 20px
- Default 50% 50% है
- object-fit के साथ काम करता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: What object-position Controls
<style>
img {
width: 200px;
height: 100px;
object-fit: cover;
object-position: center;
}
</style>
<img src="https://placehold.co/400x400" alt="Which part of the media stays visible">
Percentage और Keyword Values इस्तेमाल करना
object-position keywords (top, bottom, left, right, center) और percentage या length values दोनों accept करता है, जहाँ percentages describe करते हैं कि image का corresponding point box के edge से कितना offset है -- 0% 0% top-left है, 100% 100% bottom-right है।
- Accepted values:
- center — default centering
- top left | top right | bottom left | bottom right — corners
- 25% 75% — percentages
- 10px 20px — top-left से length offsets
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Using Percentage and Keyword Values
<style>
.tl {
width: 150px;
height: 100px;
object-fit: cover;
object-position: 0% 0%;
}
.br {
width: 150px;
height: 100px;
object-fit: cover;
object-position: 100% 100%;
}
</style>
<img class="tl" src="https://placehold.co/400x400" alt="Top-left">
<img class="br" src="https://placehold.co/400x400" alt="Bottom-right">
यह सिर्फ Certain object-fit Values के साथ ही क्यों मायने रखता है
object-position का कोई visible effect नहीं होता जब object-fit fill हो (image को box से exactly match करने के लिए stretch किया जाता है, reposition करने के लिए कोई extra space नहीं छोड़ता) या जब image का natural size पहले से box से exactly match करता हो।
यह cover और contain के साथ सबसे उपयोगी है, जहाँ cropping या letterboxing असल में चुनने के लिए जगह छोड़ती है।
- Accepted values:
- object-position: 50% 50% — default, centered
- object-position: top | bottom | left | right | center — keywords
- object-position: <length> <length> — offsets
- object-position: <percentage> <percentage> — relative
- object-fit — visible effect के लिए cover, contain, none या scale-down चाहिए
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Why It Only Matters With Certain object-fit Values
<style>
.fill {
width: 150px;
height: 100px;
object-fit: fill;
object-position: top;
}
</style>
<img class="fill" src="https://placehold.co/400x400" alt="object-position has no visible effect with fill">
किसी Landscape Box में एक Portrait को Reposition करना
एक common real use case एक wide landscape-shaped card में रखी गई एक tall portrait photo है: object-fit: cover photo को box भरने के लिए crop करता है, और object-position: top सुनिश्चित करता है कि किसी व्यक्ति का face (आमतौर पर portrait के top के पास) center पर crop होकर गायब होने के बजाय visible रहे।
- Accepted values:
- object-position: top — किसी portrait का head visible रखता है
- object-position: 50% 20% — top की तरफ़ biased
- object-fit: cover — image को crop करता है, इसलिए position तय करती है कि क्या visible रहे
- height — box image से shorter होना चाहिए
उदाहरण: Repositioning a Portrait in a Landscape Box
<style>
.portrait {
width: 300px;
height: 120px;
object-fit: cover;
object-position: top;
}
</style>
<img class="portrait" src="https://placehold.co/300x500" alt="Face stays visible near the top">
Video Elements के साथ Combine करना
object-position video elements पर images जैसा ही identically काम करता है, किसी cropped background video को player का aspect ratio चाहे जो भी हो अपने important visual content (जैसे किसी webcam recording में speaker का face) को सही तरीके से framed रखने देते हुए।
- Accepted values:
- object-position: center — default
- object-position: <position> — images जैसी ही values
- object-fit: cover | contain | none — effect देखने के लिए चाहिए
- video element — एक replaced element, इसलिए दोनों properties लागू होती हैं
उदाहरण: Combining With Video Elements
<style>
video {
width: 300px;
height: 120px;
object-fit: cover;
object-position: top;
}
</style>
<video controls></video>
object-fitके बिनाobject-positionइस्तेमाल करना, इसलिए reposition करने के लिए कुछ नहीं है।background-imageपरobject-positionइस्तेमाल करना, जबकि यहimgऔरvideoजैसे replaced elements पर लागू होता है।- values को गलत order में लिखना, क्योंकि horizontal value पहले आती है।
हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।
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