sibling-index()でアニメーションに時間差を付ける
sibling-index()は要素が親の中で何番目か、sibling-count()は兄弟要素の数を返すCSSの関数です。リストの項目を順番に出すアニメーションの時間差や、項目数で幅を等分するレイアウトを、番号を配らずに書けます。
- Chrome138
- Edge138
- Firefox154
- Safari26.2
はじめに
sibling-index()は要素が親の中で何番目かを、sibling-count()は自分を含めた兄弟要素がいくつあるかを、整数で返すCSSの関数です。calc()に入れれば、並び順や項目数をそのまま時間や長さの計算に使えます。Firefox 154が対応して、Baseline 2026に入りました。
リストの項目を少しずつずらしてフェードインさせるには、これまで各要素にstyle="--i: 3"のような番号を配っていました。テンプレートで番号を埋め込むか、li:nth-child(1)から項目数のぶんセレクタを書き並べるかです。項目数が決まっていて少ないときは、この書き方でも書けます。項目数が変わるリストでは、この2つの関数を使うと、番号や総数をCSSだけで計算に使えます。
2つの関数
sibling-index()は、親の中でその要素が何番目かを返します。:nth-child()と同じく1始まりです。sibling-count()は、自分自身を含めた兄弟要素の総数を返します。Element.childrenのlengthと同じ値です。
どちらも引数を取らず、整数を返します。calc()の中で他の値と掛け合わせられ、order: sibling-index()のように整数を受け取るプロパティには、calc()で包まずに書けます。
<ul class="bars">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<style>
.bars li {
height: 1rem;
margin-block: 4px;
background: royalblue;
width: calc(100% * sibling-index() / sibling-count());
4本なら25%、50%、75%、100%
}
</style>ブラウザで開くと、4本のバーが25%ずつ伸びる階段に並びます。<li>を1つ足すと5本で割り直され、20%ずつ伸びる階段になります。
数える対象
テキストノードとコメントノードは兄弟に数えないので、HTMLの改行やインデントで番号がずれることはありません。
要素なら種類は問いません。リストの中に<style>や<script>を置くと、それも兄弟として数えられ、後ろの番号がずれます。番号に入れたくない要素は、親の外に出します。
display: noneの兄弟も数に入ります。数えるのはDOMツリーでの位置で、描画されるかどうかは関係ありません。<style>と<script>、display: noneの<div>を混ぜた5つの子にorder: sibling-index()を当てると、どのブラウザでも計算値は1から5まで欠けずに並びます。slotで並びを入れ替えたShadow DOMでも、番号は:nth-child()と同じくDOMツリーの順のままです。
要素が増えたり減ったりすると、値は計算し直されます。リストの先頭に要素を差し込むと、後ろの要素のsibling-index()はすべて1つずつ増えます。
<label><input type="checkbox" id="reveal" /> 画面に出ない子要素も表示する</label>
<div class="list">
<div>りんご</div>
<div>みかん</div>
<!-- コメント -->
<div class="none">ぶどう</div>
<div>もも</div>
テキストノード
<div>なし</div>
</div>.list > * {
counter-reset: index sibling-index();
}
.list > *::before {
content: counter(index) ': ';
}
.none {
display: none;
}
body:has(#reveal:checked) .list > * {
display: block;
}
.list > :is(style, script, .none) {
color: GrayText;
}
.list > style::after {
content: '<style>';
}
.list > script::after {
content: '<script>';
}// りんごの前に style を、なしの前に script を置く
const list = document.querySelector('.list');
list.prepend(document.createElement('style'));
list.lastElementChild.before(document.createElement('script'));結果の欄では、見えている項目の番号が2、3、5、7と飛びます。チェックを入れると、1の<style>、4のdisplay: noneの項目、6の<script>が現れて番号がつながります。番号の無い行はテキストノードで、番号を使いません。
順番に応じて遅延をずらす
リストの項目を順に出すには、animation-delayをsibling-index()から計算します。
li {
animation: fade-in 320ms backwards;
animation-delay: calc(sibling-index() * 90ms);
}
@keyframes fade-in {
from {
opacity: 0;
translate: 0 8px;
}
}<label>項目数 <input type="range" id="count" min="2" max="8" value="5" /></label>
<button type="button" id="replay">もう一度再生</button>
<ol id="list"></ol>const count = document.querySelector('#count');
const list = document.querySelector('#list');
// 項目を作り直して、アニメーションを初めから動かす
function render() {
const items = Array.from({ length: count.valueAsNumber }, () => {
const item = document.createElement('li');
item.textContent = '項目';
return item;
});
list.replaceChildren(...items);
}
count.addEventListener('input', render);
document.querySelector('#replay').addEventListener('click', render);
render();結果の欄で項目数を変えると、項目が上から順に現れ直します。
これまでは、テンプレートで番号を書き出していました。
<li style="--i: 1">項目</li>
<li style="--i: 2">項目</li>
<li style="--i: 3">項目</li>sibling-index()を使えば、この--iは要りません。項目が増えたり減ったりしても、番号は並びから決まります。
兄弟の数から幅を決める
タブやツールバーの項目を横に等分するなら、100%をsibling-count()で割ります。
.tabs > * {
width: calc(100% / sibling-count());
}sibling-count()が数えるのは、関数を書いた要素の兄弟です。親に書いたgrid-template-columns: repeat(sibling-count(), 1fr)は、子の数ではなく親自身の兄弟の数で列を作ります。兄弟が2つある.tabsに子を4つ入れても、どのブラウザでも列は4つではなく3つになります。
.tabs {
grid-template-columns: repeat(sibling-count(), 1fr);
.tabs の子ではなく .tabs 自身の兄弟の数になる
}子の数を使うなら、calc()は子に書きます。
.chart > * {
width: calc(100% / sibling-count());
height: calc(100% * sibling-index() / sibling-count());
counter-reset: index sibling-index();
box-sizing: border-box;
border: 1px solid Canvas;
background-color: steelblue;
color: white;
text-align: center;
transition:
width 0.3s,
height 0.3s;
}
.chart > *::before {
content: counter(index);
}
.chart {
display: flex;
align-items: end;
height: 10rem;
margin-top: 1rem;
}<button type="button" id="prepend">先頭に追加</button>
<button type="button" id="append">末尾に追加</button>
<button type="button" id="remove">先頭を削除</button>
<div class="chart">
<div></div>
<div></div>
<div></div>
<div></div>
</div>const chart = document.querySelector('.chart');
document.querySelector('#prepend').addEventListener('click', () => {
chart.prepend(document.createElement('div'));
});
document.querySelector('#append').addEventListener('click', () => {
chart.append(document.createElement('div'));
});
document.querySelector('#remove').addEventListener('click', () => {
if (chart.children.length > 1) {
chart.firstElementChild.remove();
}
});結果の欄でバーを足したり消したりすると、幅が数で割り直されます。先頭に足すと、後ろのバーの番号が1つずつ増えて高くなります。
カスタムプロパティ経由で子孫に渡す
計算した番号をカスタムプロパティに入れて子孫で使うときは、@propertyで登録しておかないと、思った値になりません。
li {
--i: sibling-index();
}
li span {
width: calc(var(--i) * 10px);
li の番号ではなく span 自身の番号で解決される
}登録していないカスタムプロパティは、値を書いたままの形で持ち、var()で読んだ場所で展開されます。そのためsibling-index()はliではなくspanの位置で計算され、spanが1つだけの子なら、どのliの中でも1になります。どのブラウザでも、3つのliの中のspanの幅はどれも10pxで、getComputedStyle()で読んだliの--iはsibling-index()という文字列のままです。
@propertyで登録すると、宣言した要素で整数に決まってから継承されます。spanの幅は10px、20px、30pxになります。
@property --i {
syntax: '<integer>';
inherits: true;
initial-value: 0;
}
li {
--i: sibling-index();
}
li span {
width: calc(var(--i) * 10px);
li の番号がそのまま降りてくる
}同じ要素の中で使うだけなら、登録しなくても結果は変わりません。子孫に値を渡すときだけ、@propertyで登録します。
おわりに
sibling-index()とsibling-count()を使うと、テンプレートで配っていた番号や項目数を、CSSの値として計算に使えます。子孫に番号を渡すときは@propertyで登録し、子の数で幅を決めるときはsibling-count()を子に書きます。