selector

μœ μ•„ν˜„Β·2023λ…„ 11μ›” 30일
0

CSS

λͺ©λ‘ 보기
9/9

μ—…λ‘œλ“œμ€‘..

😴 λ‚΄κ°€ ν—·κ°ˆλ €μ„œ μ •λ¦¬ν•˜λŠ” μ…€λ ‰ν„°


μŠ€νƒ€μΌμ„ μ μš©ν•˜κ³ μž ν•˜λŠ” HTML μš”μ†Œλ₯Ό νŠΉμ •ν•˜κΈ° μœ„ν•œ λͺ©μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” 것,
HTML μš”μ†Œλ₯Ό μ…€λ ‰ν„°λ‘œ μ§€μ •ν•˜κ³  μ„ νƒλœ μš”μ†Œμ— μŠ€νƒ€μΌμ„ μ •μ˜ν•˜λŠ” 것

볡수개의 μ…€λ ‰ν„°λ₯Ό μ—°μ†ν•˜μ—¬ 지정할 수 있고 μ‰Όν‘œ(,)둜 κ΅¬λΆ„ν•œλ‹€.

h1, p {
	color: red;
}

전체 μ…€λ ‰ν„°

* : HTML λ¬Έμ„œ λ‚΄μ˜ λͺ¨λ“  μš”μ†Œλ₯Ό 선택, HTML μš”μ†Œλ₯Ό ν¬ν•¨ν•œ λͺ¨λ“  μš”μ†Œκ°€ μ„ νƒλœλ‹€ (head μš”μ†Œλ„ 포함)

<!DOCTYPE html>
<html>
<head>
  <style>
    /* λͺ¨λ“  μš”μ†Œ 선택 */
    * { color: red; }
  </style>
</head>
<body>
  <h1>Heading</h1>
  <div>
    <p>paragraph 1</p>
    <p>paragraph 2</p>
  </div>
  <p>paragraph 3</p>
</body>
</html>

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

νƒœκ·Έλͺ… : μ§€μ •λœ νƒœκ·Έλͺ…을 κ°€μ§€λŠ” μš”μ†Œλ₯Ό 선택

<!DOCTYPE html>
<html>
<head>
  <style>
    /* λͺ¨λ“  p νƒœκ·Έ μš”μ†Œ 선택 */
    p { color: red; }
  </style>
</head>
<body>
  <h1>Heading</h1>
  <div>
    <p>paragraph 1</p>
    <p>paragraph 2</p>
  </div>
  <p>paragraph 3</p>
</body>
</html>

ID μ…€λ ‰ν„°

#id μ–΄νŠΈλ¦¬λ·°νŠΈ κ°’ : id μ–΄νŠΈλ¦¬λ·°νŠΈ 값을 μ§€μ •ν•˜μ—¬ μΌμΉ˜ν•˜λŠ” μš”μ†Œλ₯Ό 선택, id μ–΄νŠΈλ¦¬λ·°νŠΈ 값은 쀑볡될 수 μ—†λ‹€.

<!DOCTYPE html>
<html>
<head>
  <style>
    /* id μ–΄νŠΈλ¦¬λ·°νŠΈ 값이 p1인 μš”μ†Œλ₯Ό 선택 */
    #p1 { color: red; }
  </style>
</head>
<body>
  <h1>Heading</h1>
  <div class="container">
    <p id="p1">paragraph 1</p>
    <p id="p2">paragraph 2</p>
  </div>
  <p>paragraph 3</p>
</body>
</html>

클래슀 μ…€λ ‰ν„°

  1. .class μ–΄νŠΈλ¦¬λ·°νŠΈ κ°’ : class μ–΄νŠΈλ¦¬λ·°νŠΈ 값을 μ§€μ •ν•˜μ—¬ μΌμΉ˜ν•˜λŠ” μš”μ†Œλ₯Ό 선택, class μ–΄νŠΈλ¦¬λ·°νŠΈ 값은 쀑볡 κ°€λŠ₯ν•˜λ‹€.
<!DOCTYPE html>
<html>
<head>
  <style>
    /* class μ–΄νŠΈλ¦¬λ·°νŠΈ 값이 container인 λͺ¨λ“  μš”μ†Œλ₯Ό 선택 */
    /* color μ–΄νŠΈλ¦¬λ·°νŠΈλŠ” μžμ‹ μš”μ†Œμ— μƒμ†λœλ‹€. */
    .container { color: red; }
    /* not supported in IE */
    #p2 { color: initial; }
  </style>
</head>
<body>
  <h1>Heading</h1>
  <div class="container">
    <p id="p1">paragraph 1</p>
    <p id="p2">paragraph 2</p>
  </div>
  <p>paragraph 3</p>
</body>
</html>

μ–΄νŠΈλ¦¬λ·°νŠΈ μ…€λ ‰ν„°

  1. μ…€λ ‰ν„°[μ–΄νŠΈλ¦¬λ·°νŠΈ] : μ§€μ •λœ μ–΄νŠΈλ¦¬λ·°νŠΈλ₯Ό κ°–λŠ” λͺ¨λ“  μš”μ†Œ 선택

<!DOCTYPE html>
<html>
<head>
  <style>
    /* a μš”μ†Œ 쀑에 href μ–΄νŠΈλ¦¬λ·°νŠΈλ₯Ό κ°–λŠ” λͺ¨λ“  μš”μ†Œ */
    a[href] { color: red; }
  </style>
</head>
<body>
  <a href="http://www.poiemaweb.com">poiemaweb.com</a><br>
  <a href="http://www.google.com" target="_blank">google.com</a><br>
  <a href="http://www.naver.com" target="_top">naver.com</a>
</body>
</html>
  1. μ…€λ ‰ν„°[μ–΄νŠΈλ¦¬λ·°νŠΈ="κ°’"] : μ§€μ •λœ μ–΄νŠΈλ¦¬λ·°νŠΈλ₯Ό 가지며 μ§€μ •λœ κ°’κ³Ό μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ 값이 μΌμΉ˜ν•˜λŠ” λͺ¨λ“  μš”μ†Œ 선택
<!DOCTYPE html>
<html>
<head>
  <style>
    /* a μš”μ†Œ 쀑에 target μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ 값이 "_blank"인 λͺ¨λ“  μš”μ†Œ */
    a[target="_blank"] { color: red; }
  </style>
</head>
<body>
  <a href="http://www.poiemaweb.com">poiemaweb.com</a><br>
  <a href="http://www.google.com" target="_blank">google.com</a><br>
  <a href="http://www.naver.com" target="_top">naver.com</a>
</body>
</html>
  1. μ…€λ ‰ν„°[μ–΄νŠΈλ¦¬λ·°νŠΈ~="κ°’"] : μ§€μ •λœ μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ 값이 μ§€μ •λœ 값을 (곡백으둜 λΆ„λ¦¬λœ) 단어λ₯Ό ν¬ν•¨ν•˜λŠ” μš”μ†Œ 선택
<!DOCTYPE html>
<html>
<head>
  <style>
    /* h1 μš”μ†Œ 쀑에 title μ–΄νŠΈλ¦¬λ·°νŠΈ 값에 "first"λ₯Ό λ‹¨μ–΄λ‘œ ν¬ν•¨ν•˜λŠ” μš”μ†Œ */
    h1[title~="first"] { color: red; }
  </style>
</head>
<body>
  <h1 title="heading first">Heading first</h1>
  <h1 title="heading-first">Heading-first</h1>
  <h1 title="heading second">Heading second</h1>
  <h1 title="heading third">Heading third</h1>
</body>
</html>
  1. μ…€λ ‰ν„°[μ–΄νŠΈλ¦¬λ·°νŠΈ|="κ°’"] : μ§€μ •λœ μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ κ°’κ³Ό μΌμΉ˜ν•˜κ±°λ‚˜ 지정 μ–΄νŠΈλ¦¬λ·°νŠΈ κ°’ λ’€ 연이은(”값-”)으둜 μ‹œμž‘ν•˜λŠ” μš”μ†Œ 선택
