CSS 의사 요소로 입력 폼 요소 커스터마이징하기
작성일 : 2025.03.12
웹 디자인에서 폼 요소는 사용자 경험에 큰 영향을 미치지만, 기본 스타일은 종종 전체 디자인과 어울리지 않습니다. 다행히도 CSS 의사 요소(Pseudo-Elements)와 몇 가지 기술을 사용하면 HTML 구조를 변경하지 않고도 입력 요소를 완전히 새로운 모습으로 바꿀 수 있습니다. 이 글에서는 다양한 HTML 입력 요소를 커스터마이징하는 방법을 자세히 살펴보겠습니다.
기본 원리: appearance 속성
입력 요소를 커스터마이징하는 첫 번째 단계는 브라우저의 기본 스타일을 제거하는 것입니다. 이를 위해 appearance 속성을 사용합니다:
input {
-webkit-appearance: none; /* Safari, Chrome용 */
-moz-appearance: none; /* Firefox용 */
appearance: none; /* 표준 */
}
이 속성은 브라우저의 기본 UI 컨트롤 스타일을 제거하여 처음부터 스타일링할 수 있게 해줍니다.
체크박스 커스터마이징
체크박스는 의사 요소를 활용한 커스터마이징이 가장 잘 작동하는 입력 요소 중 하나입니다.
기본 사각형 체크박스
[type="checkbox"] {
appearance: none;
width: 20px;
height: 20px;
border: 2px solid #777;
border-radius: 3px;
position: relative;
cursor: pointer;
}
[type="checkbox"]::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
width: 12px;
height: 12px;
background-color: #4a90e2;
border-radius: 1px;
transition: transform 0.2s ease;
}
[type="checkbox"]:checked::before {
transform: translate(-50%, -50%) scale(1);
}
/* 포커스 상태 */
[type="checkbox"]:focus {
outline: 2px solid rgba(74, 144, 226, 0.5);
outline-offset: 2px;
}
토글 스위치 스타일
[type="checkbox"].toggle {
appearance: none;
width: 44px;
height: 24px;
border-radius: 12px;
background-color: #ddd;
position: relative;
cursor: pointer;
transition: background-color 0.3s;
}
[type="checkbox"].toggle::before {
content: "";
position: absolute;
left: 2px;
top: 2px;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: white;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
transition: left 0.3s;
}
[type="checkbox"].toggle:checked {
background-color: #4cd964; /* iOS 스타일 그린 */
}
[type="checkbox"].toggle:checked::before {
left: 22px;
}
체크마크 스타일
[type="checkbox"].checkmark {
appearance: none;
width: 22px;
height: 22px;
border: 2px solid #777;
border-radius: 3px;
position: relative;
cursor: pointer;
}
[type="checkbox"].checkmark::before {
content: "";
position: absolute;
top: 3px;
left: 8px;
width: 6px;
height: 12px;
border: solid transparent;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
transition: border-color 0.2s;
}
[type="checkbox"].checkmark:checked::before {
border-color: #4a90e2;
}
라디오 버튼 커스터마이징
라디오 버튼도 체크박스와 유사한 방식으로 커스터마이징할 수 있습니다.