
Staggered Motion and Dynamic Layouts with CSS sibling-index
Creating staggered entrance animations, proportional color ramps, or radial menus in CSS traditionally forced an awkward bridge between markup and stylesheets. Because CSS lacked a native mathematical expression for an element's position within its parent or the total number of peers beside it, developers had to inject inline custom properties like style="--index: 3; --total: 8" across every DOM node in server templates or JavaScript loops. Whenever items were inserted, filtered, or reordered dynamically, those manual indices became stale and required expensive DOM mutations to recalculate.
Defined in the CSS Values and Units Level 5 specification and supported in modern Chromium and WebKit engines by mid-2026, the tree-counting functions sibling-index() and sibling-count() solve this cleanly in pure CSS. sibling-index() evaluates to a 1-based <integer> representing the element's position among its element siblings, while sibling-count() resolves to the total number of element siblings sharing the same parent. Combined with calc(), trigonometry, and oklch() color spaces, stylesheets can express responsive stagger delays, evenly distributed hue wheels, and adaptive grid spans that recompute automatically whenever the DOM changes.
Example
/* Staggered card reveal and even hue distribution with zero inline styles */
.feature-grid > .card {
--step: calc(sibling-index() - 1);
--ratio: calc(var(--step) / max(1, sibling-count() - 1));
border-top: 4px solid oklch(0.62 0.18 calc(210deg + var(--ratio) * 120deg));
animation: card-enter 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
animation-delay: calc(var(--step) * 65ms);
}
@keyframes card-enter {
from {
opacity: 0;
translate: 0 14px;
}
to {
opacity: 1;
translate: 0 0;
}
}
Implementation
<!-- Clean semantic markup without --index or --count attributes -->
<nav class="radial-dock" aria-label="Workspace tools">
<ul class="radial-list" role="list">
<li class="radial-item"><a href="#search">Search</a></li>
<li class="radial-item"><a href="#notes">Notes</a></li>
<li class="radial-item"><a href="#layers">Layers</a></li>
<li class="radial-item"><a href="#export">Export</a></li>
<li class="radial-item"><a href="#share">Share</a></li>
</ul>
</nav>
/* ==========================================================================
Dynamic Radial & Staggered List Using sibling-index() and sibling-count()
========================================================================== */
.radial-list {
list-style: none;
margin: 0;
padding: 1rem;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 0.75rem;
}
.radial-item a {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 5.5rem;
min-height: 2.75rem;
padding: 0.5rem 1rem;
border-radius: 9999px;
font: 600 0.9375rem/1.2 system-ui, sans-serif;
text-decoration: none;
color: #0f172a;
background: #e2e8f0;
border: 2px solid transparent;
transition: transform 0.2s ease, background-color 0.2s ease;
}
.radial-item a:focus-visible {
outline: 3px solid #1d4ed8;
outline-offset: 2px;
}
/* Progressive enhancement for browsers supporting CSS Values 5 tree counting */
@supports (z-index: sibling-index()) {
.radial-list {
position: relative;
display: grid;
place-items: center;
min-height: 17rem;
padding: 0;
}
.radial-item {
--index: calc(sibling-index() - 1);
--total: sibling-count();
--angle: calc((360deg / var(--total)) * var(--index) - 90deg);
--radius: 6.25rem;
grid-area: 1 / 1;
transform: translate(
calc(cos(var(--angle)) * var(--radius)),
calc(sin(var(--angle)) * var(--radius))
);
animation: dock-pop 0.35s cubic-bezier(0.16, 1, 0.3, 1) both;
animation-delay: calc(var(--index) * 55ms);
}
.radial-item a {
background: oklch(0.92 0.06 calc(var(--index) * (360deg / var(--total))));
border-color: oklch(0.65 0.14 calc(var(--index) * (360deg / var(--total))));
}
.radial-item a:hover {
transform: scale(1.06);
}
}
@keyframes dock-pop {
from {
opacity: 0;
scale: 0.85;
}
to {
opacity: 1;
scale: 1;
}
}
/* Respect user motion preferences */
@media (prefers-reduced-motion: reduce) {
.radial-item {
animation: none;
}
.radial-item a {
transition: none;
}
}
Main Point: Native CSS sibling-index and sibling-count functions derive element order and group size directly from the DOM tree, eliminating brittle inline index variables for staggered animations and proportional layouts.
Credits
Photo by celine piegay on Unsplash