关于单选框和复选框按钮自定义

很多时候 我们不需要浏览器自带的单选框按钮样式  我们想吧单选框按钮图标换成我们自己想要的图标

这里我自己琢磨出来有2种方式可以实现

第一种是用图片来代替单选按钮 通过js来控制图片 这种方法实现起来很简单就不列代码了 主要说第二种

第二种是使用css来控制单选框图标 先将input标签隐藏起来 然后給 label标签添加背景图 完整代码如下 可以找2张宽20的图片来写个demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    
    .zz {
        background: url(./public/images/ic_g.png) no-repeat;
        padding-left: 20px;
        background-position: center left;
    }

    input[type=radio]:checked+label {
        background: url(./public/images/ic_g_selected.png) no-repeat;
        padding-left: 20px;
        background-position: center left;
    }
    
    </style>
</head>
<body>
    <form action="">
        <input type="radio" name="rad" id="wx" style="display: none">
        <label for="wx" class="zz">
            <span>微信支付</span>
        </label>
        <input type="radio" name="rad" id="zf" style="display: none;">
        <label for="zf" class="zz">
            <span>
                支付宝支付
            </span>
        </label>
    </form>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/xiechuanghong/p/9355541.html