CSS Specificity
In this page:
/* specificity: inline > #id > .class > element */
element { }
.class-name { }
#id-name { }
Specificity Score Calculation
Specificity को एक चार-हिस्सों वाले score (0,0,0,0) के रूप में calculate किया जाता है। सबसे ज़्यादा से सबसे कम, columns inline styles, IDs, classes/attributes/pseudo-classes, और element tags represent करते हैं।
उदाहरण: The Specificity Score Calculation
<style>
p { color: black; }
.text { color: blue; }
#main { color: red; }
</style>
<p id="main" class="text">Highest specificity wins: red</p>
Element बनाम Class Specificity
Element selectors सीधे HTML tags को target करते हैं, जबकि class selectors custom classes को target करते हैं। एक class selector हमेशा किसी element tag selector पर जीतता है क्योंकि यह ज़्यादा specific है।
उदाहरण: Element vs. Class Specificity
<style>
p { color: black; }
.highlight { color: green; }
</style>
<p class="highlight">Class beats element: green</p>
Class बनाम ID Specificity
एक ID selector आपके page पर एक single, unique element को point करता है। क्योंकि IDs unique होते हैं, वे group styling classes से कहीं ज़्यादा specificity weight रखते हैं।
उदाहरण: Class vs. ID Specificity
<style>
.text { color: green; }
#unique { color: purple; }
</style>
<p id="unique" class="text">ID beats class: purple</p>
Inline Styles Override
Inline styles सीधे आपके HTML elements के अंदर style attribute इस्तेमाल करके declare की जाती हैं। क्योंकि वे सीधे element पर defined हैं, उनका specificity score (1,0,0,0) है और वे किसी भी stylesheet rule को override कर देते हैं।
उदाहरण: Inline Styles Override
<style>
p { color: green; }
</style>
<p style="color: orange;">Inline style wins: orange</p>
!important Exception
!important declaration CSS specificity में एक nuclear option है। किसी property value में जोड़े जाने पर, यह standard specificity score calculation को पूरी तरह bypass कर देता है, browser को उस rule को लागू करने पर मजबूर करते हुए।
उदाहरण: The !important Exception
<style>
p { color: green !important; }
</style>
<p style="color: orange;">!important beats even the inline style: green</p>
- global styling के लिए standard HTML element tags इस्तेमाल करना और फिर conflicts resolve करने के लिए !important इस्तेमाल करना।
- लंबे, compound selectors लिखना (जैसे, body div.container ul li.item a) जो अनावश्यक रूप से specificity scores बढ़ाते हैं।
- reusable classes इस्तेमाल करने के बजाय group components को style करने के लिए ID selectors इस्तेमाल करना।
- Browser CSS Specificity तय करने और styling conflicts resolve करने के लिए एक चार-हिस्सों वाला score calculation इस्तेमाल करता है।
- Inline styles और IDs सबसे ज़्यादा specificity scores रखते हैं, आसानी से classes और element selectors को override करते हुए।
- style overrides को force करने के लिए !important इस्तेमाल करने से बचें, क्योंकि यह stylesheet maintenance और troubleshooting को complicated बना देता है।
Standard CSS specificity scoring rules और !important declarations हर web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline