css教程网 css教程网365css.cn
公告为您打开互联网世界的窗户关于本站

开关按钮

checkbox + ::before 滑块,点击区域足够大。

结构

HTML
<label class="sw"><input type="checkbox" checked><i></i><span>通知</span></label>

样式

CSS
.sw {
  display: inline-flex; align-items: center; gap: 9px;
  font: 13px/1 -apple-system, "PingFang SC", sans-serif;
  color: #2f3846; cursor: pointer;
}
.sw input { display: none; }
.sw i {
  width: 40px; height: 23px; border-radius: 999px;
  background: #d3dae6; position: relative;
  transition: background .2s;
}
.sw i::after {
  content: ""; position: absolute;
  left: 3px; top: 3px;
  width: 17px; height: 17px; border-radius: 50%;
  background: #fff; transition: translate .2s;
  box-shadow: 0 1px 3px rgb(0 0 0 / .2);
}
.sw input:checked + i { background: #2b57ff; }
.sw input:checked + i::after { translate: 17px 0; }

复制后可直接使用;如果要放进已有项目,注意把类名改成你项目里的命名,避免冲突。