ঐতিহাসিকভাবে, তালিকার প্রতিটি সিবলিংয়ে অনন্য স্টাইল প্রয়োগ করতে জটিল :nth-child লুপ বা ইনলাইন স্টাইল ইনজেক্ট করার জন্য জাভাস্ক্রিপ্টের প্রয়োজন হতো। আধুনিক CSS আপনার স্টাইলশিটে সরাসরি এইসব গণনা করার জন্য 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))));
}
বৃত্তাকার ও জটিল পজিশনিং
CSS ত্রিকোণমিতির (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 কাস্টম প্রপার্টি ইনজেক্ট করে একটি ফলব্যাক প্রদান করুন। বাধ্যতামূলক: স্ক্রিপ্ট শুধুমাত্র প্রয়োজন হলেই যাতে রান করে তা নিশ্চিত করতে 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);
});
}
আপনার CSS-এ, এই ভেরিয়েবলগুলি বেস হিসেবে ব্যবহার করুন এবং @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();
}
}