:nth-child
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015ë 7ì.
* Some parts of this feature may have varying levels of support.
CSS :nth-child() ìì¬ í´ëì¤ë íì ì¬ì´ììì ììì ë°ë¼ ìì를 ì íí©ëë¤.
css
/* 목ë¡ì ë ë²ì§¸ <li> ì í */
li:nth-child(2) {
color: lime;
}
/* ììì 그룹ìì ë¤ ë²ì§¸ì ìì¹íë 모ë ìì ì í */
:nth-child(4n) {
color: lime;
}
구문
nth-child ìì¬ í´ëì¤ë íì ì 목ë¡ìì, ì ííë ¤ë ììì ì¸ë±ì¤ í¨í´ì ëíë´ë íëì 매ê°ë³ì를 ì¬ì©í´ ì§ì í©ëë¤. ì¸ë±ì¤ë 1ë¶í° ììí©ëë¤.
í¤ìë ê°
í¨ìí í기ë²
-
<An+B>- : ì¬ì©ì ì§ì í¨í´ì ë§ì¡±í ì¸ë±ì¤ë¥¼ ê°ì§ë ìì를 ëíë
ëë¤. An+Bì ííë¡ ëíë´ë©°,
Aë ì ì ì¸ë±ì¤ ì¦ê°ë,Bë ì ì ì¤íì ,nì 0ë¶í° ììíë 모ë ìì ì ì를 ëíë ëë¤.
목ë¡ì An+Bë²ì§¸ ììë¼ê³ í´ìí ì ììµëë¤.
- : ì¬ì©ì ì§ì í¨í´ì ë§ì¡±í ì¸ë±ì¤ë¥¼ ê°ì§ë ìì를 ëíë
ëë¤. An+Bì ííë¡ ëíë´ë©°,
íì 구문
Error: could not find syntax for this itemìì
>ì íì
tr:nth-child(odd)ëëtr:nth-child(2n+1)- : HTML íì íìë²ì§¸ íì ëíë ëë¤.
tr:nth-child(even)ëëtr:nth-child(2n)- : HTML íì ì§ìë²ì§¸ íì ëíë ëë¤.
:nth-child(7)- : ììì 7ë²ì§¸ ìì를 ëíë ëë¤.
:nth-child(5n)- : 5 [=5Ã1], 10 [=5Ã2], 15 [=5Ã3], ... ë²ì§¸ì ìì를 ëíë
ëë¤. í¨í´ ê³µìì ë§ì¡±íë 첫 ë²ì§¸ ê°ì 0 [=5x0]ì¼ë¡ì ì무 ììë ì ííì§ ìëë°, ììì ì¸ë±ì¤ë 1ë¶í° ììíì§ë§
nì 0ë¶í° ì¦ê°í기 ë문ì ëë¤. ë¤ì ììí ì ìì¼ë, ë°ë¡ ìë ìì ì²ë¼ ê³µìì B ë¶ë¶ì´ >0ì¸ ê²½ì° ë³´ë¤ ë©ëí기 ì¬ìì§ëë¤.
- : 5 [=5Ã1], 10 [=5Ã2], 15 [=5Ã3], ... ë²ì§¸ì ìì를 ëíë
ëë¤. í¨í´ ê³µìì ë§ì¡±íë 첫 ë²ì§¸ ê°ì 0 [=5x0]ì¼ë¡ì ì무 ììë ì ííì§ ìëë°, ììì ì¸ë±ì¤ë 1ë¶í° ììíì§ë§
: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)- : ììì ì¸ ê°ì ìì를 ëíë ëë¤. [=-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>ìì를 ëíë ëë¤.
- : íì 그룹 ë´ìì 8ë²ì§¸ë¶í° 15ë²ì§¸ ê¹ì§ì
ìì¸í ìì
HTML
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
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;
}
ê²°ê³¼
ëª ì¸
| Specification |
|---|
| Selectors Level 4 > # nth-child-pseudo > |