CSS At-Rules
In this page:
@media (condition) { }
@import url("file.css");
@font-face { }
@keyframes name { }
@supports (property: value) { }
@charset से File Encoding Declare करना
@charset browser को बताता है कि कोई stylesheet file कौन-सी character encoding इस्तेमाल करती है, जो तब मायने रखता है जब आपकी CSS में content: strings के अंदर curly quotes या accented letters जैसे non-ASCII characters हों।
यह file के literal पहले characters होने चाहिए -- यहाँ तक कि कोई comment भी इसके पहले नहीं आ सकता -- वरना browser इसे चुपचाप नज़रअंदाज़ कर देता है।
- अपनी CSS file को खुद UTF-8 के रूप में save करें और @charset "UTF-8"; declare करें; साथ में, ताकि declared encoding हमेशा file के असली bytes से match करे।
- Accepted values:
- @charset "UTF-8"; — एकमात्र widely supported encoding
- पहला होना चाहिए — इससे पहले कुछ भी नहीं आ सकता, यहाँ तक कि कोई comment भी नहीं
- double quotes — ज़रूरी, single quotes valid नहीं हैं
- exactly one — सिर्फ पहले वाले का सम्मान किया जाता है
उदाहरण: Declaring File Encoding with @charset
<style>
p { font-size: 1.2rem; }
@charset "UTF-8";
p::before {
content: "“";
}
</style>
<p>The opening quote before this text comes from CSS.</p>
@namespace से XML Namespaces को नाम देना
@namespace मुख्य रूप से SVG या XHTML जैसे XML-based documents में एक CSS prefix को एक XML namespace URI से जोड़ने के लिए इस्तेमाल होता है, svg|circle जैसे selectors को सिर्फ एक specific namespace से elements target करने देते हुए। plain HTML documents में इसकी शायद ही कभी ज़रूरत पड़ती है, क्योंकि HTML का सिर्फ एक implicit namespace है।
- @namespace की तरफ़ मुख्य रूप से तब जाएँ जब inline SVG elements को style कर रहे हों जो standard SVG markup के साथ custom XML namespaces मिलाते हैं।
- Accepted values:
- @namespace url(<uri>); — default namespace set करता है
- @namespace <prefix> url(<uri>); — एक prefix declare करता है
- prefix|element — एक prefix इस्तेमाल करने वाला selector
- |element — कोई भी namespace
उदाहरण: Naming XML Namespaces with @namespace
<style>
@namespace svg url(http://www.w3.org/2000/svg);
svg|circle {
fill: red;
}
</style>
<!-- @namespace svg targets SVG elements; it must precede other rules -->
<svg width="120" height="120"><circle cx="60" cy="60" r="50" /></svg>
@scope से Rule Scope को Limit करना
@scope CSS rules के एक block को wrap करता है ताकि उनके selectors सिर्फ एक specific DOM subtree के अंदर के elements को match करें, एक scoping root और एक optional scoping limit से defined।
यह एक बहुत पुरानी समस्या solve करता है: हर एक selector को एक unique class name से prefix किए बिना किसी rule को सिर्फ "इस card के अंदर" लागू करना।
- component-style CSS के लिए @scope इस्तेमाल करें जहाँ आप चाहते हैं कि "p { color: red; }" जैसा एक rule सिर्फ किसी specific card के अंदर लागू हो, पूरे page में globally नहीं।
- Accepted values:
- @scope (<root>) — styles root के अंदर लागू होते हैं
- @scope (<root>) to (<limit>) — एक lower boundary वाला donut scope
- :scope — scope root को refer करता है
- proximity — specificity ties पर सबसे नज़दीकी scope जीतता है
उदाहरण: Limiting Rule Scope with @scope
<style>
.card { border: 2px solid #1e6fd9; padding: 8px; }
@scope (.card) {
p {
color: red;
}
}
</style>
<div class="card"><p>Inside @scope: red</p></div>
<p>Outside the scope: unaffected</p>
@starting-style से Entrances Animate करना
@starting-style वे "before" values define करता है जिनसे एक transition animate होता है जब कोई element जो पहले display: none था (या नया DOM में जोड़ा गया था) पहली बार visible होता है।
नार्मल रूप से CSS transitions किसी element की बिल्कुल पहली appearance को animate नहीं कर सकते, क्योंकि transition करने के लिए कोई prior state नहीं होती -- @starting-style वह missing starting point provide करता है।
- display जैसी properties को animate करते समय @starting-style को transition-behavior: allow-discrete के साथ जोड़ें, ताकि entrance transition की एक defined starting point हो।
- Accepted values:
- @starting-style { ... } — पहले render से पहले की style define करता है
- transition: opacity 0.3s — starting style से animate करता है
- display: none से block — allow-discrete transitions के साथ काम करता है
उदाहरण: Animating Entrances with @starting-style
<style>
.box { padding: 20px; background: #cfe8ff; border: 2px solid #1e6fd9; }
@starting-style {
.box {
opacity: 0;
}
}
.box {
opacity: 1;
transition: opacity 0.3s;
}
</style>
<div class="box">Fades in when inserted</div>
@counter-style से Custom Counters बनाना
@counter-style आपको built-in list (decimal, roman, disc, वगैरह) से आगे एक पूरी तरह custom counter symbol system define करने देता है -- उदाहरण के लिए, custom bullet glyphs, एक specific prefix और suffix, या एक repeating custom symbol set।
यह ordered और unordered lists को styling control का एक level देता है जिस तक अकेले list-style-type नहीं पहुँच सकता।
- अपने @counter-style को एक descriptive नाम दें और sitewide consistent custom bullets के लिए list-style-type के ज़रिए इसे कई lists में reuse करें।
- Accepted values:
- system: cyclic | numeric | alphabetic | symbolic | additive | fixed | extends — counters कैसे generate होते हैं
- symbols: <symbol> — इस्तेमाल होने वाले markers
- suffix: <string> — marker के बाद का text
- prefix: <string> — marker से पहले का text
- range: <lower> <upper> | auto — counter range
उदाहरण: Building Custom Counters with @counter-style
<style>
@counter-style thumbs {
system: cyclic;
symbols: "👍";
suffix: " ";
}
ul {
list-style: thumbs;
}
</style>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
- @charset को पहली stylesheet की बिल्कुल पहली line के अलावा कहीं और रखना, जहाँ browsers को इसे बिल्कुल भी पहचानने के लिए यहीं चाहिए होता है।
- @scope इस्तेमाल करना और यह मान लेना कि यह किसी CSS class-based namespace जैसा behave करता है, जबकि यह असल में सीमित करता है कि कोई rule के selectors DOM position के आधार पर किन elements को match कर सकते हैं।
- यह भूल जाना कि @starting-style सिर्फ तभी किसी transition पर लागू होता है जब element भी display को none से बदल रहा हो, हर entrance transition पर नहीं।
- @charset किसी stylesheet की text encoding declare करता है और लागू होने के लिए file में बिल्कुल पहली चीज़ होनी चाहिए।
- @scope CSS rules के एक block को एक specific DOM subtree तक सीमित करता है, और @counter-style built-in list से आगे पूरी तरह custom counter symbols define करता है।
- @starting-style CSS transitions के लिए "before" state define करता है जो किसी element को DOM में पहली बार दिखने पर animate करते हैं।
@charset, @namespace, और @counter-style broadly supported हैं; @scope और @starting-style नए additions हैं जो current Chromium और Firefox releases में supported हैं, Safari catch up कर रहा है।
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