<!DOCTYPE html>
<html>
<head>
  <style>
    /* p μš”μ†Œ 쀑에 lang μ–΄νŠΈλ¦¬λ·°νŠΈ 값이 "en"κ³Ό μΌμΉ˜ν•˜κ±°λ‚˜ "en-"둜 μ‹œμž‘ν•˜λŠ” μš”μ†Œ */
    p[lang|="en"] { color: red; }
  </style>
</head>
<body>
  <p lang="en">Hello!</p>
  <p lang="en-us">Hi!</p>
  <p lang="en-gb">Ello!</p>
  <p lang="us">Hi!</p>
  <p lang="no">Hei!</p>
</body>
</html>
  1. μ…€λ ‰ν„°[μ–΄νŠΈλ¦¬λ·°νŠΈ^="κ°’"] : μ§€μ •λœ μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ κ°’μœΌλ‘œ μ‹œμž‘ν•˜λŠ” μš”μ†Œ 선택
<!DOCTYPE html>
<html>
<head>
  <style>
    /* a μš”μ†Œ 쀑에 href μ–΄νŠΈλ¦¬λ·°νŠΈ 값이 "https://"둜 μ‹œμž‘ν•˜λŠ” μš”μ†Œ */
    a[href^="https://"] { color: red; }
  </style>
</head>
<body>
  <a href="https://www.test.com">https://www.test.com</a><br>
  <a href="http://www.test.com">http://www.test.com</a>
</body>
</html>
  1. μ…€λ ‰ν„°[μ–΄νŠΈλ¦¬λ·°νŠΈ$="κ°’"] : μ§€μ •λœ μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ κ°’μœΌλ‘œ λλ‚˜λŠ” μš”μ†Œ 선택
<!DOCTYPE html>
<html>
<head>
  <style>
    /* a μš”μ†Œ 쀑에 href μ–΄νŠΈλ¦¬λ·°νŠΈ 값이 ".html"둜 λλ‚˜λŠ” μš”μ†Œ */
    a[href$=".html"] { color: red; }
  </style>
</head>
<body>
  <a href="test.html">test.html</a><br>
  <a href="test.jsp">test.jsp</a>
</body>
</html>
  1. μ…€λ ‰ν„°[μ–΄νŠΈλ¦¬λ·°νŠΈ*="κ°’"] : μ§€μ •λœ μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ 값을 ν¬ν•¨ν•˜λŠ” μš”μ†Œ 선택
<!DOCTYPE html>
<html>
<head>
  <style>
    /* div μš”μ†Œ μ€‘μ—μ„œ class μ–΄νŠΈλ¦¬λ·°νŠΈ 값에 "test"λ₯Ό ν¬ν•¨ν•˜λŠ” μš”μ†Œ */
    div[class*="test"] { color: red; }
    /* div μš”μ†Œ μ€‘μ—μ„œ class μ–΄νŠΈλ¦¬λ·°νŠΈ 값에 "test"λ₯Ό λ‹¨μ–΄λ‘œ ν¬ν•¨ν•˜λŠ” μš”μ†Œ */
    div[class~="test"] { background-color: yellow; }
  </style>
</head>
<body>
  <div class="first_test">The first div element.</div>
  <div class="second">The second div element.</div>
  <div class="test">The third div element.</div>
  <p class="test">This is some text in a paragraph.</p>
</body>
</html>

볡합 μ…€λ ‰ν„°

  1. 후손 μ…€λ ‰ν„°

    μ…€λ ‰ν„°A μ…€λ ‰ν„°B

    μžμ‹ μ˜ 1 레벨 μƒμœ„μ— μ†ν•˜λŠ” μš”μ†Œλ₯Ό λΆ€λͺ¨ μš”μ†Œ, 1 레벨 ν•˜μœ„μ— μ†ν•˜λŠ” μš”μ†Œλ₯Ό μžμ‹ μš”μ†Œ 라고 ν•œλ‹€.

    μžμ‹ λ³΄λ‹€ N 레벨 ν•˜μœ„μ— μ†ν•˜λŠ” μš”μ†ŒλŠ” 후손 μš”μ†Œ (ν•˜μœ„ μš”μ†Œ) 라고 ν•œλ‹€.

    후손 μ…€λ ‰ν„°λŠ” μ…€λ ‰ν„° A의 λͺ¨λ“  후손(ν•˜μœ„) μš”μ†Œ 쀑 μ…€λ ‰ν„° B와 μΌμΉ˜ν•˜λŠ” μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        /* div μš”μ†Œμ˜ ν›„μ†μš”μ†Œ 쀑 p μš”μ†Œ */
        div p { color: red; }
      </style>
    </head>
    <body>
      <h1>Heading</h1>
      <div>
        <p>paragraph 1</p>
        <p>paragraph 2</p>
        <span><p>paragraph 3</p></span>
      </div>
      <p>paragraph 4</p>
    </body>
    </html>
  2. μžμ‹ μ…€λ ‰ν„°

    μ…€λ ‰ν„°A > μ…€λ ‰ν„°B

    μžμ† μ…€λ ‰ν„°λŠ” μ…€λ ‰ν„° A의 λͺ¨λ“  μžμ‹ μš”μ†Œ 쀑 μ…€λ ‰ν„° B와 μΌμΉ˜ν•˜λŠ” μ„ νƒν•œλ‹€.

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        /* div μš”μ†Œμ˜ μžμ‹μš”μ†Œ 쀑 p μš”μ†Œ */
        div > p { color: red; }
      </style>
    </head>
    <body>
      <h1>Heading</h1>
      <div>
        <p>paragraph 1</p>
        <p>paragraph 2</p>
        <span><p>paragraph 3</p></span>
      </div>
      <p>paragraph 4</p>
    </body>
    </html>
  3. ν˜•μ œ(λ™μœ„) μ…€λ ‰ν„°

    ν˜•μ œ 관계(λ™μœ„ 관계)μ—μ„œ 뒀에 μΌμΉ˜ν•˜λŠ” μš”μ†Œλ₯Ό 선택할 λ•Œ μ‚¬μš©

    3-1. 인접 ν˜•μ œ μ…€λ ‰ν„°

    μ…€λ ‰ν„°A + μ…€λ ‰ν„°B

    μ…€λ ‰ν„°A의 ν˜•μ œ μš”μ†Œ 쀑 μ…€λ ‰ν„°A λ°”λ‘œ 뒀에 μΌμΉ˜ν•˜λŠ” μ…€λ ‰B μš”μ†Œλ₯Ό 선택, A와 B 사이에 λ‹€λ₯Έ μš”μ†Œκ°€ μ‘΄μž¬ν•˜λ©΄ μ„ νƒλ˜μ§€ μ•ŠμŒ

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        /* p μš”μ†Œμ˜ ν˜•μ œ μš”μ†Œ 쀑에 p μš”μ†Œ λ°”λ‘œ 뒀에 μœ„μΉ˜ν•˜λŠ” ul μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€. */
        p + ul { color: red; }
      </style>
    </head>
    <body>
      <div>A div element.</div>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
      </ul>
    
      <p>The first paragraph.</p>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
      </ul>
    
      <h2>Another list</h2>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
      </ul>
    </body>
    </html>

    3-2. 일반 ν˜•μ œ μ…€λ ‰ν„°

    μ…€λ ‰ν„°A ~ μ…€λ ‰ν„°B

    μ…€λ ‰ν„°A의 ν˜•μ œ μš”μ†Œ 쀑 μ…€λ ‰ν„°A 뒀에 μΌμΉ˜ν•˜λŠ” μ…€λ ‰ν„°B μš”μ†Œλ₯Ό λͺ¨λ‘ 선택

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        /* p μš”μ†Œμ˜ ν˜•μ œ μš”μ†Œ 쀑에 p μš”μ†Œ 뒀에 μœ„μΉ˜ν•˜λŠ” ul μš”μ†Œλ₯Ό λͺ¨λ‘ μ„ νƒν•œλ‹€.*/
        p ~ ul { color: red; }
      </style>
    </head>
    <body>
      <div>A div element.</div>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
      </ul>
    
      <p>The first paragraph.</p>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
      </ul>
    
      <h2>Another list</h2>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
      </ul>
    </body>
    </html>

