如何修改复选框的样式(其他input标签类推)

1.传统的方法
原理:通过label来控制复选框的样式

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>利用label实现复选框的样式更改</title>
    <style>
        .checkbox{display: inline-block;width: 30px;height: 30px;background: #563fc2;border-radius: 50%;}
        input{width: 20px;height: 20px;background: #f00;opacity: 0;}

    </style>
</head>
<body>
    <label for="id" class="checkbox"> <input type="checkbox" id="sex"> </label>
    <label for="id" class="checkbox"> <input type="checkbox" id="sex"> </label>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js">  </script>
   <script>    
   $(".checkbox").click(function(){
       if($(this).find("#sex").is(':checked')) {
              $(this).css("background","#ff0");
         }else{
              $(this).css("background","#563fc2");
         }
   });
   </script>
</body>
</html>

2.通过字体图标实现

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>利用label实现复选框的样式更改</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        input[type="checkbox"]{position: relative;background: 0;border: 0;width: 20px;height: 20px;}
        input[type="checkbox"]::before{ display: block; position: absolute; left: 0; top: 0; width: 100%; height: 100%; content: "\f14a"; font-size: 30px;background: #fff; } 
        input[type="checkbox"]:checked::before{display: block;position: absolute;left:0;top:0;width:30px;height: 30px;
          content: "\f046";}

    </style>
</head>
<body>
    <input type="checkbox" id="sex" class="fa">
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值