blog

가상 요소로 커스텀 라디오 버튼 만들기

Front-end / CSS#CSS#radio#가상 요소#appearance

기본 라디오 버튼 모양을 지우고, 선택됐을 때 가운데 점을 ::before 가상 요소로 그리는 방법이다.

<label>
  <input type="radio" name="plan" class="custom-radio" />
  기본 요금제
</label>
.custom-radio {
  /* 브라우저 기본 모양 제거 */
  appearance: none;
  position: relative; /* ::before의 기준점 */
  width: 20px;
  height: 20px;
  margin: 0;
  border: 1px solid #c4c4c4;
  border-radius: 50%;
  cursor: pointer;
}

.custom-radio:checked {
  border-color: #428dff;
}

.custom-radio:checked::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #428dff;
}

/* 키보드 사용자를 위한 포커스 표시 */
.custom-radio:focus-visible {
  outline: 2px solid #428dff;
  outline-offset: 2px;
}

포인트:

  • appearance: none이 없으면 브라우저 기본 라디오가 그대로 그려지고 가상 요소가 보이지 않는다.
  • ::before를 position: absolute로 가운데 정렬하려면 input에 position: relative가 필요하다.
  • 실제 input을 그대로 쓰기 때문에 키보드 조작, 폼 제출, 스크린 리더 동작이 유지된다. 대신 기본 포커스 링이 사라지므로 :focus-visible 스타일을 직접 넣는다.