← Back to CSS Course | Chapter 2: Colors & Backgrounds | Lesson 7 of 21

CSS Background Color

background-color वह अकेली property है जो किसी element के content और padding के पीछे की जगह को एक solid color से भरने के लिए ज़िम्मेदार है।
Syntax
css
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 को असर करता है।

Note:
  • 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

css
<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

css
<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 किया जा सके।

Note:
  • 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

css
<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

css
<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 के लिए उपयोगी।

Note:
  • 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

css
<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>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक background-color set करना और यह देखकर हैरान होना कि वह padding और border area के नीचे भी जाता है, क्योंकि यह element के background painting area को cover करता है (default रूप से border box)।
  2. text के मुकाबले बहुत कम contrast वाला background color चुनना।
  3. body पर background-color set करना और उम्मीद करना कि अंदर का कोई element उसे दिखाएगा, जबकि element का अपना background उसे cover कर लेता है।
ब्राउज़र सपोर्ट

हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.