Notice
Recent Posts
Recent Comments
Link
ยซ   2026/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

็„ก์ œ

Pseudo-class (์˜์‚ฌ ํด๋ž˜์Šค) ๋ณธ๋ฌธ

<style>

Pseudo-class (์˜์‚ฌ ํด๋ž˜์Šค)

yo-u-loo 2026. 3. 26. 12:29

 

๐ŸŸข Pseudo-class

HTML ์š”์†Œ์˜ ์ƒํƒœ(state)๋‚˜ ํŠน์ • ์กฐ๊ฑด์„ ๊ธฐ์ค€์œผ๋กœ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์„ ํƒ์ž.

 

1๏ธโƒฃ ์‚ฌ์šฉ์ž์˜ ๋™์ž‘์— ๋ฐ˜์‘ํ•˜๋Š” ๊ฐ€์ƒ ํด๋ž˜์Šค

  • :hover: ํŠน์ • ์š”์†Œ์— ๋งˆ์šฐ์Šค ํฌ์ธํŠธ๋ฅผ ์˜ฌ๋ ค๋†“์œผ๋ฉด ์Šคํƒ€์ผ ์ ์šฉ.
button:hover {
  background-color: blue;
}

 

  • :active: ํŠน์ • ์š”์†Œ๋ฅผ ํด๋ฆญํ•˜๋Š” ์ˆœ๊ฐ„ ์Šคํƒ€์ผ ์ ์šฉ.
button:active {
  background-color: red;
}

 

  • :focus: ํŠน์ • ์š”์†Œ์— ์ดˆ์ ์ด ๋งž์ถ”์–ด์ง€๊ฑฐ๋‚˜, Tab ํ‚ค๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ์Šคํƒ€์ผ ์ ์šฉ.
input:focus {
  outline: 2px solid green;
}

 

 

2๏ธโƒฃ ๋งํฌ ๋ฐ ์š”์†Œ ์ƒํƒœ์— ๋”ฐ๋ฅธ ๊ฐ€์ƒ ํด๋ž˜์Šค

  • :link: ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋งํฌ์— ์Šคํƒ€์ผ ์ ์šฉ.
a:link {
  color: blue;
}

 

  • :visited: ๋ฐฉ๋ฌธํ•œ ๋งํฌ์— ์Šคํƒ€์ผ ์ ์šฉ.
a:visited {
  color: purple;
}

 

  • :enabled: ํ•ด๋‹น ์š”์†Œ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์ผ ๊ฒฝ์šฐ.
button:enabled {
  background: green;
}

 

  • :disabled: ํ•ด๋‹น ์š”์†Œ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ์ผ ๊ฒฝ์šฐ.
button:disabled {
  background: gray;
}

 

  • :checked: ํผ์˜ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์ด ์ฒดํฌ ๋ฐ•์Šค ๋ฒ„ํŠผ์—์„œ ์„ ํƒ๋œ ํ•ญ๋ชฉ์ผ ๊ฒฝ์šฐ.
input:checked {
  accent-color: green;
}

 

 

3๏ธโƒฃ ๋ฌธ์„œ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์ƒ ํด๋ž˜์Šค

/* ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ๋งŒ ๊ธ€์ž ํฌ๊ธฐ ํ‚ค์šฐ๊ธฐ */
li:first-child { font-size: 20px; }

