我们在项目中使用到input标签的checkbox的时候,它的默认样式通常无法满足项目需求,主要是它显得有点小而丑。将它根据项目的需求进行改造就是我们要做的事情了,下面讲两种常用的做法。
1.使用背景图片代替选中效果
主要原理就是将原来的checkbox隐藏:visibility: hidden属性但它依然占据位置,用图片代替它的位置。
优点就是兼容性好
效果图:
代码:
--html--
<div id="check">
<span><input type="checkbox"class="input_check" id="check1"><label for="check1"></label></span>
<span><input type="checkbox"class="input_check" id="check2"><label for="check2"></label></span>
</div>
--css--
#check {margin: 20px auto;}
#check .input_check {position: absolute;width: 20px;height: 20px;visibility: hidden;background: #E92333;}
#check span {position: relative;}
#check .input_check+label {display: inline-block;width: 20px;height: 20px;background: url('入驻申请页面3/入驻申请页面/images/gou.png') no-repeat;background-position: -31px -3px;border: 1px solid skyblue;}
#check .input_check:checked+label {background-position: -7px -4px;}
2.使用css3实现
主要是利用after,缺点是存在兼容性问题。
效果
--html--
<div id="check_css3">
<span><input type="checkbox"class="input_check" id="check3"><label for="check3"></label></span>
<span><input type="checkbox"class="input_check" id="check4"><label for="check4"></label></span>
</div>
--css--
#check_css3 {margin: 20px auto;}
#check_css3 span {position: relative;}
#check_css3 .input_check {position: absolute;visibility: hidden;}
#check_css3 .input_check+label {display: inline-block;width: 16px;height: 16px;border: 1px solid #ffd900;}
#check_css3 .input_check:checked+label:after {content: "";position: absolute;left: 2px;bottom: 12px;width: 9px;height: 4px; border: 2px solid #e92333;border-top-color: transparent;border-right-color: transparent; -ms-transform: rotate(-60deg); -moz-transform: rotate(-60deg); -webkit-transform: rotate(-60deg); transform: rotate(-45deg);}
以上是两种实现方法的介绍,如果你有更好更快捷的方法。 Tell me please.