:nth-child()
CSS の :nth-child()
擬似クラスは、兄弟要素のグループの中での位置に基づいて選択します。
/* リスト中の2番目の <li> 要素を選択 */
li:nth-child(2) {
color: lime;
}
/* 兄弟要素の中で3つおきに
要素を選択 */
:nth-child(4n) {
color: lime;
}
構文
nth-child
擬似クラスは、引数を1つ指定し、リストの子要素を要素の位置で選択するためのパターンを記述します。要素の位置は1から始まります。
キーワード値
odd
- 一連の兄弟要素の中で奇数番目の要素 (1, 3, 5, など) を表します。
even
- 一連の兄弟要素の中で偶数番目の要素 (2, 4, 6, など) を表します。
関数表記
<An+B>
- リスト中の位置が、
An+B
で定義された数値のパターンと一致する要素を表します。
A
は整数の刻み値です。
B
は整数の加算値です。
n
はすべての正の整数で、0から始まります。 - リスト中の An+B 番目の要素として読むことができます。
形式文法
:nth-child( <nth> [ of <complex-selector-list> ]? )ここで
<nth> = <an-plus-b> | even | odd
<complex-selector-list> = <complex-selector>#ここで
<complex-selector> = <compound-selector> [ <combinator>? <compound-selector> ]*
ここで
<compound-selector> = [ <type-selector>? <subclass-selector>* [ <pseudo-element-selector> <pseudo-class-selector>* ]* ]!
<combinator> = '>' | '+' | '~' | [ '||' ]ここで
<type-selector> = <wq-name> | <ns-prefix>? '*'
<subclass-selector> = <id-selector> | <class-selector> | <attribute-selector> | <pseudo-class-selector>
<pseudo-element-selector> = ':' <pseudo-class-selector>
<pseudo-class-selector> = ':' <ident-token> | ':' <function-token> <any-value> ')'ここで
<wq-name> = <ns-prefix>? <ident-token>
<ns-prefix> = [ <ident-token> | '*' ]? |
<id-selector> = <hash-token>
<class-selector> = '.' <ident-token>
<attribute-selector> = '[' <wq-name> ']' | '[' <wq-name> <attr-matcher> [ <string-token> | <ident-token> ] <attr-modifier>? ']'ここで
<attr-matcher> = [ '~' | | | '^' | '$' | '*' ]? '='
<attr-modifier> = i | s
例
セレクターの例
tr:nth-child(odd)
又はtr:nth-child(2n+1)
- HTML テーブルの奇数行 (1, 3, 5, など) を表します。
tr:nth-child(even)
またはtr:nth-child(2n)
- HTML テーブルの偶数行 (2, 4, 6, など) を表します。
:nth-child(7)
- 7番目の要素を表します。
:nth-child(5n)
- 5番目 [=5×1], 10番目 [=5×2], 15番目 [=5×3], 等の要素を表します。最初のものは 0番目 [=5x0] が式の結果として返りますが、
n
が0から始まるのに対して添字は1から始まるので、一致するものはないという結果になります。これは最初は奇妙に見えるかもしれませんが、次の例のようにB
の部分が>0
となる場合にもっとよく分かるでしょう。 :nth-child(n+7)
- 7番目とそれ以降のすべての要素を表します。 7番目 [=0+7], 8番目 [=1+7], 9番目 [=2+7], 等です。
:nth-child(3n+4)
- 4番目 [=(3×0)+4], 7番目 [=(3×1)+4], 10番目 [=(3×2)+4], 13番目 [=(3×3)+4], 等の要素を表します。
:nth-child(-n+3)
- 兄弟要素のグループの中で最初の3つの要素を表します。 [=-0+3, -1+3, -2+3]
p:nth-child(n)
- 兄弟要素のグループの中ですべての
<p>
要素を表します。これは単純なp
セレクターと同じ要素を選択します (但し、具体度はより高くなります)。 p:nth-child(1)
またはp:nth-child(0n+1)
- 兄弟要素のグループの中で最初の
<p>
要素すべてを表します。これは:first-child
セレクターと同じです (具体度も同じです)。
p:nth-child(n+8):nth-child(-n+15)
- 兄弟要素のグループの中で8~15番目の
<p>
要素を表します。
詳細な例
HTML
<h3><code>span:nth-child(2n+1)</code>, WITHOUT an
<code><em></code> among the child elements.</h3>
<p>Children 1, 3, 5, and 7 are selected.</p>
<div class="first">
<span>Span 1!</span>
<span>Span 2</span>
<span>Span 3!</span>
<span>Span 4</span>
<span>Span 5!</span>
<span>Span 6</span>
<span>Span 7!</span>
</div>
<br>
<h3><code>span:nth-child(2n+1)</code>, WITH an
<code><em></code> among the child elements.</h3>
<p>Children 1, 5, and 7 are selected.<br>
3 is used in the counting because it is a child, but it isn't
selected because it isn't a <code><span></code>.</p>
<div class="second">
<span>Span!</span>
<span>Span</span>
<em>This is an `em`.</em>
<span>Span</span>
<span>Span!</span>
<span>Span</span>
<span>Span!</span>
<span>Span</span>
</div>
<br>
<h3><code>span:nth-of-type(2n+1)</code>, WITH an
<code><em></code> among the child elements.</h3>
<p>Children 1, 4, 6, and 8 are selected.<br>
3 isn't used in the counting or selected because it is an <code><em></code>,
not a <code><span></code>, and <code>nth-of-type</code> only selects
children of that type. The <code><em></code> is completely skipped
over and ignored.</p>
<div class="third">
<span>Span!</span>
<span>Span</span>
<em>This is an `em`.</em>
<span>Span!</span>
<span>Span</span>
<span>Span!</span>
<span>Span</span>
<span>Span!</span>
</div>
CSS
html {
font-family: sans-serif;
}
span,
div em {
padding: 5px;
border: 1px solid green;
display: inline-block;
margin-bottom: 3px;
}
.first span:nth-child(2n+1),
.second span:nth-child(2n+1),
.third span:nth-of-type(2n+1) {
background-color: lime;
}
結果
仕様書
仕様書 | 状態 | 備考 |
---|---|---|
Selectors Level 4 :nth-child の定義 |
草案 | <selector> の構文と仕様書に、親を持たない要素も一致するよう追加。 |
Selectors Level 3 :nth-child の定義 |
勧告 | 初回定義。 |
ブラウザーの互換性
BCD tables only load in the browser
このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 https://github.com/mdn/browser-compat-data をチェックアウトしてプルリクエストを送信してください。