css3利用表单伪类:checked自定义单选按钮

总结:为所有被选中的 input 元素直接兄弟元素span设置背景色:

input:checked +span{ background-color: pink; }

小点:点击label就会触发input控件,所以可以把label设置溢出隐藏把input藏起来,内部再设置span标签绝对定位填充

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        label{
            position: relative;
            display: inline-block;
            width: 100px;
            height: 100px;
            border: 1px solid;
            border-radius: 50% 50%;
            overflow: hidden;
        }
        label>input{
            position: absolute;
            left: -50%;
            top: -50%;
        }
        label>span{
            display: block;
            position: absolute;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
        }
        input:checked +span{
            background-color: pink;
        }
    </style>
</head>
<body>
<label><input type="radio" name="testInput"><span></span></label>
<label><input type="radio" name="testInput"><span></span></label>
<label><input type="radio" name="testInput"><span></span></label>
</body>
</html>

原文地址:https://www.cnblogs.com/threeyou/p/13460881.html