CKTIS / CSS
counters() — nested counter path
`counters()` formats every counter of the same name from outermost to innermost and joins their representations with a supplied separator.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
counters( <counter-name>, <string>, <counter-style>? )counters() formats every counter of the same name from outermost to innermost and joins their representations with a supplied separator.
The example creates outer value 2 and inner value 4. counters(section, ".") paints three glyphs corresponding to 2.4, whereas counter(section) shows only innermost 4; measured widths verify the hierarchy.
Grammar: counters( <counter-name>, <string>, <counter-style>? ).
The separator is a required string and the optional style defaults to decimal. Ordering is always outermost first, making the function suitable for section numbers. A missing counter is instantiated at 0; an accidental counter-reset on a descendant adds another level and can unexpectedly lengthen the path.
Examples
const style = document.createElement('style');
style.textContent = '.outer{counter-reset:section 2}.inner{counter-reset:section 4}.path,.leaf{display:inline-block;font:20px monospace}.path::before{content:counters(section, ".")}.leaf::before{content:counter(section)}';
const outer = document.createElement('div'); outer.className = 'outer';
const inner = document.createElement('div'); inner.className = 'inner';
const path = document.createElement('span'); path.className = 'path';
const leaf = document.createElement('span'); leaf.className = 'leaf';
document.head.append(style); inner.append(path, leaf); outer.append(inner); document.body.append(outer);
const pathWidth = path.getBoundingClientRect().width;
const leafWidth = leaf.getBoundingClientRect().width;
console.log('path-glyphs=' + Math.round(pathWidth / leafWidth) + '; leaf-glyphs=1; width-ratio=' + (pathWidth / leafWidth).toFixed(2));Result
path-glyphs=3; leaf-glyphs=1; width-ratio=3.00
`counters()` formats every counter of the same name from outermost to innermost and joins their representations with a supplied separator.