CSS Custom Properties
In this page:
:root {
--custom-property: value;
}
selector {
property: var(--custom-property);
}
Variables बनाम Preprocessor Variables
preprocessor variables (जैसे Sass या Less) के उलट जो browser तक पहुँचने से पहले static CSS में compile और hardened हो जाते हैं, native CSS custom properties dynamic होते हैं। वे browser के live DOM के अंदर रहते हैं, यानी आप उन्हें real-time में read, update, और manipulate कर सकते हैं।
- native, dynamic page overrides को natively support करने के लिए preprocessor variables के बजाय native CSS variables इस्तेमाल करें।
- Accepted values:
- name: <value> — एक custom property declare करता है
- var(--name) — property पढ़ता है
- var(--name, fallback) — एक fallback value के साथ पढ़ता है
- initial — किसी custom property को guaranteed-invalid पर reset करता है
उदाहरण: Variables vs. Preprocessor Variables
<style>
:root {
--main-color: teal;
}
p {
color: var(--main-color);
}
</style>
<p>A live, native CSS variable, not a compiled preprocessor one</p>
JavaScript के साथ Dynamic Updates
क्योंकि native CSS variables DOM का हिस्सा हैं, आप simple JavaScript scripts इस्तेमाल करके real-time में आसानी से उनकी values read, modify, और update कर सकते हैं, rich, interactive user settings बनाने देते हुए।
- अपने user interaction handlers के अंदर अपनी CSS variables को dynamically update करने के लिए JavaScript इस्तेमाल करें।
- Accepted values:
- element.style.setProperty('--name', value) — इसे JavaScript से set करता है
- getComputedStyle(el).getPropertyValue('--name') — इसे पढ़ता है
- style.removeProperty('--name') — इसे हटाता है
- var(--name) — CSS बदलाव को पकड़ लेता है
उदाहरण: Dynamic Updates with JavaScript
<style>
:root {
--highlight: yellow;
}
.box {
background: var(--highlight);
}
</style>
<div class="box" onclick="document.documentElement.style.setProperty('--highlight', 'pink')">Click to update --highlight live</div>
Variable Inheritance और Cascading
CSS variables standard CSS inheritance और cascading rules follow करते हैं। जब :root के बजाय किसी specific selector के अंदर declare किए जाते हैं, तो variable local होता है और सिर्फ उस element और उसके children के लिए accessible होता है।
- अपने component styles को encapsulated और modular रखने के लिए local, scoped variables इस्तेमाल करें।
- Accepted values:
- :root पर declared --name — हर जगह available
- एक class पर declared --name — उस subtree तक scoped
- var(--name, fallback) — undefined होने पर fallback करता है
- automatically inherit होता है — custom properties default रूप से inherit होते हैं
उदाहरण: Variable Inheritance and Cascading
<style>
.card {
--card-bg: navy;
background: var(--card-bg);
}
</style>
<div class="card">Local variable, scoped to this element and its children</div>
Standard Layout Theming Systems
CSS variables dynamic website theme changers बनाना आसान बना देते हैं। अपने page color variables को :root के अंदर define करके और उन्हें एक custom class (जैसे '.dark-mode') के तहत update करके, आप themes को instantly toggle कर सकते हैं।
- themes को साफ़ तरीके से toggle करने के लिए custom data attributes को variables के साथ combine करें।
- Accepted values:
- name: <value> :root पर — global tokens
- [data-theme="dark"] { --name: ... } — theme override
- var(--name) — consumption
- var(--name, fallback) — safe default
उदाहरण: Standard Layout Theming Systems
<style>
:root {
--bg: white;
}
.dark-mode {
--bg: black;
background: var(--bg);
color: white;
}
</style>
<div class="dark-mode">Theme toggled by swapping one variable</div>
Dark Mode Schemas Design करना
आप prefers-color-scheme media query इस्तेमाल करके अपने dark mode theme को user की system preferences के आधार पर automatically activate होने के लिए design कर सकते हैं, query के अंदर अपने color variables को साफ़ तरीके से update करते हुए।
- automated dark mode themes आसानी से बनाने के लिए prefers-color-scheme media queries को अपनी CSS variables के साथ combine करें।
- Accepted values:
- :root { --bg: ...; } — light tokens
- @media (prefers-color-scheme: dark) — dark tokens
- [data-theme="dark"] — manual override
- color-scheme: light dark — native controls follow करते हैं
उदाहरण: Designing Dark Mode Schemas
<style>
:root {
--bg: white;
--text: black;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: black;
--text: white;
}
}
body {
background: var(--bg);
color: var(--text);
}
</style>
<p>Activates automatically from system preferences</p>
- custom CSS variables को नाम देते या call करते समय double hyphen prefix (--) लिखना भूल जाना।
- local, scoped variables को उनके parent container scope के बाहर reference करने की कोशिश करना।
- variable names की spelling गलत करना, जो case-sensitive होते हैं।
- Native CSS custom properties dynamic variables हैं जो browser के live DOM के अंदर active और editable रहते हैं।
- interactive layouts आसानी से बनाने के लिए अपने JavaScript scripts के अंदर अपनी CSS variables को dynamically read और update करें।
- automated dark mode themes साफ़ तरीके से बनाने के लिए prefers-color-scheme media queries और variables का फ़ायदा उठाएँ।
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 Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties