็ก์
Pseudo-class (์์ฌ ํด๋์ค) ๋ณธ๋ฌธ
๐ข 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 |