CSS ์ ์šฉ๊ณผ์ • :: cascade, specificity, inheritance์— ๊ด€ํ•˜์—ฌ

์„ ํƒ์ž ์ถฉ๋Œ ์‹œ

<p class="special"></p>

/*css*/
.special{
 color: blue;
}
p{
 color: red;
}

 

css๋Š” cascade์™€ specificity๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” rule์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ํ•ด๋‹น rule์— ๋”ฐ๋ผ ์ถฉ๋Œ ์‹œ ๋” ๊ฐ•ํ•œ ์„ ํƒ์ž๊ฐ€ ์ ์šฉ๋œ๋‹ค.

  1. cascade rule : ๋’ค์— ์ ์šฉ๋œ ์Šคํƒ€์ผ์ด ์•ž์„  ์Šคํƒ€์ผ์„ ๋ฎ์–ด์“ด๋‹ค.
  2. specificity rule : ์š”์†Œ๋ฅผ ๋” ํŠน์ •ํ•˜๋Š” ์„ ํƒ์ž๊ฐ€ ์Šคํƒ€์ผ์„ ์ฐจ์ง€ํ•œ๋‹ค.
    • ๋”ฐ๋ผ์„œ ์œ„์˜ ์˜ˆ์ œ์—์„œ specificty rule์— ์˜ํ•ด color๋Š” blue๊ฐ€ ๋œ๋‹ค.

 

specificity

specificity๋Š” 4๊ฐ€์ง€์˜ ๋‹จ์œ„๋กœ ์ธก์ •๋œ๋‹ค.

  • Thousands : style ์†์„ฑ ๋‚ด์— ์ •์˜๋˜์–ด ์žˆ๋Š” style(inline style)์ผ ๊ฒฝ์šฐ
  • Hundreds : ID selector๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•œ style์ผ ๊ฒฝ์šฐ
  • Tens : Class selector๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•œ style์ผ ๊ฒฝ์šฐ
  • Ones : ์ด์™ธ์˜ selector
  • ์˜ˆ์™ธ : universal selector(*), combinators(+, >, ~, ''), negation pseudo-class (:not)๋Š” specificity์— ์˜ํ–ฅ์ด ์—†๋‹ค.

ํ•ด๋‹น ๋‹จ์œ„๋“ค์€ ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•œ ๋‹จ์œ„์ผ ๋ฟ Ones๊ฐ€ 10๊ฐœ ๋ชจ์˜€๋‹ค๊ณ  Tens๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์•„์ง€์ง„ ์•Š๋Š”๋‹ค.

 

!importance

ํ•ด๋‹น ๋ช…๋ น์–ด๋ฅผ style ์ •์˜ ๋’ค์— ๋ถ™์ž„์œผ๋กœ์จ specificity๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, !importance๋ฅผ ๋ถ™์ธ style์„ ๋‹ค์‹œ overrideํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋” ๋†’์€ specificity๋ฅผ ๊ฐ€์ง€๊ณ  !importance๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ css๋Š” ์ ์šฉ๋œ๋‹ค.

  1. user agent style sheet : ๋ธŒ๋ผ์šฐ์ € ๋“ฑ์˜ ๊ธฐ๋ณธ ์Šคํƒ€์ผ
  2. normal declarations in user style sheets : ์œ ์ €์˜ ์ปค์Šคํ…€ ์Šคํƒ€์ผ
  3. normal declarations in author style sheets : ์›น ๊ฐœ๋ฐœ์ž๊ฐ€ ์„ธํŒ…ํ•œ ์Šคํƒ€์ผ
  4. important declarations in author style sheets : ๊ฐœ๋ฐœ์ž important ์Šคํƒ€์ผ
  5. important declarations in user style sheets : ์œ ์ € important ์Šคํƒ€์ผ

 

Inheritance

inheritance๋„ css์˜ ์ ์šฉ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์š”ํ•œ ์š”์†Œ์ด๋‹ค. ์–ด๋Š ํ•œ ์š”์†Œ์— ์ค€ style์ด ํ•ด๋‹น ์š”์†Œ์˜ ์ž์†๋“ค์—๊ฒŒ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฒƒ์„ inheritance๋ผ๊ณ  ํ•œ๋‹ค.

์–ด๋–ค ์†์„ฑ๋“ค์€ ์ž๋™์œผ๋กœ ์ƒ์†๋˜๊ณ  ์–ด๋–ค ์†์„ฑ๋“ค์€ ์ƒ์†๋˜์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด color, font-family์™€ ๊ฐ™์€ ์†์„ฑ๋“ค์€ ์ƒ์†๋˜์ง€๋งŒ margin, padding, border ์™€ ๊ฐ™์€ ์Šคํƒ€์ผ ์†์„ฑ์€ ์ƒ์†๋˜์ง€ ์•Š๋Š”๋‹ค. css๋Š” inheritance๋ฅผ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ 4๊ฐœ์˜ ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  1. inherit : ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•œ๋‹ค.
  2. initial : ๊ฐ ์†์„ฑ ๋ณ„ default ๊ฐ’์ด ์ ์šฉ๋œ๋‹ค.
  3. unset : ๊ฐ ์š”์†Œ์˜ ๊ธฐ๋ณธ ์†์„ฑ ๊ฐ’์ด inherit์ธ์ง€ initial์ธ์ง€์— ๋”ฐ๋ผ ์ ์šฉ๋œ๋‹ค.
  4. revert : unset๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ์†์„ฑ์ด ์•„๋‹Œ ๋ธŒ๋ผ์šฐ์ €์˜ ๊ธฐ๋ณธ ์†์„ฑ ๊ฐ’์œผ๋กœ ์ ์šฉ๋œ๋‹ค.

all์ด๋ผ๋Š” ์†์„ฑ์œผ๋กœ ๋ชจ๋“  ์†์„ฑ์„ ์–ด๋–ค ์†์„ฑ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ง€ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

.hi{
 all : unset;
}

 

inherit ๋˜๋Š” ์†์„ฑ

  • color
  • cursor
  • font
  • line-height
  • letter-spacing
  • list-style
  • text-align
  • visibility
  • white-space
  • word-spacing
๋ฐ˜์‘ํ˜•