Style Sheet/CSS
π first-child / first-of-type μμ± μ°¨μ΄μ
μΈν_
2023. 4. 20. 08:17
first-child μ first-of-type μ°¨μ΄μ
first-child μ first-of-typeμ λκ°μ΄ μμ νμ μμ κ³ λ₯΄λκ²κ°μλ° λ¬΄μμ΄ λ€λ₯ΌκΉ? νλ°©μ μ΄ν΄νκΈ° μ½κ² κ°λ¨λͺ λ£νκ² μ€λͺ νλ©΄ μ΄λ λ€.
div > p:first-child
- 'div νμμ p μμ μ€ μ²«λ²μ§Έ μμ' λ‘ μ½λ κ² μλλΌ 'div νμμ 첫λ²μ§Έ μμμΈ μμκ° p μμ' μ΄λ©΄μ΄λ€.
- μ¦, divμ μμ μμμ€μ first-childλ₯Ό κ³ λ₯΄λλ° κ·Έκ² pμ΄λ©΄ μ€νμΌμ μ μ©νλ€.
- λ§μΌ pκ° μλ€λ©΄ μ€νμΌμ μ μ©νμ§ μλλ€.
- 'div first-child is p' λΌκ³ μμ΄ λ¬Έμ₯μΌλ‘ μ΄ν΄νλ©΄ λλ€.
.parent > span:first-child {
background: lightgreen;
}
.parent2 > p:first-child {
background: lightgreen;
}
<div class="parent">
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<p class="box box1">p</p>
<p class="box box1">p</p>
</div>
<div class="parent2">
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<p class="box box1">p</p>
<p class="box box1">p</p>
</div>
See the Pen first-child by barzz12 (@inpaSkyrim) on CodePen.
div > p:first-of-type
- λ§μ°¬κ°μ§λ‘ 'div νμμ p μμ μ€(OF) 첫λ²μ§Έ μμ' λΌκ³ μ½μΌλ©΄ λ°λ‘ μ΄ν΄κ° λ κ²μ΄λ€.
- μ¦, divμ μμμμμ€μ pνκ·Έλ₯Ό λͺ¨λ κ³ λ₯΄κ³ κ·Έμ€μ first-of-typeλ§ μ€νμΌμ μ μ©νλ€.
- 'div p first-of-type' λΌκ³ μμ΄ λ¬Έμ₯μΌλ‘ μ΄ν΄νλ©΄ λλ€.
.parent > span:first-of-type {
background: lightgreen;
}
.parent2 > p:first-of-type {
background: lightgreen;
}
<div class="parent">
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<p class="box box1">p</p>
<p class="box box1">p</p>
</div>
<div class="parent2">
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<span class="box box1">span</span>
<p class="box box1">p</p>
<p class="box box1">p</p>
<p class="box box1">p</p>
</div>
See the Pen first-of-type by barzz12 (@inpaSkyrim) on CodePen.
last-child μ last-of-type μ°¨μ΄μ
last-child μ last-of-type λ λκ°μ κ°λ μΌλ‘ μ μ©λλ€κ³ 보면λλ€.
div > p:last-child
- divμ μμ μμμ€μ first-childλ₯Ό κ³ λ₯΄λλ° κ·Έκ² pμ΄λ©΄ μ€νμΌμ μ μ©νλ€.
- λ§μΌ pκ° μλ€λ©΄ μ€νμΌμ μ μ©νμ§ μλλ€.
.parent > span:last-child {
background: lightgreen;
}
.parent2 > p:last-child {
background: lightgreen;
}
See the Pen Untitled by barzz12 (@inpaSkyrim) on CodePen.
div > p:last-of-type
- divμ μμμμμ€μ pνκ·Έλ₯Ό λͺ¨λ κ³ λ₯΄κ³ κ·Έμ€μ first-of-typeλ§ μ€νμΌμ μ μ©νλ€.
.parent > span:last-of-type {
background: lightgreen;
}
.parent2 > p:last-of-type {
background: lightgreen;
}
See the Pen Untitled by barzz12 (@inpaSkyrim) on CodePen.