4.1 ์์ฌ ํด๋์ค :checked
CSS์ ํผ ์์ฌ ํด๋์ค๋ ํผ ์์์ ์ํ์ ๋ฐ๋ผ ์คํ์ผ์ ์ค์ ํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
์ด ์์ฌ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ์ฉ์ ์ธํฐํ์ด์ค์ ๊ฒฝํ์ ํฅ์์ํค๋ ์ธํฐ๋ํฐ๋ธํ๊ณ ์ ๊ทผ ๊ฐ๋ฅํ ํผ์ ๋ง๋ค ์ ์์ต๋๋ค.
์ด์ :checked
, :disabled
, :enabled
, :invalid
์์ฌ ํด๋์ค๋ฅผ ์์ธํ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
:checked ์์ฌ ํด๋์ค๋ "์ ํ๋" ์ํ์ ํผ ์์์ ์ ์ฉ๋ฉ๋๋ค. ์ด๊ฒ์ ์ฒดํฌ๋ฐ์ค(<input type="checkbox">
), ๋ผ๋์ค ๋ฒํผ(<input type="radio">
), ์ต์
(<option>
)์ ํด๋นํฉ๋๋ค.
๊ตฌ๋ฌธ:
์
๋ ํฐ:checked {
์์ฑ: ๊ฐ;
}
์ฌ์ฉ ์์
์ด ์์ ์์๋ ์ ํ๋ ์ฒดํฌ๋ฐ์ค, ๋ผ๋์ค ๋ฒํผ ๋ฐ ์ต์ ์ด "์ ํ๋จ" ์ํ์ผ ๋ ์ ์ ํ ๋ฐฐ๊ฒฝ์์ ๋ฐ์ต๋๋ค:
/* ์ ํ๋ ์ฒดํฌ๋ฐ์ค ์คํ์ผ๋ง */
input[type="checkbox"]:checked {
background-color: #3498db;
}
/* ์ ํ๋ ๋ผ๋์ค ๋ฒํผ ์คํ์ผ๋ง */
input[type="radio"]:checked {
background-color: #2ecc71;
}
/* ์ ํ๋ ์ต์
์คํ์ผ๋ง */
option:checked {
background-color: #e74c3c;
}
4.2 ์์ฌ ํด๋์ค :disabled
:disabled
์์ฌ ํด๋์ค๋ ๋นํ์ฑํ ์ํ์ ํผ ์์์ ์ ์ฉ๋ฉ๋๋ค. ์ด๋ ์ผ์์ ์ผ๋ก ์ ๊ทผํ ์ ์๋ ์์๋ฅผ ์คํ์ผ๋งํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
๊ตฌ๋ฌธ:
์
๋ ํฐ:disabled {
์์ฑ: ๊ฐ;
}
์ฌ์ฉ ์์
์ด ์์ ์์๋ ๋นํ์ฑํ๋ ์ ๋ ฅ ํ๋์ ๋ฒํผ์ด ์ ์ ํ ๋ฐฐ๊ฒฝ์, ํ ๋๋ฆฌ ๋ฐ ํ ์คํธ ์์์ ๋ฐ์ผ๋ฉฐ, ์ปค์๋ฅผ "๊ธ์ง๋จ"์ผ๋ก ๋ณ๊ฒฝํฉ๋๋ค:
/* ๋นํ์ฑํ๋ ์
๋ ฅ ํ๋ ์คํ์ผ๋ง */
input:disabled {
background-color: #f0f0f0;
border-color: #ccc;
color: #888;
}
/* ๋นํ์ฑํ๋ ๋ฒํผ ์คํ์ผ๋ง */
button:disabled {
background-color: #d0d0d0;
color: #777;
cursor: not-allowed;
}
4.3 ์์ฌ ํด๋์ค :enabled
:enabled
์์ฌ ํด๋์ค๋ ํ์ฑํ๋ ํผ ์์์ ์ ์ฉ๋ฉ๋๋ค.
์ด๋ ์ ๊ทผ ๊ฐ๋ฅํ ์์๋ฅผ ์คํ์ผ๋งํ ์ ์๊ฒ ํด์ค๋๋ค.
๊ตฌ๋ฌธ:
์
๋ ํฐ:enabled {
์์ฑ: ๊ฐ;
}
์ฌ์ฉ ์์
์ด ์์ ์์๋ ํ์ฑํ๋ ์ ๋ ฅ ํ๋์ ๋ฒํผ์ด ์ ์ ํ ๋ฐฐ๊ฒฝ์, ํ ๋๋ฆฌ ๋ฐ ํ ์คํธ ์์์ ๋ฐ์ผ๋ฉฐ ์ปค์๋ฅผ "ํฌ์ธํฐ"๋ก ๋ณ๊ฒฝํฉ๋๋ค:
/* ํ์ฑํ๋ ์
๋ ฅ ํ๋ ์คํ์ผ๋ง */
input:enabled {
background-color: #fff;
border-color: #3498db;
}
/* ํ์ฑํ๋ ๋ฒํผ ์คํ์ผ๋ง */
button:enabled {
background-color: #2ecc71;
color: white;
cursor: pointer;
}
4.4 ์์ฌ ํด๋์ค :invalid
:invalid
์์ฌ ํด๋์ค๋ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํต๊ณผํ์ง ๋ชปํ ํผ ์์์ ์ ์ฉ๋ฉ๋๋ค. ์ด๋
์๋ชป๋ ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๋ ์
๋ ฅ ํ๋๋ฅผ ์คํ์ผ๋งํ ์ ์๊ฒ ํด์ค๋๋ค.
๊ตฌ๋ฌธ:
์
๋ ํฐ:invalid {
์์ฑ: ๊ฐ;
}
์ฌ์ฉ ์์
์ด ์์ ์์๋ ์๋ชป๋ ์ ๋ ฅ ํ๋์ ํ ์คํธ ํ๋๊ฐ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํต๊ณผํ์ง ๋ชปํ ๊ฒ์ ์๊ฐ์ ์ผ๋ก ๋ณด์ฌ์ฃผ๊ธฐ ์ํด ์ ์ ํ ํ ๋๋ฆฌ์ ๋ฐฐ๊ฒฝ์์ ๋ฐ์ต๋๋ค:
/* ์๋ชป๋ ์
๋ ฅ ํ๋ ์คํ์ผ๋ง */
input:invalid {
border-color: #e74c3c;
background-color: #f9e0e0;
}
/* ์๋ชป๋ ํ
์คํธ ํ๋ ์คํ์ผ๋ง */
textarea:invalid {
border-color: #e74c3c;
background-color: #f9e0e0;
}
4.5 ์ ์ฒด ๊ตฌํ ์์
/* ์ ํ๋ ์ฒดํฌ๋ฐ์ค ์คํ์ผ๋ง */
input[type="checkbox"]:checked {
background-color: #3498db;
}
/* ์ ํ๋ ๋ผ๋์ค ๋ฒํผ ์คํ์ผ๋ง */
input[type="radio"]:checked {
background-color: #2ecc71;
}
/* ๋นํ์ฑํ๋ ์
๋ ฅ ํ๋ ์คํ์ผ๋ง */
input:disabled {
background-color: #f0f0f0;
border-color: #ccc;
color: #888;
}
/* ๋นํ์ฑํ๋ ๋ฒํผ ์คํ์ผ๋ง */
button:disabled {
background-color: #d0d0d0;
color: #777;
cursor: not-allowed;
}
/* ํ์ฑํ๋ ์
๋ ฅ ํ๋ ์คํ์ผ๋ง */
input:enabled {
background-color: #fff;
border-color: #3498db;
}
/* ํ์ฑํ๋ ๋ฒํผ ์คํ์ผ๋ง */
button:enabled {
background-color: #2ecc71;
color: white;
cursor: pointer;
}
/* ์๋ชป๋ ์
๋ ฅ ํ๋ ์คํ์ผ๋ง */
input:invalid {
border-color: #e74c3c;
background-color: #f9e0e0;
}
/* ์๋ชป๋ ํ
์คํธ ํ๋ ์คํ์ผ๋ง */
textarea:invalid {
border-color: #e74c3c;
background-color: #f9e0e0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ํผ ์์ฌ ํด๋์ค ์์</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<label>
<input type="checkbox" name="option1"> Option 1
</label>
<br>
<label>
<input type="radio" name="choice" value="1"> Choice 1
</label>
<label>
<input type="radio" name="choice" value="2"> Choice 2
</label>
<br>
<input type="text" placeholder="Enter your name" required>
<br>
<input type="email" placeholder="Enter your email" required>
<br>
<button type="submit">Submit</button>
<button type="button" disabled>Disabled Button</button>
</form>
</body>
</html>
GO TO FULL VERSION