가상 클래슀 μ…€λ ‰ν„°

μš”μ†Œμ˜ νŠΉμ • μƒνƒœμ— 따라 μŠ€νƒ€μΌμ„ μ •μ˜ν•  λ•Œ μ‚¬μš©, νŠΉμ • μƒνƒœλž€ 예λ₯Ό λ“€μ–΄ λ‹€μŒκ³Ό κ°™λ‹€.

  • λ§ˆμš°μŠ€κ°€ μ˜¬λΌμ™€ μžˆμ„ λ•Œ
  • 링크λ₯Ό λ°©λ¬Έν–ˆμ„ λ•Œμ™€ 아직 λ°©λ¬Έν•˜μ§€ μ•Šμ•˜μ„ λ•Œ
  • ν¬μ»€μŠ€κ°€ 듀어와 μžˆμ„ λ•Œ

이런 νŠΉμ • μƒνƒœμ—λŠ” μ›λž˜ ν΄λž˜μŠ€κ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ§€λ§Œ 가상 클래슀λ₯Ό μž„μ˜λ‘œ 지정해 μ„ νƒν•˜λŠ” 방법이닀.

가상 ν΄λž˜μŠ€λŠ” λ§ˆμΉ¨ν‘œ(.) λŒ€μ‹  콜둠(:)을 μ‚¬μš©, CSS ν‘œμ€€μ— μ˜ν•΄ 미리 μ •μ˜λœ 이름이 있기 λ•Œλ¬Έμ— μž„μ˜μ˜ 이름을 μ‚¬μš© ν•  수 μ—†λ‹€.

νŒ¨ν„΄

selector:pseudo-class {
  property: value;
}
<!DOCTYPE html>
<html>
<head>
  <style>
    /* a μš”μ†Œκ°€ hover μƒνƒœμΌ λ•Œ */
    a:hover { background-color: red; }
    /* input μš”μ†Œκ°€ focus μƒνƒœμΌ λ•Œ */
    input:focus { background-color: yellow; }
  </style>
</head>
<body>
  <a href="#">Hover me</a><br><br>
  <input type="text" placeholder="focus me">
</body>
</html>
  1. 링크 μ…€λ ‰ν„°, 동적 μ…€λ ‰ν„°

    pseudo-classDescription
    :linkμ…€λ ‰ν„°κ°€ λ°©λ¬Έν•˜μ§€ μ•Šμ€ 링크일 λ•Œ
    :visitedμ…€λ ‰ν„°κ°€ λ°©λ¬Έν•œ 링크일 λ•Œ
    :hover셀렉터에 λ§ˆμš°μŠ€κ°€ μ˜¬λΌμ™€ μžˆμ„ λ•Œ
    :activeμ…€λ ‰ν„°κ°€ 클릭된 μƒνƒœμΌ λ•Œ
    :focus셀렉터에 ν¬μ»€μŠ€κ°€ 듀어와 μžˆμ„ λ•Œ
<!DOCTYPE html>
<html>
<head>
  <style>
    /* a μš”μ†Œκ°€ λ°©λ¬Έν•˜μ§€ μ•Šμ€ 링크일 λ•Œ */
    a:link { color: orange; }

    /* a μš”μ†Œκ°€ λ°©λ¬Έν•œ 링크일 λ•Œ */
    a:visited { color: green; }

    /* a μš”μ†Œμ— λ§ˆμš°μŠ€κ°€ μ˜¬λΌμ™€ μžˆμ„ λ•Œ */
    a:hover { font-weight: bold; }

    /* a μš”μ†Œκ°€ 클릭된 μƒνƒœμΌ λ•Œ */
    a:active { color: blue; }

    /* text input μš”μ†Œμ™€ password input μš”μ†Œμ— ν¬μ»€μŠ€κ°€ 듀어와 μžˆμ„ λ•Œ */
    input[type=text]:focus,
    input[type=password]:focus {
      color: red;
    }
    </style>
  </head>
<body>
  <a href="#" target="_blank">This is a link</a><br>
  <input type="text" value="I'll be red when focused"><br>
  <input type="password" value="I'll be red when focused">
