CSS Backgrounds
In this page:
selector {
background-color: color;
background-image: url("image.jpg");
background-repeat: repeat | no-repeat;
}
background-color
background-color property किसी element के content के पीछे की जगह को एक solid color से भरती है, CSS के support किए गए किसी भी color format का इस्तेमाल करके। यह सबसे ज़्यादा इस्तेमाल होने वाली CSS properties में से एक है, जो किसी भी background image के नीचे base layer बनाती है।
- एक subtle, light background-color किसी page के एक section को दूसरे से visually अलग करने का एक आसान तरीका है।
- Accepted values:
- color — कोई भी CSS color: name, hex, rgb(), hsl()
- transparent — कोई color नहीं (initial value)
- currentcolor — element की text color
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: background-color
<style>
.box {
background-color: lightblue;
}
</style>
<div class="box">Filled with a solid background color</div>
background-image
background-image property किसी element के content के पीछे एक image रखती है, जिसे url() से reference किया जाता है। इसे background-color के साथ combine किया जा सकता है ताकि image load होने तक एक fallback मिले।
- हमेशा अपनी image के dominant color के करीब एक background-color set करें, ताकि image load न होने पर भी layout ठीक-ठाक दिखे।
- Accepted values:
- url("...") — एक image file
- none — कोई image नहीं (initial value)
- linear-gradient() — एक straight-line color gradient
- radial-gradient() — एक circular या elliptical gradient
- conic-gradient() — किसी center point के चारों ओर sweep होने वाला gradient
- commas से अलग की गई कई values — stacked layers
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: background-image
<style>
.box {
background-color: #ddd;
background-image: url('https://placehold.co/300x150');
}
</style>
<div class="box">Image with a fallback color behind it</div>
background-repeat और background-position
Default रूप से, एक background image अपने element को भरने के लिए tile होती है। background-repeat control करती है कि वह tile हो भी या नहीं, और background-position control करती है कि image ठीक कहाँ से शुरू हो। इन दो properties को गलत करना किसी background image के stretched, cut off, या misaligned दिखने का एक आम कारण है।
- जब भी आपको tiled pattern के बजाय एक single, साफ़ image चाहिए हो तो background-repeat: no-repeat set करें।
- Accepted values:
- repeat | no-repeat | repeat-x | repeat-y | space | round — background-repeat के लिए tiling
- left / center / right — horizontal position keywords
- top / center / bottom — vertical position keywords
- length — top-left से offset, जैसे 20px 10px
- percentage — 50% 50% image को center करता है
- edge offsets — जैसे right 10px bottom 20px
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: background-repeat and background-position
<style>
.box {
background-image: url('https://placehold.co/50x50');
background-repeat: no-repeat;
background-position: center;
height: 150px;
}
</style>
<div class="box">Single centered image, no tiling</div>
background-size
background-size control करती है कि एक background image कितनी बड़ी render होती है। cover जैसे special keywords image को उसके element को पूरी तरह भरने के लिए scale करते हैं, जबकि contain उसे बिना crop किए पूरी तरह अंदर fit करने के लिए scale करता है।
- Full-width hero banners के लिए background-size: cover इस्तेमाल करें, यह container के exact dimensions चाहे जो भी हों उस जगह को अच्छी तरह भर देता है।
- Accepted values:
- auto — image का natural size (initial value)
- cover — box भरने के लिए scale करता है, crop हो सकता है
- contain — पूरी तरह अंदर fit होने के लिए scale करता है, gaps छूट सकते हैं
- length — जैसे 200px या 10rem
- percentage — background positioning area के relative
- do values — पहले width फिर height, जैसे 50% auto
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: background-size
<style>
.box {
background-image: url('https://placehold.co/300x150');
background-size: cover;
height: 150px;
}
</style>
<div class="box">Fills the element completely</div>
Shorthand background Property
background shorthand property आपको color, image, repeat, position, और size एक साथ एक ही declaration में set करने देती है, पाँच अलग properties लिखने के बजाय। Shorthand इस्तेमाल करना गलती से उन properties को reset कर सकता है जिन्हें आपने explicitly शामिल नहीं किया, इसलिए पूरी value list double-check करना ठीक रहता है।
- एक बार जब आप individual background properties में सहज हो जाएँ, तो shorthand version आपकी CSS को बिना clarity खोए छोटा रखता है।
- Accepted values:
- color — जैसे #eee
- image — url() या एक gradient
- position / size — जैसे center / cover
- repeat — जैसे no-repeat
- attachment — scroll, fixed या local
- origin और clip — border-box, padding-box या content-box
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Shorthand background Property
<style>
.box {
background: #ddd url('https://placehold.co/300x150') no-repeat center / cover;
height: 150px;
}
</style>
<div class="box">Five properties in one declaration</div>
- background-image इस्तेमाल करते समय एक fallback background-color set करना भूल जाना, इस स्थिति में कि image load न हो पाए।
- background-size set न करना, जिससे background image बहुत बड़ी या अजीब तरीके से cropped रह सकती है।
- background-repeat का ज़रूरत से ज़्यादा इस्तेमाल करना जबकि एक अच्छी तरह positioned single image ज़्यादा साफ़ दिखती।
- background-color किसी element के content के पीछे एक solid color set करता है।
- background-image, background-repeat, background-position, और background-size control करते हैं कि कोई image background कैसे behave करता है।
- background shorthand property इनमें से कई settings को एक ही declaration में combine कर सकती है।
यहाँ बताई गई सभी standard CSS background properties हर modern browser में supported हैं।
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