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.