</body>
</html>
  1. UI μš”μ†Œ μƒνƒœ μ…€λ ‰ν„°
pseudo-classDescription
:checkedμ…€λ ‰ν„°κ°€ 체크 μƒνƒœμΌ λ•Œ
:enabledμ…€λ ‰ν„°κ°€ μ‚¬μš© κ°€λŠ₯ν•œ μƒνƒœμΌ λ•Œ
:disabledμ…€λ ‰ν„°κ°€ μ‚¬μš© λΆˆκ°€λŠ₯ν•œ μƒνƒœμΌ λ•Œ
<!DOCTYPE html>
<html>
<head>
  <style>
    /* input μš”μ†Œκ°€ μ‚¬μš© κ°€λŠ₯ν•œ μƒνƒœμΌ λ•Œ,
       input μš”μ†Œ λ°”λ‘œ 뒀에 μœ„μΉ˜ν•˜λŠ” 인접 ν˜•μ œ span μš”μ†Œλ₯Ό 선택 */
    input:enabled + span {
      color: blue;
    }
    /* input μš”μ†Œκ°€ μ‚¬μš© λΆˆκ°€λŠ₯ν•œ μƒνƒœμΌ λ•Œ,
       input μš”μ†Œ λ°”λ‘œ 뒀에 μœ„μΉ˜ν•˜λŠ” 인접 ν˜•μ œ span μš”μ†Œλ₯Ό 선택 */
    input:disabled + span {
      color: gray;
      text-decoration: line-through;
    }
    /* input μš”μ†Œκ°€ 체크 μƒνƒœμΌ λ•Œ,
       input μš”μ†Œ λ°”λ‘œ 뒀에 μœ„μΉ˜ν•˜λŠ” 인접 ν˜•μ œ span μš”μ†Œλ₯Ό 선택 */
    input:checked + span {
      color: red;
    }
  </style>
</head>
<body>
  <input type="radio" checked="checked" value="male" name="gender"> <span>Male</span><br>
  <input type="radio" value="female" name="gender"> <span>Female</span><br>
  <input type="radio" value="neuter" name="gender" disabled> <span>Neuter</span><hr>

  <input type="checkbox" checked="checked" value="bicycle"> <span>I have a bicycle</span><br>
  <input type="checkbox" value="car"> <span>I have a car</span><br>
  <input type="checkbox" value="motorcycle" disabled> <span>I have a motorcycle</span>
</body>
</html>
  1. ꡬ쑰 가상 클래슀 μ…€λ ‰ν„°
pseudo-classDescription
:first-child셀렉터에 ν•΄λ‹Ήν•˜λŠ” λͺ¨λ“  μš”μ†Œ 쀑 첫번째 μžμ‹μΈ μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
:last-child셀렉터에 ν•΄λ‹Ήν•˜λŠ” λͺ¨λ“  μš”μ†Œ 쀑 λ§ˆμ§€λ§‰ μžμ‹μΈ μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
pseudo-classDescription
:nth-child(n)셀렉터에 ν•΄λ‹Ήν•˜λŠ” λͺ¨λ“  μš”μ†Œ 쀑 μ•žμ—μ„œ n번째 μžμ‹μΈ μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
:nth-last-child(n)셀렉터에 ν•΄λ‹Ήν•˜λŠ” λͺ¨λ“  μš”μ†Œ 쀑 λ’€μ—μ„œ n번째 μžμ‹μΈ μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
pseudo-classDescription
:first-of-type셀렉터에 ν•΄λ‹Ήν•˜λŠ” μš”μ†Œμ˜ λΆ€λͺ¨ μš”μ†Œμ˜ μžμ‹ μš”μ†Œ 쀑 첫번째 λ“±μž₯ν•˜λŠ” μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
:last-of-type셀렉터에 ν•΄λ‹Ήν•˜λŠ” μš”μ†Œμ˜ λΆ€λͺ¨ μš”μ†Œμ˜ μžμ‹ μš”μ†Œ 쀑 λ§ˆμ§€λ§‰μ— λ“±μž₯ν•˜λŠ” μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
:nth-of-type(n)셀렉터에 ν•΄λ‹Ήν•˜λŠ” μš”μ†Œμ˜ λΆ€λͺ¨ μš”μ†Œμ˜ μžμ‹ μš”μ†Œ 쀑 μ•žμ—μ„œ nλ²ˆμ§Έμ— λ“±μž₯ν•˜λŠ” μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
:nth-last-of-type(n)셀렉터에 ν•΄λ‹Ήν•˜λŠ” μš”μ†Œμ˜ λΆ€λͺ¨ μš”μ†Œμ˜ μžμ‹ μš”μ†Œ 쀑 λ’€μ—μ„œ nλ²ˆμ§Έμ— λ“±μž₯ν•˜λŠ” μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
<!DOCTYPE html>
<html>
<head>
  <style>
    /* p μš”μ†Œ μ€‘μ—μ„œ 첫번째 μžμ‹μ„ 선택 */
    p:first-child { color: red; }

    /* p μš”μ†Œ μ€‘μ—μ„œ λ§ˆμ§€λ§‰ μžμ‹μ„ 선택 */
    /* body μš”μ†Œμ˜ λ‘λ²ˆμ§Έ p μš”μ†ŒλŠ” λ§ˆμ§€λ§‰ μžμ‹ μš”μ†Œκ°€ μ•„λ‹ˆλ‹€.
       body μš”μ†Œμ˜ λ§ˆμ§€λ§‰ μžμ‹ μš”μ†ŒλŠ” div μš”μ†Œμ΄λ‹€. */
    p:last-child { color: blue; }
  </style>
