CSS Counters
In this page:
body {
counter-reset: counter-name;
}
selector::before {
counter-increment: counter-name;
content: counter(counter-name);
}
counter-reset से Counters Initialize करना
एक CSS counter शुरू करने के लिए, आपको एक parent element पर counter-reset property इस्तेमाल करके इसकी value initialize करनी होगी, जो आपके counter variable की starting value set करती है (default रूप से zero)। counter-set counter-reset जैसा ही काम करता है लेकिन अगर scope में पहले से कोई counter मौजूद न हो तो एक नया नहीं बनाता -- यह सिर्फ किसी existing counter की value set करता है, जो किसी list के बीच में किसी counter की value को उसे parent से restart किए बिना reset करने के लिए उपयोगी बनाता है।
- अपने counter-reset properties को उस list के parent container पर declare करें जिसे आप count करना चाहते हैं।
- Accepted values:
- counter-reset — नाम और optional start value, जैसे section 0
- counter-set — counter-reset जैसा लेकिन एक existing counter की value set करता है
- none — कोई counter change नहीं (initial value)
- integer — negative हो सकता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Initializing Counters with counter-reset
<style>
ol {
counter-reset: item;
}
</style>
<ol><li>First</li></ol>
counter-increment से Counters को Increment करना
counter-increment property browser को आपके custom counter variable की value बढ़ाने के लिए बताती है। हर बार जब browser targeted element से टकराता है, यह counter को ऊपर tick करता है (default रूप से 1 से)।
- steps में count करने के लिए आप एक custom increment value specify कर सकते हैं (जैसे counter-increment: chapter 2)।
- Accepted values:
- counter-increment — नाम और optional step, जैसे section 1
- counter-reset — नाम और optional start value, जैसे section 0
- none — कोई counter change नहीं (initial value)
- integer — negative हो सकता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Incrementing Counters with counter-increment
<style>
ol {
counter-reset: item;
}
li {
counter-increment: item;
}
</style>
<ol>
<li>Ticks up by 1 each time</li>
<li>Ticks up by 1 each time</li>
</ol>
Content के साथ Counters Render करना
अपने counter value को अपनी webpage पर दिखाने के लिए, आप एक CSS pseudo-element (::before या ::after) की content property के अंदर counter() function इस्तेमाल करते हैं, जो browser को calculated numbers automatically render करने देता है।
उदाहरण: Rendering Counters with Content
<style>
ol {
counter-reset: item;
list-style: none;
}
li {
counter-increment: item;
}
li::before {
content: "Chapter " counter(item) ": ";
}
</style>
<ol>
<li>Introduction</li>
<li>Basics</li>
</ol>
Nested Counters को Handle करना
आप complex, multi-level outlines (जैसे section '1.1' या '1.1.1') handle करने के लिए counters को nest कर सकते हैं। counters() function (आख़िर में एक s के साथ) इस्तेमाल करके, आप browser को nested counters को automatically dots से अलग करके render करने का instruction दे सकते हैं।
उदाहरण: Handling Nested Counters
<style>
ol {
counter-reset: section;
list-style: none;
}
li {
counter-increment: section;
}
li::before {
content: counters(section, ".") " ";
}
</style>
<ol>
<li>Chapter
<ol><li>Nested section</li></ol>
</li>
</ol>
Counter Styles को Customize करना
आप अपने counter() function को एक style parameter (जैसे upper-roman या lower-alpha) pass करके यह customize कर सकते हैं कि आपके counter numbers कैसे display होते हैं, जिससे आप Roman numerals या alphabetical sequences आसानी से render कर सकते हैं।
- अपनी lists को पढ़ने और follow करने में आसान बनाने के लिए sub-steps के लिए lowercase letters इस्तेमाल करें।
- Accepted values:
- decimal — 1, 2, 3 (initial value)
- lower-alpha / upper-alpha — a, b, c या A, B, C
- lower-roman / upper-roman — i, ii, iii या I, II, III
- disc / circle / square — bullets
- none — कोई marker नहीं
उदाहरण: Customizing Counter Styles
<style>
ol {
counter-reset: item;
list-style: none;
}
li {
counter-increment: item;
}
li::before {
content: counter(item, upper-roman) ". ";
}
</style>
<ol>
<li>First</li>
<li>Second</li>
</ol>
- counter-reset इस्तेमाल करके किसी parent element पर अपनी counter values initialize करना भूल जाना।
- ज़रूरी ::before या ::after pseudo-elements इस्तेमाल किए बिना elements के अंदर सीधे counter values दिखाने की कोशिश करना।
- nested lists पर simple counter() function इस्तेमाल करना जबकि sub-level numbering दिखाने के लिए counters() चाहिए होगा।
- CSS counters stylesheets को page elements को automatically count करने और sequence numbers को साफ़ तरीके से दिखाने देते हैं।
- counter-reset इस्तेमाल करके किसी parent element पर अपना counter initialize करें, और child elements पर counter-increment इस्तेमाल करके value को ऊपर tick करें।
- ::before या ::after pseudo-elements की content property के अंदर counter() या counters() functions इस्तेमाल करके अपने counter values दिखाएँ।
Standard CSS counters और nested list count functions हर 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