/* ๋ฆฌ์ŠคํŠธ์˜ ์ง์ˆ˜ ๋ฒˆ์งธ ํ•ญ๋ชฉ๋งŒ ๋ฐฐ๊ฒฝ์ƒ‰ ์ž…ํžˆ๊ธฐ */
li:nth-child(2n) { background-color: #f9f9f9; }

/* p ์š”์†Œ ์ค‘ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ์„ ํƒ */
.container p:nth-of-type(1) { background-color: red; }
  • :first-child: ๋ถ€๋ชจ ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ์š”์†Œ ์„ ํƒ.
  • :last-child: ๋ถ€๋ชจ ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ์š”์†Œ ์„ ํƒ.
  • :only-child: ๋ถ€๋ชจ ์š”์†Œ์˜ ์ž์‹ ์š”์†Œ๊ฐ€ ํ•˜๋‚˜์ผ ๋•Œ ์„ ํƒ.
  • :nth-child(n): ๋ถ€๋ชจ ์š”์†Œ์˜ n๋ฒˆ์งธ ์ž์‹ ์š”์†Œ๋ฅผ ์„ ํƒ.
    • 2n: ์ง์ˆ˜. (2, 4, 6, 8, ...)
    • 2n+1: ํ™€์ˆ˜. (1, 3, 5, 7, ...)
    • 3n: 3์˜ ๋ฐฐ์ˆ˜.
    • n+3: 3๋ฒˆ์งธ๋ถ€ํ„ฐ ๋๊นŒ์ง€.
    • odd: ํ™€์ˆ˜ ๋ฒˆ์งธ. (1, 3, 5, 7, ...)
    • even: ์ง์ˆ˜ ๋ฒˆ์งธ. (2, 4, 6, 8, ...)
  • :nth-of-type(n): ๊ฐ™์€ ํƒ€์ž…์˜ ์š”์†Œ๋“ค ์‚ฌ์ด์—์„œ ์ˆœ์„œ๋ฅผ ์ง€์ •.

 

4๏ธโƒฃ ๋ถ€์ •/์กฐ๊ฑด ์„ ํƒ ๊ฐ€์ƒ ํด๋ž˜์Šค

div:not(.active) {
  opacity: 0.5;
}
  • :not(์กฐ๊ฑด): ํŠน์ • ์กฐ๊ฑด์„ ์ œ์™ธํ•˜๋Š” ์„ ํƒ. (์ œ์™ธ)
  • :is(์กฐ๊ฑด): ์—ฌ๋Ÿฌ ์„ ํƒ์ž ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋งž์œผ๋ฉด ์„ ํƒ. (OR ์กฐ๊ฑด)
  • :where(์กฐ๊ฑด): ์—ฌ๋Ÿฌ ์„ ํƒ์ž ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋งž์œผ๋ฉด ์„ ํƒํ•˜์ง€๋งŒ, ์šฐ์„ ์ˆœ์˜๊ฐ€ ํ•ญ์ƒ 0์œผ๋กœ ๋ฎ์–ด์“ฐ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์„ ํƒ์ž. (OR + ์šฐ์„ ์ˆœ์œ„ 0)
  • :has(์กฐ๊ฑด): ํŠน์ • ์ž์‹/ํ›„์† ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ๋ถ€๋ชจ๋ฅผ ์„ ํƒ. (์ž์‹ ์กฐ๊ฑด ๊ธฐ๋ฐ˜ ๋ถ€๋ชจ ์„ ํƒ)

 

 

 

๐Ÿ“โ€Š POINT ๐Ÿ“โ€Š

โœ”๏ธ ๊ฐ€์ƒ ํด๋ž˜์Šค๋Š” ์ฝœ๋ก  ํ•˜๋‚˜(:)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ๋‹ค. โœ”๏ธ

โœ”๏ธ :nth-child ๋Š” ์•„์ฃผ ๊ฐ•๋ ฅํ•œ ์„ ํƒ์ž๋กœ, ๋ณต์žกํ•œ ํ‘œ(Table)๋‚˜ ๋ฆฌ์ŠคํŠธ ์Šคํƒ€์ผ๋ง์— ์œ ์šฉํ•˜๋‹ค. โœ”๏ธ

โœ”๏ธ :not() ์„ ํƒ์ž๋ฅผ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์˜ˆ์™ธ ์ผ€์ด์Šค(์˜ˆ: ๋งˆ์ง€๋ง‰ ์š”์†Œ๋งŒ ์ œ์™ธํ•˜๊ณ  ๋งˆ์ง„ ์ฃผ๊ธฐ)๋ฅผ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. โœ”๏ธ

 

 

๐Ÿ“‹ ์ฃผ์š” ๊ฐ€์ƒ ํด๋ž˜์Šค ์š”์•ฝํ‘œ ๐Ÿ“‹

์ข…๋ฅ˜ ๊ฐ€์ƒ ํด๋ž˜์Šค ์˜๋ฏธ
๋™์  :hover, :active, :focus ์‚ฌ์šฉ์ž์˜ ์›€์ง์ž„์— ๋ฐ˜์‘
๊ตฌ์กฐ :first-child, :last-child, :nth-child ๋ถ€๋ชจ ๋‚ด ์ˆœ์„œ์— ๋”ฐ๋ผ ์„ ํƒ
์ƒํƒœ :checked, :disabled, :required ์š”์†Œ์˜ ์„ค์ • ์ƒํƒœ์— ๋”ฐ๋ผ ์„ ํƒ
๋ถ€์ • :not(์กฐ๊ฑด) ํŠน์ • ์กฐ๊ฑด์ด ์•„๋‹Œ ์š”์†Œ๋งŒ ์„ ํƒ

 


 

โš™๏ธ ์‹ค์ „ ์˜ˆ์ œ ์ฝ”๋“œ: ๋ฐ˜์‘ํ˜• ๋ฆฌ์ŠคํŠธ์™€ ํผ โš™๏ธ

<style>
  /* 1. ๋ฆฌ์ŠคํŠธ ์Šคํƒ€์ผ๋ง */
  ul li { padding: 10px; border-bottom: 1px solid #ddd; }
  ul li:hover { background-color: #eef; cursor: pointer; }
  ul li:first-child { border-top: 2px solid #333; }
  ul li:nth-child(odd) { color: navy; } /* ํ™€์ˆ˜ ๋ฒˆ์งธ๋งŒ ๋‚จ์ƒ‰ */

  /* 2. ์ฒดํฌ๋ฐ•์Šค ๊ฐ•์กฐ */
  input[type="checkbox"]:checked + label {
    font-weight: bold;
    color: green;
  }

  /* 3. ๋น„ํ™œ์„ฑ ์ž…๋ ฅ์ฐฝ */
  input:disabled { background-color: #eee; cursor: not-allowed; }
</style>

<ul>
  <li>์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ (๋‘๊บผ์šด ์„ )</li>
  <li>๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ (๋‚จ์ƒ‰ ์•„๋‹˜)</li>
  <li>์„ธ ๋ฒˆ์งธ ํ•ญ๋ชฉ (๋‚จ์ƒ‰)</li>
  <li>๋„ค ๋ฒˆ์งธ ํ•ญ๋ชฉ (๋‚จ์ƒ‰ ์•„๋‹˜)</li>
</ul>

<input type="checkbox" id="agree">
<label for="agree">์•ฝ๊ด€์— ๋™์˜ํ•ฉ๋‹ˆ๋‹ค.</label>

<input type="text" value="์ˆ˜์ • ๋ถˆ๊ฐ€" disabled>โ€‹

 

'<style>' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

transform ์†์„ฑ๊ณผ functions (ํ•จ์ˆ˜)  (0) 2026.03.26
Pseudo-elements (์˜์‚ฌ ์š”์†Œ)  (0) 2026.03.26
Attribute Selectors  (0) 2026.03.25
Combination Selector  (0) 2026.03.25
Flexbox Layout ์˜ Flex Container ์š”์†Œ  (0) 2026.03.24