← Back to CSS Course | Chapter 9: Modern CSS Features | Lesson 13 of 25

CSS @property

एक plain sticky note की कल्पना करें जो बिना किसी context के सिर्फ 10 कहता है -- क्या यह dollars है, degrees है, या कोई page number? अब एक smart label maker की कल्पना करें जो note पर सीधे 'THIS IS A NUMBER, DEFAULT 0, DO NOT INHERIT' stamp कर देता है। @property CSS custom properties के लिए वही label maker है: यह किसी --variable को untyped text छोड़ने के बजाय एक असली, enforced type देता है।
Syntax
css
@property --property-name {
  syntax: "<type>";
  inherits: true | false;
  initial-value: value;
}

Plain Custom Properties के साथ समस्या

var() से declared एक plain --custom-property में बिल्कुल कोई type checking नहीं होती -- browser इसकी value को use-time पर substitute की गई एक opaque string की तरह treat करता है, जिसका मतलब है इसे smoothly animate नहीं किया जा सकता (browser को नहीं पता कि दो arbitrary strings के बीच कैसे interpolate करना है) और typos या invalid values चुपचाप fail हो जाती हैं।

Note:
  • Accepted values:
  • name: <value> — कोई भी token stream accept करता है
  • var(--name) — use time पर as-is substitute होता है
  • var(--name, fallback) — defined न होने पर fallback
  • transition: --name — एक unregistered property animate होने के बजाय अचानक switch होती है

उदाहरण: The Problem With Plain Custom Properties

css
<style>
.box { padding: 20px; color: white; background: var(--my-color); }
.box:hover { --my-color: blue; }
:root {
  --my-color: red;
}
.box {
  transition: --my-color 0.3s;
}
</style>
<div class="box">Hover: the color snaps, it does not animate</div>

@property से एक Typed Property Register करना

@property किसी custom property name को एक explicit syntax (उसका type, जैसे <length> या <color>), यह inherit होता है या नहीं, और एक initial-value के साथ register करता है -- जो पहले एक opaque string थी उसे एक असली, browser-understood typed value में बदलते हुए।

Note:
  • Accepted values:
  • syntax: '<color>' — ज़रूरी type descriptor
  • inherits: true | false — ज़रूरी inheritance flag
  • initial-value: <value> — non-universal types के लिए ज़रूरी
  • @property --name — rule name को दो dashes से शुरू होना चाहिए

उदाहरण: Registering a Typed Property With @property

css
<style>
@property --my-color {
  syntax: '<color>';
  inherits: false;
  initial-value: red;
}
.box { padding: 20px; color: white; background: var(--my-color); transition: --my-color 0.5s; }
.box:hover { --my-color: blue; }
@property --my-color {
  syntax: '<color>';
  inherits: false;
  initial-value: red;
}
</style>
<div class="box">Registered typed property</div>

syntax Descriptor

syntax descriptor <color>, <length>, <percentage>, <number>, या <angle> जैसे predefined syntax component names इस्तेमाल करके accepted value type declare करता है।

property को assign की गई एक invalid value reject कर दी जाती है और उसके बजाय initial-value इस्तेमाल होती है, plain custom properties के उलट जो कुछ भी accept करती हैं।

Note:
  • Accepted values:
  • '<color>' — कोई भी CSS color
  • '<length>' — px, rem, em वगैरह
  • '<percentage>' — जैसे 50%
  • '<number>' | '<integer>' — unitless numbers, सिर्फ whole numbers
  • '<angle>' — deg, rad, turn
  • '<length-percentage>' — एक length या एक percentage
  • '<color> | none' — | से अलग किए गए alternatives
  • '<length>+' | '<length>#' — space या comma separated lists
  • '*' — कोई भी value (universal)

उदाहरण: The syntax Descriptor

css
<style>
@property --gap-size {
  syntax: '<length>';
  inherits: false;
  initial-value: 0px;
}
.box { display: flex; gap: var(--gap-size); transition: --gap-size 0.4s; }
.box:hover { --gap-size: 30px; }
.box { background: #cfe8ff; border: 2px solid #1e6fd9; padding: 10px; }
@property --gap-size {
  syntax: '<length>';
  inherits: false;
  initial-value: 0px;
}
</style>
<div class="box">Length-typed custom property</div>

अब इसे Smoothly Animate किया जा सकता है

क्योंकि एक registered property की एक असली, interpolatable type है, browser अब इसे transition या @keyframes से smoothly animate कर सकता है -- यही @property का primary practical payoff है, ऐसी animations unlock करते हुए जो एक plain untyped custom property के साथ असंभव थीं।

Note:
  • Accepted values:
  • transition: --my-color 0.3s — एक registered property animate करता है
  • registered property पर animation — type के हिसाब से interpolate करता है
  • <color> — color values को interpolate करता है
  • <length> | <angle> | <number> — numerically interpolate करते हैं
  • syntax: '*' — interpolate नहीं किया जा सकता

उदाहरण: Now It Can Be Smoothly Animated

css
<style>
@property --angle {
  syntax: '<angle>';
  inherits: false;
  initial-value: 0deg;
}
.box {
  background: conic-gradient(red var(--angle), blue);
  transition: --angle 0.3s;
}
.box:hover {
  --angle: 180deg;
}
</style>
<div class="box">Now smoothly animatable</div>

inherits और initial-value Descriptors

inherits (true/false) explicitly control करता है कि property children तक जाती है या नहीं, और initial-value वह fallback set करता है जो इस्तेमाल होता है जब भी property unset हो या उसे एक invalid value assign की गई हो।

दोनों descriptors एक valid @property rule के लिए ज़रूरी हैं, var() में plain custom property fallbacks की optional nature के उलट।

Note:
  • Accepted values:
  • inherits: true — children value inherit करते हैं
  • inherits: false — children को initial value मिलती है
  • initial-value: <value> — property set न होने पर इस्तेमाल होने वाली value
  • syntax: '*' — initial-value छोड़ी जा सकती है

उदाहरण: The inherits and initial-value Descriptors

css
<style>
.parent { --my-size: 24px; }
.child { font-size: var(--my-size); background: #cfe8ff; border: 2px solid #1e6fd9; padding: 10px; }
@property --my-size {
  syntax: '<length>';
  inherits: true;
  initial-value: 16px;
}
</style>
<div class="parent"><div class="child">Child inherits --my-size</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. syntax descriptor भूल जाना, इसलिए property register नहीं होती।
  2. inherits वाली किसी property में initial-value missing होना, इसलिए rule invalid है।
  3. बिना @property से register किए किसी custom property के animate होने की उम्मीद करना।
ब्राउज़र सपोर्ट

@property एक नयी feature है: Chrome 85+, Safari 16.4+ और Firefox 128+ में supported है।

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.