我有一个jsfiddle - https://jsfiddle.net/5qmnptuk/4/
我正在尝试自定义单选按钮.
我已经隐藏了单选按钮,然后创建了新的单选按钮:在标签上.
我正在尝试使用:选中以确定所点击的样式,但它不起作用
任何人都可以看到为什么这不起作用.
.form-group{
margin-top: 30px;
}
label{
cursor: pointer;
display: inline-block;
position: relative;
padding-right: 25px;
margin-right: 15px;
}
label:after{
bottom: -1px;
border: 1px solid #aaa;
border-radius: 25px;
content: "";
display: inline-block;
height: 25px;
margin-right: 25px;
position: absolute;
right: -31px;
width: 25px;
}
input[type=radio]{
display: none;
}
input[type=radio]:checked + label{
content: \2022;
color: red;
font-size: 30px;
text-align: center;
line-height: 18px;
}
Run Code Online (Sandbox Code Playgroud)
您的代码有几个问题:
label在+和~CSS选择器只能选择兄妹之后的DOM元素,所以你需要有标签的前输入.
要分配标签,请使用该for属性,并为输入提供ID:
<div class="form-group">
<input type="radio" id="custom" />
<label for="custom">Label</label>
</div>
Run Code Online (Sandbox Code Playgroud)
input[type=radio]:checked + label{
content: \2022;
color: red;
font-size: 30px;
text-align: center;
line-height: 18px;
}
Run Code Online (Sandbox Code Playgroud)
选择标签,同时:
input[type=radio]:checked + label:after{
content: "\2022";
color: red;
font-size: 30px;
text-align: center;
line-height: 18px;
}
Run Code Online (Sandbox Code Playgroud)
选择after元素