トグルスイッチ デザイン見本
チェックボックスと :checked 疑似クラスで作る、ON / OFF を切り替えるトグルスイッチの実装デモ。コピーしてすぐに使える HTML・CSS・JS のコードを掲載。
HTML・CSS・JSコードプレビューツール にて実装確認ができます。
① 基本のトグルスイッチ(CSSのみ)
スイッチをクリックしてください
<label class="switch">
<input type="checkbox" checked>
<span class="slider"></span>
</label>
.switch {
position: relative;
display: inline-block;
width: 52px;
height: 30px;
}
.switch input {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #cbd5e1;
border-radius: 999px;
cursor: pointer;
transition: background 0.25s ease;
}
.slider::before {
content: "";
position: absolute;
width: 24px;
height: 24px;
left: 3px;
top: 3px;
background: #fff;
border-radius: 50%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
transition: transform 0.25s ease;
}
.switch input:checked + .slider {
background: #667eea;
}
.switch input:checked + .slider::before {
transform: translateX(22px);
}
チェックボックス本体は opacity: 0 で隠し、隣に置いた span をスイッチの見た目として描画する。span の ::before が丸いつまみ(ノブ)になる。input:checked + .slider の隣接セレクタで、チェック時だけ背景色を変え、ノブを translateX で右へずらすことで JavaScript なしに ON / OFF を表現できる。label で全体を囲んでいるため、どこをクリックしても切り替わる。
② サイズ・カラーのバリエーション
<label class="switch sm green">
<input type="checkbox" checked>
<span class="slider"></span>
</label>
<label class="switch lg pink">
<input type="checkbox" checked>
<span class="slider"></span>
</label>
.switch {
position: relative;
display: inline-block;
width: 52px;
height: 30px;
}
.switch input {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #cbd5e1;
border-radius: 999px;
cursor: pointer;
transition: background 0.25s ease;
}
.slider::before {
content: "";
position: absolute;
width: 24px;
height: 24px;
left: 3px;
top: 3px;
background: #fff;
border-radius: 50%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
transition: transform 0.25s ease;
}
.switch input:checked + .slider {
background: #667eea;
}
.switch input:checked + .slider::before {
transform: translateX(22px);
}
.switch.sm {
width: 40px;
height: 24px;
}
.switch.sm .slider::before {
width: 18px;
height: 18px;
}
.switch.sm input:checked + .slider::before {
transform: translateX(16px);
}
.switch.lg {
width: 64px;
height: 36px;
}
.switch.lg .slider::before {
width: 30px;
height: 30px;
}
.switch.lg input:checked + .slider::before {
transform: translateX(28px);
}
.switch.green input:checked + .slider { background: #22c55e; }
.switch.blue input:checked + .slider { background: #3b82f6; }
.switch.pink input:checked + .slider { background: #ec4899; }
サイズを変えるときは、外枠の width / height、ノブの width / height、そしてチェック時の translateX の3つをセットで調整するのがコツ。translateX の値は「外枠の幅 − ノブの幅 − 余白×2」でぴったり右端に収まる。カラーは input:checked + .slider の背景色を差し替えるだけでバリエーションを増やせる。
③ ラベル付きトグル(ON / OFF 表示)
<label class="switch">
<input type="checkbox" checked>
<span class="slider"></span>
</label>
.switch {
position: relative;
display: inline-block;
width: 74px;
height: 30px;
}
.switch input {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #cbd5e1;
border-radius: 999px;
cursor: pointer;
transition: background 0.25s ease;
}
.slider::before {
content: "";
position: absolute;
width: 24px;
height: 24px;
left: 3px;
top: 3px;
background: #fff;
border-radius: 50%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
transition: transform 0.25s ease;
}
.slider::after {
content: "OFF";
position: absolute;
top: 0;
right: 10px;
height: 30px;
line-height: 30px;
font-size: 11px;
font-weight: 700;
color: #64748b;
letter-spacing: 0.05em;
}
.switch input:checked + .slider {
background: #0891b2;
}
.switch input:checked + .slider::after {
content: "ON";
left: 12px;
right: auto;
color: #fff;
}
.switch input:checked + .slider::before {
transform: translateX(44px);
}
スライダーの ::after に content: “OFF” を置いて文字を表示し、input:checked のときは content: “ON” に差し替える。ON のときは文字を左寄せ、OFF のときは右寄せにすると、ノブと文字が重ならず自然に見える。数字だけでなく状態を言葉で示せるので、設定画面などで分かりやすさが増す。
④ 設定パネル(JS で状態を取得)
2 / 3 の設定が有効です
<div class="panel">
<div class="panel-row">
<span>通知を受け取る</span>
<label class="switch">
<input type="checkbox" checked>
<span class="slider"></span>
</label>
</div>
<div class="panel-row">
<span>ダークモード</span>
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
</div>
</div>
<p class="readout"></p>
.panel {
max-width: 300px;
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 6px 16px;
}
.panel-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 0;
border-bottom: 1px solid #eef2f7;
}
.panel-row:last-child {
border-bottom: none;
}
.switch {
position: relative;
display: inline-block;
width: 52px;
height: 30px;
flex-shrink: 0;
}
.switch input {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #cbd5e1;
border-radius: 999px;
cursor: pointer;
transition: background 0.25s ease;
}
.slider::before {
content: "";
position: absolute;
width: 24px;
height: 24px;
left: 3px;
top: 3px;
background: #fff;
border-radius: 50%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
transition: transform 0.25s ease;
}
.switch input:checked + .slider {
background: #7c3aed;
}
.switch input:checked + .slider::before {
transform: translateX(22px);
}
(function() {
var inputs = document.querySelectorAll('.panel .switch input');
var readout = document.querySelector('.readout');
if (!readout) return;
function refresh() {
var onNum = 0;
inputs.forEach(function(inp) {
if (inp.checked) onNum++;
});
readout.textContent = onNum + ' / ' + inputs.length + ' の設定が有効です';
}
inputs.forEach(function(inp) {
inp.addEventListener('change', refresh);
});
refresh();
})();
見た目の切り替えは CSS に任せ、JavaScript は各チェックボックスの checked プロパティを読むだけでよい。change イベントで有効な数を数え直し、表示テキストを更新している。実際のアプリでは、この値をサーバーへ保存したり localStorage に書き込んだりして、設定として利用する。
当サイトで公開しているWebデザインやUIの実装例は、一覧として以下記事に纏めています。

