CSS Background Size
In this page:
selector {
background-size: auto | cover | contain | width height;
}
Explicit Dimensions
background-size एक या दो length/percentage values accept करता है -- एक value width और height दोनों को बराबर set करती है (proportionally scaled, height auto), दो values width और height को independently set करती हैं, जो image के aspect ratio को distort कर सकती हैं अगर वे उसके natural proportions से match न करें।
- Accepted values:
- auto — natural image size (default)
- cover — box को भरता है, crop हो सकता है
- contain — अंदर fit होता है, gaps छूट सकते हैं
- length — जैसे 200px या 200px 100px
- percentage — background area के relative
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Explicit Dimensions
<style>
.box {
background-image: url('https://placehold.co/100x100');
background-size: 60px 40px;
height: 100px;
}
</style>
<div class="box">Width and height set independently</div>
cover Keyword
background-size: cover image को उसके aspect ratio को बनाए रखते हुए scale करता है, जब तक वह element को पूरी तरह भरने के लिए काफ़ी बड़ी न हो जाए -- अगर element का aspect ratio image से अलग है तो image का कुछ हिस्सा crop हो सकता है, लेकिन कभी कोई खाली gap नहीं छूटेगा।
- Accepted values:
- auto — image का natural size (initial value)
- cover — box भरने के लिए scale करता है, crop हो सकता है
- contain — पूरी तरह अंदर fit होने के लिए scale करता है, gaps छूट सकते हैं
- length — जैसे 200px या 10rem
- percentage — background positioning area के relative
- dो values — पहले width फिर height, जैसे 50% auto
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: The cover Keyword
<style>
.box {
background-image: url('https://placehold.co/300x150');
background-size: cover;
height: 150px;
}
</style>
<div class="box">Fills completely, may crop</div>
contain Keyword
background-size: contain image को aspect ratio बनाए रखते हुए scale करता है, जब तक वह element के अंदर पूरी तरह fit होते हुए जितनी बड़ी हो सकती है उतनी बड़ी न हो जाए -- पूरी image हमेशा visible रहती है, लेकिन इससे खाली gaps छूट सकते हैं (background-color से भरे हुए) अगर aspect ratios match न करें।
- Accepted values:
- auto — image का natural size (initial value)
- cover — box भरने के लिए scale करता है, crop हो सकता है
- contain — पूरी तरह अंदर fit होने के लिए scale करता है, gaps छूट सकते हैं
- length — जैसे 200px या 10rem
- percentage — background positioning area के relative
- dो values — पहले width फिर height, जैसे 50% auto
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: The contain Keyword
<style>
.box {
background-image: url('https://placehold.co/300x150');
background-size: contain;
background-repeat: no-repeat;
height: 150px;
background-color: #eee;
}
</style>
<div class="box">Whole image visible, gaps possible</div>
Percentage Sizing
Percentage values background को element के अपने padding box dimensions के relative size करती हैं, image के natural size के relative नहीं -- background-size: 50% 50% image को element के area का एक चौथाई (आधी width, आधी height) occupy करवाता है।
- Accepted values:
- auto — image का natural size (initial value)
- cover — box भरने के लिए scale करता है, crop हो सकता है
- contain — पूरी तरह अंदर fit होने के लिए scale करता है, gaps छूट सकते हैं
- length — जैसे 200px या 10rem
- percentage — background positioning area के relative
- dो values — पहले width फिर height, जैसे 50% auto
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Percentage Sizing
<style>
.box {
background-image: url('https://placehold.co/300x150');
background-size: 50% 50%;
height: 150px;
}
</style>
<div class="box">Occupies a quarter of the element's area</div>
cover, contain, और Explicit Sizes के बीच चुनना
cover hero photos और full-bleed backgrounds के लिए सही है जहाँ cropping acceptable है; contain logos या product images के लिए सही है जहाँ कुछ भी कभी crop नहीं होना चाहिए; explicit pixel sizes fixed-size icons या patterns के लिए सही हैं जहाँ responsiveness से ज़्यादा predictable dimensions मायने रखते हैं।
उदाहरण: Choosing Between cover, contain, and Explicit Sizes
<style>
.hero {
background-size: cover;
background-image: url('https://placehold.co/300x100');
height: 100px;
}
.logo {
background-size: contain;
background-repeat: no-repeat;
background-image: url('https://placehold.co/300x100');
height: 100px;
}
</style>
<div class="hero">Hero: cover</div>
<div class="logo">Logo: contain</div>
background-size: 100%इस्तेमाल करना और उम्मीद करना कि image height भर देगी, जबकि heightautoहै।cover(box भरता है और crop करता है) कोcontain(अंदर fit होता है और gaps छोड़ सकता है) के साथ confuse करना।- बिना unit के
background-size: 200देना, जो invalid है।
हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- CSS Colors
- CSS RGB Colors
- CSS HEX Colors
- CSS HSL Colors
- CSS Color Keywords
- CSS Backgrounds
- CSS Background Color
- CSS Background Image
- CSS Background Repeat
- CSS Background Attachment
- CSS Background Shorthand
- CSS Multiple Backgrounds
- CSS Background Size
- CSS Background Origin
- CSS Background Clip
- CSS Borders
- CSS Border Style
- CSS Border Width
- CSS Border Color
- CSS Border Sides
- CSS Border Shorthand