</head>
<body>
  <p>This paragraph is the first child of its parent (body).</p>

  <h1>Welcome to My Homepage</h1>
  <p>This paragraph is not the first child of its parent.</p>

  <div>
    <p>This paragraph is the first child of its parent (div).</p>
    <p>This paragraph is not the first child of its parent.</p>
  </div>
</body>
</html>
  1. λΆ€μ • μ…€λ ‰ν„°

    pseudo-classDescription
    :not(μ…€λ ‰ν„°)셀렉터에 ν•΄λ‹Ήν•˜μ§€ μ•ŠλŠ” λͺ¨λ“  μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
    <!DOCTYPE html>
    <html>
    <head>
      <style>
        /* input μš”μ†Œ μ€‘μ—μ„œ type μ–΄νŠΈλ¦¬λ·°νŠΈμ˜ 값이 passwordκ°€ μ•„λ‹Œ μš”μ†Œλ₯Ό 선택 */
        input:not([type=password]) {
          background: yellow;
        }
      </style>
    </head>
    <body>
      <input type="text" value="Text input">
      <input type="email" value="email input">
      <input type="password" value="Password input">
    </body>
    </html>
  2. μ •ν•©μ„± 체크 μ…€λ ‰ν„°

    pseudo-classDescription
    :valid(μ…€λ ‰ν„°)μ •ν•©μ„± 검증이 μ„±κ³΅ν•œ input μš”μ†Œ λ˜λŠ” form μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
    :invalid(μ…€λ ‰ν„°)μ •ν•©μ„± 검증이 μ‹€νŒ¨ν•œ input μš”μ†Œ λ˜λŠ” form μš”μ†Œλ₯Ό μ„ νƒν•œλ‹€.
    <!DOCTYPE html>
    <html>
    <head>
      <style>
        input[type="text"]:valid {
          background-color: greenyellow;
        }
    
        input[type="text"]:invalid {
          background-color: red;
        }
      </style>
    </head>
    <body>
      <label>μž…λ ₯값이 λ°˜λ“œμ‹œ ν•„μš”
        <input type="text" required>
      </label>
      <br>
      <label>특수문자λ₯Ό ν¬ν•¨ν•˜μ§€ μ•ŠλŠ” 4자리 문자 λ˜λŠ” 숫자
        <input type="text" value="ab1!"
          pattern="[a-zA-Z0-9]{4}" required>
      </label>
      <br>
      <label>ν•Έλ“œν° 번호 ν˜•μ‹
        <input type="text" value="010-1111-2222"
          pattern="^\d{3}-\d{3,4}-\d{4}$" required>
      </label>
    </body>
    </html>

