관리 메뉴

Lennon FE

[CSS3] μ…€λ ‰ν„°(Selector) λ³Έλ¬Έ

πŸ§‘‍πŸ’» Web/CSS

[CSS3] μ…€λ ‰ν„°(Selector)

Lennon 2022. 1. 26. 21:30
728x90
λ°˜μ‘ν˜•

CSS μ…€λ ‰ν„°λž€?

 

μŠ€νƒ€μΌμ„ μ μš©μ‹œν‚€κΈ° μœ„ν•΄ HTML에 μž‘μ„±ν•œ μš”μ†Œλ“€μ— λŒ€ν•΄ μ…€λ ‰ν„°λ‘œ νŠΉμ •ν•˜κ³ , μ„ νƒλœ μš”μ†Œμ— μŠ€νƒ€μΌμ„ μ μš©ν•œλ‹€.

μš°μ„  보편적으둜 μ‚¬μš©ν•˜λŠ” 전체 μ…€λ ‰ν„°, νƒœκ·Έ μ…€λ ‰ν„°, id, classμ…€λ ‰ν„°λ₯Ό μ•Œμ•„λ³΄κ³ ,

κ·Έ ν›„ 후손 μ…€λ ‰ν„°, μžμ‹ μ…€λ ‰ν„°, 링크 μ…€λ ‰ν„°λ₯Ό μ•Œμ•„보도둝 ν•˜μž.

 

전체 μ…€λ ‰ν„°

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        color: blue;
      }
    </style>
  </head>
  <body>
    <p id="first-pTag">hi minho</p>
    <p class="second-pTag">hi ddoddo</p>
    <span>hello world!</span>
  </body>
</html>

*

* 으둜 μ…€λ ‰ν„°λ₯Ό μ§€μ •ν•˜λ©΄, html에 μžˆλŠ” λͺ¨λ“  μš”μ†Œλ₯Ό μ„ νƒν•˜λŠ” κ±Έ μ˜λ―Έν•œλ‹€.

μ–΄λ–€ νƒœκ·Έλ“  μ„ νƒλ˜μ–΄ κΈ€μžμƒ‰μ΄ νŒŒλž—κ²Œ λ³€ν•  것이닀.


νƒœκ·Έ μ…€λ ‰ν„°

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      p {
        color: red;
      }
    </style>
  </head>
  <body>
    <p>hi minho</p>
    <p>hi ddoddo</p>
  </body>
</html>

html에 νƒœκ·Έλ“€μ— μ‚¬μš©ν•˜λ©΄ 됨(p, div, span, table λ“±λ“±)

pνƒœκ·Έμ— λŒ€ν•΄ κΈ€μžμƒ‰μ„ red둜 μ§€μ •ν–ˆλ‹€. κ²°κ³ΌλŠ” λ‹€μŒκ³Ό κ°™λ‹€.


ID, Class μ…€λ ‰ν„°

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #first-pTag {
        color: red;
      }
      .second-pTag {
        color: orange;
      }
    </style>
  </head>
  <body>
    <p id="first-pTag">hi minho</p>
    <p class="second-pTag">hi ddoddo</p>
  </body>
</html>

λͺ¨λ‘κ°€ μ•Œλ‹€μ‹œν”Ό idμ…€λ ‰ν„°λŠ” #으둜, class μ…€λ ‰ν„°λŠ” .으둜 μ‹œμž‘ν•˜λ©΄ λœλ‹€.

λ‘˜ κ°œλŠ” λΉ„μŠ·ν•˜λ©΄μ„œ λ‹€λ₯Έ 점이 μžˆλ‹€. idλŠ” htmlμ—μ„œ μœ μΌν•΄μ•Όν•˜μ§€λ§Œ, classλŠ” μ•„λ‹ˆλ‹€.

μ…€λ ‰ν„°μ—μ„œλ„ classλŠ” μ—¬λŸ¬ 값을 지정할 수 μžˆλ‹€λŠ” 것이닀.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .first-pTag {
        color: red;
      }
      .second-pTag {
        color: blue;
      }
      .text-font {
        font-size: 50px;
      }
    </style>
  </head>
  <body>
    <p class="first-pTag">hi minho</p>
    <p class="second-pTag text-font">hi ddoddo</p>
    <span id="first-spnaTag">hello world!</span>
  </body>
</html>

hello ddoddoλ₯Ό ν¬ν•¨ν•œ pνƒœκ·Έμ—λŠ” 2개의 classκ°€ λ“€μ–΄κ°€ μžˆλŠ” κ±Έ 확인할 수 있고, λ‘˜ λ‹€ 적용됨이 ν™•μΈλœλ‹€.

class μ…€λ ‰ν„°λ₯Ό μ΄μš©ν•˜λŠ” 것은 μž¬μ‚¬μš© μΈ‘λ©΄μ—μ„œ μœ λ¦¬ν•˜λ‹€.


