【JS不要、コピペで1分!】CSSだけで作るトグルボタン

この記事を読むのに必要な時間は約 32 分です。

簡単!Javascript不要!CSSだけで作るトグルボタンの紹介です。
ソースをコピペするだけで即使えます。

デモの紹介

まずはサンプル用のデモです。シンプルで汎用性の高いトグルボタンです。
クリックする度にボタンのON・OFFが切り替わります。

ソース

以下のソースをコピペすると使えます。
実装に1分かかりません。

HTML

<div class="switch">
    <label class="switch__label">
        <input type="checkbox" class="switch__input"/>
        <span class="switch__content"></span>
        <span class="switch__circle"></span>
    </label>
</div>

CSS

.switch__label {
    width: 50px;
    position: relative;
    display: inline-block;
}
.switch__content {
    display: block;
    cursor: pointer;
    position: relative;
    border-radius: 30px;
    height: 31px;
    overflow: hidden;
}
.switch__content:before {
    content: "";
    display: block;
    position: absolute;
    width: calc(100% - 3px);
    height: calc(100% - 3px);
    top: 0;
    left: 0;
    border: 1.5px solid #E5E5EA;
    border-radius: 30px;
    background-color: #fff;
}
.switch__content:after {
    content: "";
    display: block;
    position: absolute;
    background-color: transparent;
    width: 0;
    height: 0;
    top: 50%;
    left: 50%;
    border-radius: 30px;
    -webkit-transition: all .5s;
       -moz-transition: all .5s;
        -ms-transition: all .5s;
         -o-transition: all .5s;
            transition: all .5s;
}
.switch__input {
    display: none;
}
.switch__circle {
    display: block;
    top: 2px;
    left: 2px;
    position: absolute;
    -webkit-box-shadow: 0 2px 6px #999;
            box-shadow: 0 2px 6px #999;
    width: 27px;
    height: 27px;
    -webkit-border-radius: 20px;
            border-radius: 20px;
    background-color: #fff;
    -webkit-transition: all .5s;
       -moz-transition: all .5s;
        -ms-transition: all .5s;
         -o-transition: all .5s;
            transition: all .5s;
}
.switch__input:checked ~ .switch__circle {
    left: 21px;
}
.switch__input:checked ~ .switch__content:after {
    background-color: #00c4cc;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
.switch2__label {
    width: 44px;
    position: relative;
    display: inline-block;
}
.switch2__content {
    display: block;
    cursor: pointer;
    position: relative;
    border-radius: 30px;
    height: 20px;
    -webkit-transition: all .1s .4s;
       -moz-transition: all .1s .4s;
        -ms-transition: all .1s .4s;
         -o-transition: all .1s .4s;
            transition: all .1s .4s;
    overflow: hidden;
}
.switch2__content:before {
    content: "";
    display: block;
    position: absolute;
    width: calc(100% - 4px);
    height: calc(100% - 4px);
    top: 0;
    left: 0;
    border: 1.5px solid #000000;
    border-radius: 20px;
    background-color: #fff;
}
.switch2__content:after {
    content: "";
    display: block;
    position: absolute;
    background-color: transparent;
    width: 0;
    height: 100%;
    top: 0;
    left: 0;
    border-radius: 20px;
    -webkit-transition: all .5s;
       -moz-transition: all .5s;
        -ms-transition: all .5s;
         -o-transition: all .5s;
            transition: all .5s;
}
.switch2__input {
    display: none;
}
.switch2__circle {
    display: block;
    top: 5px;
    left: 5px;
    position: absolute;
    width: 10px;
    height: 10px;
    -webkit-border-radius: 10px;
            border-radius: 10px;
    background-color: #000000;
    -webkit-transition: all .5s;
       -moz-transition: all .5s;
        -ms-transition: all .5s;
         -o-transition: all .5s;
            transition: all .5s;
}
.switch2__input:checked ~ .switch2__circle {
    left: 29px;
    background-color: #fff;
}
.switch2__input:checked ~ .switch2__content {
    border-color: transparent;
    -webkit-transition: all 0s;
       -moz-transition: all 0s;
        -ms-transition: all 0s;
         -o-transition: all 0s;
            transition: all 0s;
}
.switch2__input:checked ~ .switch2__content:after {
    background-color: #0078D7;
    width: 100%;
}
.switch3__label {
    width: 37px;
    position: relative;
    display: inline-block;
    padding-top: 3px;
}
.switch3__content {
    display: block;
    cursor: pointer;
    position: relative;
    border-radius: 7px;
    height: 14px;
    background-color: rgba(34,31,31,.26);
    -webkit-transition: all .1s .4s;
       -moz-transition: all .1s .4s;
        -ms-transition: all .1s .4s;
         -o-transition: all .1s .4s;
            transition: all .1s .4s;
    overflow: hidden;
}
.switch3__content:after {
    content: "";
    display: block;
    position: absolute;
    
    width: 0;
    height: 100%;
    top: 0;
    left: 0;
    border-radius: 7px;
    -webkit-transition: all .5s;
       -moz-transition: all .5s;
        -ms-transition: all .5s;
         -o-transition: all .5s;
            transition: all .5s;
}
.switch3__input {
    display: none;
}
.switch3__circle {
    display: block;
    top: 0px;
    left: 0px;
    position: absolute;
    width: 20px;
    height: 20px;
    -webkit-border-radius: 10px;
            border-radius: 10px;
    background-color: #F1F1F1;
    -webkit-transition: all .5s;
       -moz-transition: all .5s;
        -ms-transition: all .5s;
         -o-transition: all .5s;
            transition: all .5s;
    -webkit-box-shadow: 0 2px 2px #ccc;
            box-shadow: 0 2px 2px #ccc;
}
.switch3__input:checked ~ .switch3__circle {
    left: 18px;
    background-color: #009688;
}
.switch3__input:checked ~ .switch3__content {
    border-color: transparent;
    -webkit-transition: all 0s;
       -moz-transition: all 0s;
        -ms-transition: all 0s;
         -o-transition: all 0s;
            transition: all 0s;
}
.switch3__input:checked ~ .switch3__content:after {
    background-color: rgba(0,150,136,.5);
    width: 100%;
}

おわりに

以上、シンプルなトグルボタンをCSSだけで作る方法の紹介でした。

ではまた!