CSS

CSS - κ°€μƒν΄λž˜μŠ€ + μš”μ†Œ μ„ νƒμž

eddie0329 2021. 1. 28. 23:39
λ°˜μ‘ν˜•

πŸ“Œ λͺ©λ‘

  • Introudction
  • Hover
  • Active
  • Focus
  • First child
  • Last child
  • nth child
  • nth of type
  • not
  • before
  • after
  • 속성 μ„ νƒμž
  • 상속
  • μš°μ„  μˆœμœ„
  • Conclusion

πŸ“Œ Introduction

μ•ˆλ…•ν•˜μ„Έμš”. μ˜€λŠ˜μ€ cssμ—μ„œμ˜ κ°€μƒν΄λž˜μŠ€ μ„ νƒμžμ— λŒ€ν•΄μ„œ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€. :둜 ν‘œμ‹œν•˜κ²Œ λ©λ‹ˆλ‹€. ::λž‘ ν—·κ°ˆλ¦¬μ§€ λ§ˆμ„Έμš”!

πŸ“Œ Hover

마우슀 포인터가 μ˜¬λΌκ°€ μžˆλŠ” λ™μ•ˆμ—λ§Œ 선택이 λ©λ‹ˆλ‹€.

<style>
  .my-btn:hover {
    color: red;
  }
</style
<button class="my-btn">HELLO</button>

πŸ“Œ Active

마우슀둜 ν΄λ¦­ν•˜λŠ” λ™μ•ˆμ—λ§Œ 선택이 λ©λ‹ˆλ‹€.

<style>
  .my-btn:active {
    color: blue;
  }
</style>
<button class="my-btn">HELLO</button>

πŸ“Œ Focus

포컀슀 된 λ™μ•ˆμ—λ§Œ 선택이 λ©λ‹ˆλ‹€.

<style>
  .my-btn:focus {
    background: teal;
  }
</style>
<button class="my-btn">HELLO</button>

πŸ“Œ First child

μš”μ†Œκ°€ ν˜•μ œ μš”μ†Œ 쀑 첫번째 μš”μ†ŒλΌλ©΄ 선택

<style>
   li:first-child {
    color: red;
  }
</style>

<ul>
  <li>pear</li> <!-- 선택  -->
  <li>lemon</li>
  <li>orange</li>
</ul>

πŸ“Œ Last child

μš”μ†Œκ°€ ν˜•μ œ μš”μ†Œ 쀑 κ°€μž₯ λ§ˆμ§€λ§‰ μš”μ†Œ 선택

<style>
 li:last-child {
    color: orange;
  }
</style>

<ul>
  <li>pear</li> 
  <li>lemon</li>
  <li>orange</li> <!-- 선택  -->
</ul>

πŸ“Œ nth child

ν˜•μ œ μš”μ†Œ 쀑 n 번째 μš”μ†ŒλΌλ©΄ 선택 (0λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€.)

<style>
li:nth-child(2) {
    color: blue;
  }
</style>

<ul>
  <li>pear</li> <!-- 0  -->
  <li>lemon</li> <!-- 선택  -->
  <li>orange</li> <!-- 2  -->
</ul>

μ΄λ ‡κ²Œλ„ μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.

<style>
 li:nth-child(2n) {
    color: blue;
  }
</style>

<ul>
  <li>pear</li> 
  <li>lemon</li> <!-- 선택  -->
  <li>orange</li>
  <li>mango</li> <!-- 선택  -->
</ul>
<style>
.fruits li:nth-child(1) {
    color: yellow;
  }
</style>

<ul class="fruits">
  <div>pear</div> 
  <li>lemon</li>
  <li>orange</li>
  <li>mango</li>
</ul>

μœ„μ— 뢀뢄은 μ‹€μ œλ‘œ 아무것도 택해지지 μ•ŠμŠ΅λ‹ˆλ‹€. nth-child(1)은 div인 pearλ₯Ό μ„ νƒν•˜κ²Œ λ˜λŠ”λ° 그게 liκ°€ μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— 아무것도 선택이 λ˜μ–΄μ§€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

