如何改变默认的checkbox样式

我们在项目中使用到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.

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值