
用css怎么写开关
1、首先创建开关按钮的html结构,input框具有选中未选中的效果,label具有关联input的特性,因此可以很方便的为label设置样式模拟开关的效果。
<div class="switch-box">
<div class="bg_con">
<input id="checked_1" type="checkbox" class="switch" value="0" />
<label for="checked_1"></label>
</div>
</div> 2、编写css,使用伪类元素:bofore和after设计开关样式。(推荐学习:CSS视频教程)
.switch-box{
width:42px;
}
.switch-box .switch{
display:none;
}
.switch-box label{
position:relative;
display: block;
padding: 1px;
border-radius: 24px;
height: 22px;
margin-bottom: 15px;
background-color: #eee;
cursor: pointer;
vertical-align: top;
-webkit-user-select: none;
}
.switch-box label:before{
content: '';
display: block;
border-radius: 24px;
height: 22px;
background-color: white;
-webkit-transform: scale(1, 1);
-webkit-transition: all 0.3s ease;
}
.switch-box label:after{
content: '';
position: absolute;
top: 50
left: 50
margin-top: -11px;
margin-left: -11px;
width: 22px;
height: 22px;
border-radius: 22px;
background-color: white;
box-shadow: 1px 1px 1px 1px rgba(0,0,0,0.08);
-webkit-transform: translateX(-9px);
-webkit-transition: all 0.3s ease;
}
.switch-box .switch:checked + label:after{
-webkit-transform: translateX(9px);
}
.switch-box .switch:checked + label:before{
background-color:#de0b23;
}效果:

知识点:
1、label中的for跟input的id绑定。作用是在点击label时选中input或者取消选中input
2、(:checked + 紧邻其后面标签) 的选择器。例如:.switch:checked + label 解释:当class为switch的checked为选中状态时,就选择紧邻其后的label标签。
更多CSS相关技术文章,请访问CSS3答疑栏目进行学习!
以上就是用css怎么写开关的详细内容,更多请关注易知道|edz.cc其它相关文章!













