Style Sheet/CSS

🌟 first-child / first-of-type 속성 차이점

인파_ 2023. 4. 20. 08:17

first-child-first-of-type

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.