در گذشته، اعمال سبکهای منحصربهفرد به هر خواهر و برادر در یک فهرست نیاز به حلقههای پیچیده :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() پشتیبانی نمیشود، با تزریق داراییهای سفارشی 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();
}
}