테스트 스위치를 어떻게 생성하나요?

CSS를 사용하여 '테스트 스위치'(켜기/끄기 버튼)를 생성하는 방법을 배웁니다.

직접 테스트해 보세요

이중 전환 스위치를 어떻게 만드는가

첫 번째 단계 - HTML 추가:

<!-- 사각 스위치 -->
<label class="switch">
  <input type="checkbox">
  <span class="slider"></span>
</label>
<!-- 원각 스위치 -->
<label class="switch">
  <input type="checkbox">
  <span class="slider round"></span>
</label>

두 번째 단계 - CSS 추가:

/* 스위치 - 슬라이더 주위의 프레임 */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
{}
/* 기본 HTML 체크박스 숨기기 */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
{}
/* 슬라이더 */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
{}
.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
{}
input:checked + .slider {
  background-color: #2196F3;
{}
input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
{}
input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
{}
/* 원각 슬라이더 */
.slider.round {
  border-radius: 34px;
{}
.slider.round:before {
  border-radius: 50%;
{}

직접 테스트해 보세요