CSS Background Color
In this page:
selector {
background-color: color;
}
एक Background Color Set करना
background-color कोई भी CSS color value accept करती है -- named, hex, rgb(), या hsl() -- और element के padding box को उस solid color से भर देती है, जो content के पीछे लेकिन page background के आगे बैठता है। यह color से independent है, जो सिर्फ text को असर करता है।
- Accepted values:
- named color — जैसे lightblue
- #rrggbb — hex code
- rgb() / hsl() — optional alpha के साथ
- transparent — कोई fill नहीं (default)
- currentcolor — text color से match करता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Setting a Background Color
<style>
.box {
background-color: rgb(200, 220, 255);
}
</style>
<div class="box">Filled behind the content, in front of the page background</div>
Background Color और Box Model
यह fill default रूप से content area और padding पर लागू होती है, border पर रुक जाती है (जब तक background-clip इसे न बदले), इसलिए padding बढ़ाना colored area को visibly बड़ा कर देता है भले ही element की declared width/height न बदली हो।
उदाहरण: Background Color and the Box Model
<style>
.box {
background-color: lightblue;
padding: 30px;
}
</style>
<div class="box">More padding visibly grows the colored area</div>
Transparent और Inherited Backgrounds
Default value transparent है, यानी बिना explicit background-color वाला element अपने पीछे जो भी है वह दिखाता है -- आमतौर पर अपने parent का background।
Background-color: transparent explicitly set करना कभी-कभी उपयोगी होता है ताकि कहीं और set किए गए color को उस declaration को पूरी तरह हटाए बिना override किया जा सके।
- Accepted values:
- transparent — कोई fill नहीं (initial value)
- named color — जैसे tomato
- #rrggbb
- rgb() | hsl() — exact colors
- rgba() | hsla() — transparency वाला color
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Transparent and Inherited Backgrounds
<style>
.parent {
background-color: yellow;
}
.child {
background-color: transparent;
}
</style>
<div class="parent">
<div class="child">Shows the parent's yellow behind it</div>
</div>
Full-Page Background Color
html या body selector पर background-color set करना पूरे visible page को color कर देता है, क्योंकि वे elements default रूप से पूरे viewport में फैले होते हैं। पूरे page का overall background color set करने या dark-mode theme के लिए base layer implement करने का यह standard तरीका है।
उदाहरण: Full-Page Background Color
<style>
body {
background-color: #111;
}
p {
color: #eee;
}
</style>
<p>Colors the entire visible page</p>
Alpha के साथ Background Color
background-color के लिए rgba() या hsla() इस्तेमाल करना fill को semi-transparent बनाता है, जिससे element के पीछे जो कुछ भी है वह थोड़ा दिख सके -- overlays, hover states, या दूसरे content के ऊपर layered subtle tinted panels के लिए उपयोगी।
- Accepted values:
- rgba(r, g, b, a) — 0 (transparent) से 1 (opaque) तक alpha
- rgb(r g b / 50%) — percentage के रूप में लिखा गया alpha
- hsla(h, s%, l%, a) — alpha वाला HSL color
- #rrggbbaa — alpha वाला 8-digit hex
- transparent — पूरी तरह transparent black का shorthand
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Background Color with Alpha
<style>
.overlay {
background-color: rgba(0, 0, 0, 0.4);
color: white;
}
</style>
<div class="overlay">Semi-transparent panel over whatever is behind it</div>
- एक
background-colorset करना और यह देखकर हैरान होना कि वह padding और border area के नीचे भी जाता है, क्योंकि यह element के background painting area को cover करता है (default रूप से border box)। - text के मुकाबले बहुत कम contrast वाला background color चुनना।
bodyपरbackground-colorset करना और उम्मीद करना कि अंदर का कोई element उसे दिखाएगा, जबकि element का अपना background उसे cover कर लेता है।
हर 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