पहले, किसी सूची में मौजूद हर सिबलिंग पर यूनीक स्टाइल लागू करने के लिए, इनलाइन स्टाइल इंजेक्ट करने के लिए जटिल :nth-child लूप या JavaScript की ज़रूरत होती थी. मॉडर्न सीएसएस, sibling-index() और sibling-count() की सुविधा देता है. इससे, सीधे अपनी स्टाइलशीट में ये कैलकुलेशन किए जा सकते हैं. इससे डाइनैमिक लेआउट और कलर सिस्टम चालू हो जाते हैं. ये एलिमेंट जोड़े या हटाए जाने पर, अपने-आप अडजस्ट हो जाते हैं.
डाइनैमिक कलर सिस्टम
एक ही पैरेंट एलिमेंट के चाइल्ड एलिमेंट के ग्रुप के लिए, कलर स्पेक्ट्रम बनाया जा सकता है. इसके लिए, हर चाइल्ड एलिमेंट के लिए यूनीक ह्यू या लाइटनेस वैल्यू का हिसाब लगाना होगा. इससे यह पक्का होता है कि आइटम की संख्या चाहे जो भी हो, ग्रेडिएंट इफ़ेक्ट एक जैसा रहे.
.swatch {
/* Calculate hue by dividing the full 360deg circle by total siblings */
/* and multiplying by the current element's 1-based index */
background-color: hsl(
calc(360deg / sibling-count() * sibling-index()),
70%,
50%
);
}
सिमेट्रिकल लेआउट और फ़ैन इफ़ेक्ट
सिमेट्रिकल इफ़ेक्ट (जैसे, "फ़ैन" या आइटम को बीच में रखना) बनाने के लिए, सूची के बीच का पॉइंट ढूंढने के लिए कुल संख्या का इस्तेमाल करें.
.card {
/* Find the center index (e.g., 3 if there are 5 siblings) */
--center: calc((sibling-count() + 1) / 2);
/* Rotate items away from the center: negative for left, positive for right */
/* center element gets 0deg rotation */
transform: rotate(calc(10deg * (sibling-index() - var(--center))));
}
सर्कुलर और मुश्किल पोज़िशनिंग
इन फ़ंक्शन को सीएसएस ट्रिग्नोमेट्री (sin(), cos()) के साथ जोड़कर, एलिमेंट को बिना किसी मैन्युअल कोऑर्डिनेट के एक सटीक सर्कल में रखा जा सकता है.
.orb {
/* Calculate the angle for this item's position on a 360deg circle */
--angle: calc(360deg / sibling-count() * sibling-index());
--radius: 150px;
/* Set the pre-transformed position for all items to be centered */
position: absolute;
place-self: center;
/* Position each element around the parent center */
transform: translate(
calc(cos(var(--angle)) * var(--radius)),
calc(sin(var(--angle)) * var(--radius))
);
}
फ़ॉलबैक रणनीतियां
अगर sibling-index() और sibling-count() काम नहीं करते हैं, तो JavaScript के ज़रिए सीएसएस की कस्टम प्रॉपर्टी इंजेक्ट करके फ़ॉलबैक उपलब्ध कराएं. ज़रूरी: CSS.supports() के साथ सुविधा की पहचान करने की सुविधा का इस्तेमाल करें, ताकि यह पक्का किया जा सके कि स्क्रिप्ट सिर्फ़ ज़रूरत पड़ने पर ही चले.
/* MANDATORY: Check for native support before applying fallback */
if (!CSS.supports('top: calc(sibling-index() * 1px)')) {
const items = document.querySelectorAll('.item');
items.forEach((item, index) => {
/* MANDATORY: Injected index must be 1-based to match native function */
item.style.setProperty('--sibling-index', index + 1);
item.style.setProperty('--sibling-count', items.length);
});
}
अपनी सीएसएस में, इन वैरिएबल को बेस के तौर पर इस्तेमाल करें. साथ ही, @supports ब्लॉक में नेटिव फ़ंक्शन का इस्तेमाल करके, इन्हें बदलें. ज़रूरी: आपको नेटिव फ़ंक्शन ओवरराइड को @supports में रैप करना होगा. इससे यह पक्का किया जा सकेगा कि वैरिएबल, पुराने ब्राउज़र में काम करते रहें.
.item {
/* 1. Set base values using variables (from JS fallback) */
--index: var(--sibling-index);
--count: var(--sibling-count);
/* 2. Use the computed variables - replace this with your implementation-specific styles */
background-color: hsl(calc(360deg / var(--count) * var(--index)), 70%, 50%);
}
@supports (top: calc(sibling-index() * 1px)) {
.item {
/* 3. Override with native functions ONLY if supported */
--index: sibling-index();
--count: sibling-count();
}
}