πŸ“Œ nth of type

E의 νƒ€μž…κ³Ό λ™μΌν•œ νƒ€μž…μΈ ν˜•μ œ μš”μ†Œ 쀑 Eκ°€ n번째 μš”μ†ŒλΌλ©΄ 선택

E:nth-of-type(n)
<style>
.fruits li:nth-of-type(1) {
    color: red;
  }
</style>

<ul class="fruits">
  <div>pear</div> 
  <li>lemon</li> <!-- 선택  -->
  <li>orange</li>
  <li>mango</li>
</ul>

πŸ“Œ before

ν•΄λ‹Ή μš”μ†Œ λ‚΄λΆ€μ˜ μ•žμ— λ‚΄μš©μ„ μ‚½μž…ν•˜λŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.

<style>
ul li::before{
    content: "fruit";
    font-size: large;
    color: red;
  }
</style>

<ul class="fruits">
  <li>{fruit}pear</li> 
  <li>{fruit}lemon</li> 
  <li>{fruit}orange</li>
  <li>{fruit}mango</li>
</ul>

μœ„μ™€ 같이 fruitκ°€ λ“€μ–΄κ°€κ²Œ λ©λ‹ˆλ‹€. (빨간색) μ΄λ•Œ contentκ°€ 항상 μžˆμ–΄μ•Όν•¨μ„ λͺ…심해야 ν•©λ‹ˆλ‹€.

πŸ“Œ after

ν•΄λ‹Ή μš”μ†Œ λ‚΄λΆ€μ˜ 뒀에 λ‚΄μš©μ„ μ‚½μž… ν•˜λŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.

<style>
ul li::after {
    content: "END";
    color: blue;
  }
</style>

<ul class="fruits">
  <li>pear{END}</li> 
  <li>lemon{END}</li> 
  <li>orange{END}</li>
  <li>mango{END}</li>
</ul>

πŸ“Œ 속성 μ„ νƒμž

속성은 μ΄λ ‡κ²Œ 선택이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

<style>
  [disabled] {
    opacity: 0.9;
    color: red;
  }
  [type="password"] {
    opacity: 0.1;
  }
</style>

<input type="password" value="1234">
<input type="text" value="eddie" disabled>

λΉ„μŠ·ν•˜κ²Œ ^= μ΄λ ‡κ²Œ 생긴 녀석이 μžˆλŠ”λ° 이것은 속성 attr을 ν¬ν•¨ν•˜λ©° 속성 값이 value둜 μ‹œμž‘ν•˜λŠ” μš”μ†Œλ₯Ό μ„ νƒν•˜κ²Œ λ©λ‹ˆλ‹€.

<style>
  [class^="btn-"] {
    font-weight: bold;
    border-radius: 20px;
  }
</style>

<button class="btn-success">Success</button> <!-- 선택 -->
<button class="btn-danger">Danger</button> <!-- 선택  -->
<button>Normal</button>

이제 μ‹œμž‘ν•˜λŠ” μš”μ†Œμ˜€μ§€λ§Œ λλ‚˜λŠ” μš”μ†Œλ₯Ό 선택 ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

$=λ₯Ό μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€.

<style>
  [class$="success"] {
    color: blue;
  }
</style>

<button class="btn-success">Success</button> <!-- 선택 -->
<button class="btn-danger">Danger</button>
<button>Normal</button>

πŸ“Œ μš°μ„ μˆœμœ„

μœ„μ˜ μ„ νƒμžμ˜ μš°μ„ μˆœμœ„ μž…λ‹ˆλ‹€.

  1. important
  2. 인라인 μŠ€νƒ€μΌ
  3. 아이디
  4. 클래슀
  5. 전체 μ„ νƒμž
  6. 상속

πŸ“Œ Conclusion

μ΄λ ‡κ²Œ 돔을 μ–΄λ–»κ²Œ μ„ νƒν•˜λŠλƒμ— λ”°λΌμ„œ λ§Žμ€ μ„ νƒμžλ“€μ΄ μžˆμ—ˆλ„€μš”.

λ°˜μ‘ν˜•