Waiting on the spec, and what developers can do in the meantime
CSS features move through a long pipeline of discussion, specification, prototyping, testing and shipping. The sibling-count() and sibling-index() functions have been somewhere in that pipeline for several years, and developers who want to work with tree positions today have to approximate them.
What the two functions will do
They exist because counters don't fit the job. Counters are strings, which makes them useless inside calc(); a 2017 CSSWG issue (#1026) asked for counter() support in calc() for exactly this layout use case. What's needed instead is a pair of numeric functions: one that reports an element's index among siblings, one that reports how many sibling elements exist.
The CSSWG approved the pair this year (issue #4559) and the spec text defines them as returning <integer> values:
The
sibling-count()functional notation represents, as an<integer>, the total number of child elements in the parent of the element on which the notation is used.The
sibling-index()functional notation represents, as an<integer>, the index of the element on which the notation is used among the children of its parent. Like:nth-child(),sibling-index()is 1-indexed.
Shipping isn't imminent: Adam Argyle relayed that a Chromium engineer wanted to implement both, but there was no flag to test with yet. As of March 5, 2025, Chrome has filed an intent to prototype.
Workaround one: hardcoded index and count properties
The closest match in syntax and behavior is a pair of custom properties. The problem is populating them. The most straightforward approach assigns --sibling-index with nth-child():
li:nth-child(1) {
--sibling-index: 1;
}
li:nth-child(2) {
--sibling-index: 2;
}
li:nth-child(3) {
--sibling-index: 3;
}
/* and so on... */
The count equivalent needs quantity queries built on :has(), whose shape is:
.container:has(> :last-child:nth-child(m)) { }
where m is the target count. The query tests whether the container's last element is also its nth element, which holds only when there are exactly that many. Temani Afif has a tool for generating them. Applied to the example, they look like this:
ol:has(> :nth-child(1)) {
--sibling-count: 1;
}
ol:has(> :last-child:nth-child(2)) {
--sibling-count: 2;
}
ol:has(> :last-child:nth-child(3)) {
--sibling-count: 3;
}
/* and so on... */
The sample is deliberately small; the CSS does not scale. A demo capped at 12 elements needs 24 rules just to know the index and count of each one. Sass could generate them, but the focus here is vanilla CSS.
The one real saving is nesting the --sibling-count rule inside the --sibling-index rule instead of listing both separately:
li:nth-child(2) {
--sibling-index: 2;
}
ol:has(> :last-child:nth-child(2)) {
--sibling-count: 2;
}
li:nth-child(2) {
--sibling-index: 2;
ol:has(> &:last-child) {
--sibling-count: 2;
}
}
It reads strangely to nest a parent inside its children, but it validly selects the second li and then an ol whose second li is also its last. Neither form fixes the underlying issue: 100 elements still mean 100 hardcoded declarations.
Workaround two: logarithmic rules with a factor
Roman Komarov's prototype from October covers both tree-counting functions plus the future random() function, and it grows logarithmically (base 2) instead of linearly: roughly 10 rules for 100 elements rather than 100.
Two custom properties, --si1 and --si2, both starting at 0, combine to build --sibling-index:
li {
--si1: 0;
--si2: 0;
}
The combination uses a factor F (an integer of at least 2) through the formula calc(F * var(--si2) + var(--si1)). The factor determines reach as sqrt(F) - 1:
2reaches3elements3reaches85reaches2410reaches9925reaches624
Each step up in factor pays back exponentially in reach. Here is the combined form at a factor of 3:
li {
--si1: 0;
--si2: 0;
/* factor of 3; it's a harcoded number */
--sibling-index: calc(3 * var(--si2) + var(--si1));
}
For --si1, rules select elements that are multiples of the factor and offset them by 1, up to F - 1, setting --si1 to that offset:
li:nth-child(Fn + 1) { --si1: 1; }
li:nth-child(Fn + 2) { --si1: 2; }
/* ... */
li:nth-child(Fn+(F-1)) { --si1: (F-1) }
At factor 3, that means rules up to F - 1, i.e. two:
li:nth-child(3n + 1) { --si1: 1; }
li:nth-child(3n + 2) { --si1: 2; }
For --si2, rules select elements in batches of the factor (three at a time at factor 3), working backward from the highest reachable index (here 8) until no more full batches fit:
li:nth-child(n + F*1):nth-child(-n + F*1-1){--si2: 1;}
li:nth-child(n + F*2):nth-child(-n + F*2-1){--si2: 2;}
/* ... */
li:nth-child(n+(F*(F-1))):nth-child(-n+(F*F-1)) { --si2: (F-1) }
At factor 3, that's two rules:
li:nth-child(n + 3):nth-child(-n + 5) {
--si2: 1;
}
li:nth-child(n + 6):nth-child(-n + 8) {
--si2: 2;
}
Two values of --si1 and --si2 are enough to count to 8. An interactive demo accompanies the technique: hovering a snippet shows which elements it selects, and clicking a snippet combines it into an index. Pushing the element total and factor to their maximum allows selecting 48 elements from just 14 snippets.
The count function reuses the same machinery: --sc1 and --sc2 sit on the container, also starting at 0, with the same formula.
ol {
--sc1: 0;
--sc2: 0;
/* factor of 3; also a harcoded number */
--sibling-count: calc(3 * var(--sc2) + var(--sc1));
}
Rather than writing separate selectors for the count properties, the :has() approach from the first technique can host them inside the rules where the index properties are declared:
/* --si1 and --sc1 */
li:nth-child(3n + 1) {
--si1: 1;
ol:has(> &:last-child) {
--sc1: 1;
}
}
li:nth-child(3n + 2) {
--si1: 2;
ol:has(> &:last-child) {
--sc1: 2;
}
}
/* --si2 and --sc2 */
li:nth-child(n + 3):nth-child(-n + 5) {
--si2: 1;
ol:has(> &:last-child) {
--sc2: 1;
}
}
li:nth-child(n + 6):nth-child(-n + 8) {
--si2: 2;
ol:has(> &:last-child) {
--sc2: 2;
}
}
With a factor of 3, eight elements cost four rules; at a factor of 7, 48 elements cost 14 rules. The method's opacity is its drawback — it may strike some as too magical, or simply not aesthetically pleasing.
Workaround three: JavaScript and Mutation Observer
Fewer than 20 lines and the Mutation Observer API reach the double-precision floating-point limit, 1.7976931348623157e+308, and practically "infinity." This does mean JavaScript. Where the JS version is far simpler, it's the appropriate tool; if raw performance is the priority, hardcoding in CSS or HTML remains the fastest route.
Grab the container from the DOM:
const elements = document.querySelector("ol");
Then write a function that assigns --sibling-index per element and --sibling-count on the container, where the cascade exposes it to the children. Loop over elements.children, and take the count from elements.children.length:
const updateCustomProperties = () => {
let index = 1;
for (element of elements.children) {
element.style.setProperty("--sibling-index", index);
index++;
}
elements.style.setProperty("--sibling-count", elements.children.length);
};
Call it once to establish initial values:
updateCustomProperties();
Finally, construct a MutationObserver, passing the updateCustomProperties function as the callback invoked on every change. Call observe() with the observed element plus a config object whose three booleans — attributes, childList, subtree — define what triggers it. Only child-list changes matter here, so that flag is set to true:
const observer = new MutationObserver(updateCustomProperties);
const config = {attributes: false, childList: true, subtree: false};
observer.observe(elements, config);
The technique counts many elements; the accompanying demo caps at 100, but tenfold is easy. It's overkill for most cases, but it is available today.
Switching over once the functions ship
Nobody can predict the release date, but with spec text written and Chromium moving quickly on implementation, both functions should arrive soon. Once support is acceptable, migration means pointing each custom property at its functional homonym — unless you'd rather edit every declaration by hand:
ol{
--sibling-count: sibling-count();
}
li{
--sibling-index: sibling-index();
}
Further reading: Roman Komarov's Possible Future CSS: Tree-Counting Functions and Random Values, plus Chris Coyier on View Transitions Staggering and Element Indexes. Related CSSWG issues include #9572 (a selector argument for both functions), #11068 (children-count()) and #11069 (descendant-count()).