가상 μš”μ†Œ μ…€λ ‰ν„°

μš”μ†Œμ˜ νŠΉμ • 뢀뢄에 μŠ€νƒ€μΌμ„ μ μš©ν•˜κΈ° μœ„ν•΄ μ‚¬μš©, νŠΉμ • λΆ€λΆ„μ΄λž€ 예λ₯Ό λ“€μ–΄ λ‹€μŒκ³Ό κ°™λ‹€.

  • μš”μ†Œ μ½˜ν…μΈ μ˜ μ²«κΈ€μž λ˜λŠ” 첫쀄
  • μš”μ†Œ μ½˜ν…μΈ μ˜ μ•ž λ˜λŠ” λ’€

가상 μš”μ†Œμ—λŠ” 두 개의 콜둠(::)을 μ‚¬μš©ν•œλ‹€. CSS ν‘œμ€€μ— μ˜ν•΄ 미리 μ •μ˜λœ 이름이 있기 λ•Œλ¬Έμ— μž„μ˜μ˜ 이름을 μ‚¬μš©ν•  수 μ—†λ‹€.

νŒ¨ν„΄

selector::pseudo-element {
  property:value;
}
pseudo-elementDescription
::first-letterμ½˜ν…μΈ μ˜ μ²«κΈ€μžλ₯Ό μ„ νƒν•œλ‹€.
::first-lineμ½˜ν…μΈ μ˜ 첫쀄을 μ„ νƒν•œλ‹€. 블둝 μš”μ†Œμ—λ§Œ μ μš©ν•  수 μžˆλ‹€.
::afterμ½˜ν…μΈ μ˜ 뒀에 μœ„μΉ˜ν•˜λŠ” 곡간을 μ„ νƒν•œλ‹€. 일반적으둜 content ν”„λ‘œνΌν‹°μ™€ ν•¨κ»˜ μ‚¬μš©λœλ‹€.
::beforeμ½˜ν…μΈ μ˜ μ•žμ— μœ„μΉ˜ν•˜λŠ” 곡간을 μ„ νƒν•œλ‹€. 일반적으둜 content ν”„λ‘œνΌν‹°μ™€ ν•¨κ»˜ μ‚¬μš©λœλ‹€.
::selectionλ“œλž˜κ·Έν•œ μ½˜ν…μΈ λ₯Ό μ„ νƒν•œλ‹€. iOS Safari λ“± 일뢀 λΈŒλΌμš°μ €μ—μ„œ λ™μž‘ μ•ŠλŠ”λ‹€.

<!DOCTYPE html>
<html>
<head>
  <style>
    /* p μš”μ†Œ μ½˜ν…μΈ μ˜ μ²«κΈ€μžλ₯Ό 선택 */
    p::first-letter { font-size: 3em; }
    /* p μš”μ†Œ μ½˜ν…μΈ μ˜ 첫쀄을 선택 */
    p::first-line   { color: red; }

    /* h1 μš”μ†Œ μ½˜ν…μΈ μ˜ μ•ž 곡간에 content μ–΄νŠΈλ¦¬λ·°νŠΈ 값을 μ‚½μž…ν•œλ‹€ */
    h1::before {
      content: " HTML!!! ";
      color: blue;
    }
    /* h1 μš”μ†Œ μ½˜ν…μΈ μ˜ λ’· 곡간에 content μ–΄νŠΈλ¦¬λ·°νŠΈ 값을 μ‚½μž…ν•œλ‹€ */
    h1::after {
      content: " CSS3!!!";
      color: red;
    }

    /* λ“œλž˜κ·Έν•œ μ½˜ν…μΈ λ₯Ό μ„ νƒν•œλ‹€ */
    ::selection {
      color: red;
      background: yellow;
    }
  </style>
</head>
<body>
  <h1>This is a heading</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo illum sunt distinctio sed, tempore, repellat rerum et ea laborum voluptatum! Quisquam error fugiat debitis maiores officiis, tenetur ullam amet in!</p>
</body>
</html>

0개의 λŒ“κΈ€