CSS @property
In this page:
@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 हो जाती हैं।
- 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
<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 में बदलते हुए।
- 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
<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 करती हैं।
- 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
<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 के साथ असंभव थीं।
- 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
<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 के उलट।
- 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
<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>
syntaxdescriptor भूल जाना, इसलिए property register नहीं होती।inheritsवाली किसी property मेंinitial-valuemissing होना, इसलिए rule invalid है।- बिना
@propertyसे register किए किसी custom property के animate होने की उम्मीद करना।
@property एक नयी feature है: Chrome 85+, Safari 16.4+ और Firefox 128+ में supported है।
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep