CSS Variables
In this page:
:root {
--variable-name: value;
}
selector {
property: var(--variable-name, fallback);
}
:root में Variables Declare करना
अपने variables को अपनी stylesheets में globally available बनाने के लिए, आप उन्हें ':root' selector के अंदर declare करते हैं। यह selector document structure में सबसे high-level element को represent करता है, variables को बाकी सभी selectors के लिए accessible बनाते हुए।
उदाहरण: Declaring Variables in :root
<style>
:root {
--brand-color: teal;
}
</style>
<p>Variable declared globally in :root</p>
var() Function इस्तेमाल करना
किसी declared variable value को किसी property पर लागू करने के लिए, आप var() function इस्तेमाल करते हैं, parentheses के अंदर अपना custom variable name pass करते हुए।
उदाहरण: Using the var() Function
<style>
:root {
--brand-color: teal;
}
p {
color: var(--brand-color);
}
</style>
<p>Uses the variable via var()</p>
Custom Property Fallback Values
var() function आपको एक second argument के रूप में एक fallback value specify करने देता है। अगर custom variable load होने में fail हो जाए, तो browser automatically fallback value इस्तेमाल करेगा।
उदाहरण: Custom Property Fallback Values
<style>
p {
color: var(--undefined-color, gray);
}
</style>
<p>Falls back to gray since --undefined-color isn't set</p>
Variable Inheritance और Scope
Variables standard CSS inheritance और scoping rules follow करते हैं। जब :root के बजाय किसी specific selector के अंदर declare किए जाते हैं, तो variable local होता है और सिर्फ उस element और उसके children के लिए accessible होता है।
उदाहरण: Variable Inheritance and Scope
<style>
.card {
--card-color: navy;
color: var(--card-color);
}
</style>
<div class="card">Local variable, only accessible here and inside</div>
Variables के साथ Theming
CSS variables dynamic website theme changers बनाना आसान बना देते हैं। अपने page color variables को :root के अंदर define करके और उन्हें एक custom class (जैसे '.dark-mode') के तहत update करके, आप themes को instantly toggle कर सकते हैं।
उदाहरण: Theming with Variables
<style>
:root {
--bg: white;
--text: black;
}
.dark-mode {
--bg: black;
--text: white;
background: var(--bg);
color: var(--text);
}
</style>
<div class="dark-mode">Theme toggled by swapping the variables</div>
- custom CSS variables को नाम देते या call करते समय double hyphen prefix (--) लिखना भूल जाना।
- local variables को उनके parent container scope के बाहर reference करने की कोशिश करना।
- variable names की spelling गलत करना, जो case-sensitive होते हैं।
- global access के लिए high-level :root selector के अंदर global custom variables declare करें।
- var() function इस्तेमाल करके properties पर variable values लागू करें, parentheses के अंदर custom variable names pass करते हुए।
- अपने var() functions के अंदर fallback values दें, और themes को toggle करना साफ़ और आसान बनाने के लिए variable inheritance का फ़ायदा उठाएँ।
Standard CSS custom properties (variables) हर modern 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