因为原生的checkbox样式比较难看,因此咱们常常须要改写它的样式,美化复选框,因此今天总结下自定义checkbox样式的方法,上代码:css
html部分html
<label class="checkbox-inline">
<input type="checkbox" class="checkbox" name="hobby">
<span class="hobby">羽毛球</span>
<span class="checkmark"></span>
</label>
<label class="checkbox-inline">
<input type="checkbox" class="checkbox" name="hobby">
<span class="hobby">跑步</span>
<span class="checkmark"></span>
</label>
复制代码
css部分css3
.checkbox-inline{
position: relative;
}
.checkbox{
position: absolute;
width: 0;
height: 0;
}
.checkmark{
position: absolute;
top: 2px;
left: 0;
height: 15px;
width: 15px;
border: 1px solid;
background-color: #fff;
border-radius: 10px;
}
.hobby{
padding-left: 20px;
}
.checkbox-inline input:checked ~ .checkmark {
background-color: #492c94;
}
.checkbox-inline input:checked ~ .checkmark:after {
display: block;
}
.checkbox-inline .checkmark:after {
display: none;
content: "";
position: absolute;
left: 4px;
top: 0;
width: 4px;
height: 10px;
border: solid white;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
复制代码
效果图(未选中与选中对比):web
采起的作法是bash
这里还涉及到一个知识点,伪元素属于主元素的一部分,所以点击伪元素触发的是主元素的click事件。ui