← Back to CSS Course | Chapter 1: Introduction & Basics | Lesson 7 of 7

CSS Inheritance

Inheritance को family trait की तरह सोचें जो generations में आगे बढ़ता है: parent की eye color अपने आप children में दिख जाती है, किसी को उसे दोबारा declare नहीं करना पड़ता। लेकिन parent का घर अपने आप children का नहीं हो जाता -- कुछ चीज़ें मुफ़्त में आगे बढ़ती हैं, बाकी नहीं। CSS inheritance भी वैसे ही काम करती है। कुछ properties बिना दोबारा बताए parent से child elements तक flow करती हैं, बाकी parent पर ही रुक जाती हैं।
Syntax
css
selector {
  property: inherit;
  property: initial;
  property: unset;
}

कौन-सी Properties Default रूप से Inherit होती हैं

color, font-family, font-size, और line-height जैसी text-related properties default रूप से inherit होती हैं -- इन्हें किसी parent पर set करें और हर nested element इन्हें ले लेता है जब तक वह उन्हें override न करे। यही वजह है कि body पर एक global font set करना बिना हर paragraph और span पर दोहराए काम कर जाता है।

Note:
  • Accepted values:
  • inherit — parent की computed value लेता है
  • initial — spec की default पर reset करता है
  • unset — inheritable हो तो inherit करता है, वरना initial
  • revert — browser की default style पर वापस जाता है
  • revert-layer — पिछले cascade layer पर वापस जाता है

उदाहरण: Which Properties Inherit by Default

css
<style>
body {
  color: navy;
  font-family: sans-serif;
}
</style>
<body>
  <p>Inherits navy color and the font-family without repeating them</p>
</body>

कौन-सी Properties Inherit नहीं होतीं

border, margin, padding, width, और background जैसी box-model properties inherit NAHI होतीं -- हर element अपने खुद के default box के साथ fresh शुरू होता है, वरना हर nested div गलती से अपने parent की border और padding inherit कर लेता।

यह default खासतौर पर इसलिए मौजूद है ताकि layout properties हर element पर predictable रहें।

उदाहरण: Which Properties Don't Inherit

css
<style>
.parent {
  border: 2px solid black;
  padding: 20px;
  background: lightyellow;
}
</style>
<div class="parent">
  <div>Child has no border or padding of its own</div>
</div>

inherit Keyword

कोई भी property, यहाँ तक कि वे भी जो default रूप से inherit नहीं होतीं, को inherit keyword का इस्तेमाल करके अपने parent की computed value लेने के लिए force किया जा सकता है। यह तब उपयोगी है जब आप चाहते हैं कि कोई child जानबूझकर अपने container से match करे, property की normal initial value पर fall back करने के बजाय।

उदाहरण: The inherit Keyword

css
<style>
.parent {
  border-color: blue;
  background: lightyellow;
  padding: 10px;
}
.child {
  border: 2px solid black;
  border-color: inherit;
  padding: 8px;
  background: white;
}
</style>
<div class="parent">
  <div class="child">Border forced to match the parent's border-color</div>
</div>

initial Keyword

initial keyword किसी property को CSS specification में defined उसकी default value पर reset कर देता है, inheritance और किसी भी stylesheet rule दोनों को नज़रअंदाज़ करते हुए। यह किसी inherited या cascaded value को browser के baseline पर वापस undo करने के लिए काम का है।

उदाहरण: The initial Keyword

css
<style>
p {
  color: red;
}
.reset {
  color: initial;
}
</style>
<p class="reset">Reset back to the browser's default text color</p>

unset Keyword

unset keyword context-sensitive है: जो property normally inherit होती है उस पर, यह inherit की तरह behave करता है; जो property inherit नहीं होती उस पर, यह initial की तरह behave करता है। यह किसी property को reset करने के लिए एक सुविधाजनक single keyword है, बिना यह जाने कि उसका inheritance behavior क्या है।

उदाहरण: The unset Keyword

css
<style>
.parent {
  color: navy;
}
.child-color {
  color: unset;
}
.child-border {
  border: 2px solid black;
  border-color: unset;
  background: lightyellow;
  padding: 8px;
}
</style>
<div class="parent">
  <p class="child-color">unset acts like inherit here</p>
  <div class="child-border">unset acts like initial here</div>
</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. यह उम्मीद करना कि हर property inherit होगी, जबकि margin, padding और border जैसी box properties default रूप से inherit नहीं होतीं।
  2. किसी parent पर color set करना और यह देखकर हैरान होना कि उसके अंदर का link नीला ही रहता है, क्योंकि browser की अपनी link style सीधे a को target करती है।
  3. inherit को initial के साथ confuse करना, क्योंकि inherit parent की value इस्तेमाल करता है और initial property की default value।
चैप्टर सारांश
  • CSS HTML को style करती है, और इसका syntax और selectors यह चुनते हैं कि किन elements को style करना है।
  • CSS को अलग-अलग तरीकों से जोड़ा जा सकता है और comments से document किया जा सकता है।
  • Errors और inheritance यह समझाते हैं कि styles क्यों लागू होते हैं या क्यों लागू नहीं होते।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.