في السابق، كان تطبيق أنماط فريدة على كل عنصر شقيق في قائمة يتطلّب استخدام حلقات :nth-child معقّدة أو JavaScript لإدخال أنماط مضمّنة. توفّر لغة 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 مخصّصة عبر 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);
});
}
في 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();
}
}