이제 μ‘μš©μ„ ν•΄λ³΄μž.

 

후손 μ…€λ ‰ν„°

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div p {
        color: red;
      }
    </style>
  </head>
  <body>
    <div>
      <p class="first-pTag">div p</p>
      <p class="second-pTag text-font">div p2</p>
      <table border="1">
        <tr>
          <td><p>d</p></td>
          <td>d</td>
          <td>d</td>
          <td>d</td>
        </tr>
      </table>
    </div>
    <p>p</p>
  </body>
</html>

div내뢀에 μžˆλŠ” pνƒœκ·Έμ— λŒ€ν•΄ color값을 λ³€λ™ν•΄μ£Όμ—ˆλ‹€. κ²°κ³Όκ°’ μ—­μ‹œ div내뢀에 μœ„μΉ˜ν•œ pνƒœκ·Έλ§Œ 바뀐 것을 확인할 수 μžˆλ‹€.

κ·ΈλŸ¬λ‚˜ λ§Œμ•½μ— divμ•ˆμ— <table>νƒœκ·Έμ— 감싸진 pνƒœκ·Έκ°€ μžˆλ‹€κ³  μƒκ°ν•΄λ³΄μž. 이것은 κ·Έ νƒœκ·Έ μ•ˆμ—λ„ 적용이 λ˜μ–΄ μ˜ˆμƒν•˜μ§€ λͺ»ν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆλ‹€. μ΄λŠ” μžμ‹ μ…€λ ‰ν„°λ‘œ ν•΄κ²°ν•  수 μžˆλ‹€.


μžμ‹ μ…€λ ‰ν„°

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div > p {
        color: red;
      }
    </style>
  </head>
  <body>
    <div>
      <p class="first-pTag">div p</p>
      <p class="second-pTag text-font">div p2</p>
      <table border="1">
        <tr>
          <td><p>d</p></td>
          <td>d</td>
          <td>d</td>
          <td>d</td>
        </tr>
      </table>
    </div>
    <p>p</p>
  </body>
</html>

div > p λ₯Ό μ΄μš©ν•΄ divμ•ˆμ— μžˆλŠ” μžμ‹μ€‘μ— pνƒœκ·Έλ₯Ό κ³ λ₯Ό 수 μžˆλ‹€. 후손(tableνƒœκ·Έ)κΉŒμ§€ 전달이 μ•ˆ λ˜λ―€λ‘œ, μ˜ˆμƒν•  수 μžˆλŠ” 값을 얻을 수 μžˆλ‹€. 


링크 μ…€λ ‰ν„°

<!DOCTYPE html>
<html>
  <head>
    <style>
      /* 아직 λ°©λ¬Έν•˜μ§€ μ•Šμ•˜μ„ λ•Œ 폰트 색상 black */
      a:link {
        color: black;
      }

      /* λ°©λ¬Έ ν–ˆμ„ λ•Œ 폰트 색상 red둜 λ³€κ²½ */
      a:visited {
        color: red;
      }

      /* λ§ˆμš°μŠ€κ°€ μ˜¬λΌκ°€ μžˆμ„ λ•Œ 30px둜 μ‘°μ • */
      a:hover {
        font-size: 30px;
      }

      /* μš”μ†Œκ°€ 클릭 된 μƒνƒœμΌ λ•Œ 색상 μ˜€λ Œμ§€ */
      a:active {
        color: orange;
      }
    </style>
  </head>
  <body>
    <a href="http://www.naver.com" targer="_black">This is a link</a><br />
  </body>
</html>

:link

μ…€λŸ­ν„°κ°€ 아직 λ°©λ¬Έν•˜μ§€ μ•Šμ€ 링크일 λ•Œ

:visited

μ…€λ ‰ν„°κ°€ λ°©λ¬Έν•œ 링크일 λ•Œ

:hover

셀렉터에 λ§ˆμš°μŠ€κ°€ 올린 μƒνƒœμΌ λ•Œ

:active

셀렉터에 마우슀λ₯Ό 클릭된 μƒνƒœμΌ λ•Œ

 

 

μž‘μ„±ν•œ 것을 μ œμ™Έν•œ λ§Žμ€ 셀렉터듀이 μžˆμ§€λ§Œ, 이 μ •λ„λ§Œ μˆ™μ§€ν•΄λ„ CSS3λ₯Ό μž‘μ„±ν•œ HTML에 잘 μ μš©ν•  수 μžˆμ„ 것이닀.

 

728x90
λ°˜μ‘ν˜•

'πŸ§‘β€πŸ’» Web > CSS' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[CSS] Transform, Transition, Animation에 λŒ€ν•΄ μ•Œμ•„λ³΄μž  (0) 2022.04.07
Comments