CKTIS / CSS
counters() — pot ugnezdenih števcev
`counters()` oblikuje vse istoimenske števce od zunanjega do notranjega in njihove prikaze združi s podanim ločilom.
Različica: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Sintaksa
counters( <counter-name>, <string>, <counter-style>? )counters() oblikuje vse istoimenske števce od zunanjega do notranjega in njihove prikaze združi s podanim ločilom.
Primer ustvari zunanjo vrednost 2 in notranjo 4. counters(section, ".") nariše tri znake za 2.4, medtem ko counter(section) pokaže samo notranjo vrednost 4; izmerjene širine potrdijo hierarhijo.
Slovnica: counters( <counter-name>, <string>, <counter-style>? ).
Ločilo je obvezen niz, neobvezni slog pa je privzeto decimal. Vrstni red vedno poteka od zunaj navznoter, zato je funkcija primerna za številke razdelkov. Manjkajoči števec se ustvari z 0; naključni counter-reset na potomcu doda raven in lahko pot nepričakovano podaljša.
Primeri
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));Rezultat
path-glyphs=3; leaf-glyphs=1; width-ratio=3.00
`counters()` oblikuje vse istoimenske števce od zunanjega do notranjega in njihove prikaze združi s podanim ločilom.