自定义单选按钮:如何取消选中? (Checkbox Hack)

时间:2017-07-04 09:30:01

标签: css css3 radio-button css-hack

我创建了一些效果很好的自定义复选框:

https://codepen.io/anon/pen/jwxXar

我尝试用单选按钮做同样的事情。但如果一个单选按钮处于活动状态而我点击另一个单选按钮,则取消选中无法正常工作

https://codepen.io/anon/pen/YQLdxd

自定义单选按钮CSS代码

.radio  {
    position: relative;
    margin-bottom: 10px;
}

.radio input[type="radio"] {
    cursor: pointer;
    height: 100%;
    left: 0;
    margin: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 1;
}

.radio label {margin-left: 32px;}

.radio label:before,
.radio label:after {
    content: "";
    display: block;
    position: absolute;
}

.radio label:before {
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 50%;
    height: 20px;
    left: 0;
    top: 4px;
    width: 20px;
}

.radio label:after {
    background: red;
    border-radius: 50%;
    height: 12px;
    left: 5px;
    opacity: 0;
    pointer-events: none;
    top: 9px;
    width: 12px;
}

.radio input:checked ~ label:after {opacity: 1;}

input[type="radio"]:checked + label:before {
    border: 1px solid red;
    box-shadow: inset 0 0 0 1px #fff, inset 0 0 0 0 #fff, inset 0 0 0 16px #fff;
    color: #fff;
}

自定义单选按钮HTMLCode

<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 1</label>
</div>
<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 2</label>
</div>
<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 3</label>
</div>
<div class="radio">
    <input value="" type="radio">
    <label>Radio Button 4</label>
</div>

CSS超级英雄有没有想法,为什么单选按钮不起作用?

2 个答案:

答案 0 :(得分:2)

您必须指定无线电的名称:name =“radios”,该组的所有无线电的名称相同

.radio  {
    position: relative;
    margin-bottom: 10px;
}

.radio input[type="radio"] {
    cursor: pointer;
    height: 100%;
    left: 0;
    margin: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 1;
}

.radio label {margin-left: 32px;}

.radio label:before,
.radio label:after {
    content: "";
    display: block;
    position: absolute;
}

.radio label:before {
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 50%;
    height: 20px;
    left: 0;
    top: 4px;
    width: 20px;
}

.radio label:after {
    background: red;
    border-radius: 50%;
    height: 12px;
    left: 5px;
    opacity: 0;
    pointer-events: none;
    top: 9px;
    width: 12px;
}

.radio input:checked ~ label:after {opacity: 1;}


input[type="radio"]:checked + label:before {
    border: 1px solid red;
    box-shadow: inset 0 0 0 1px #fff, inset 0 0 0 0 #fff, inset 0 0 0 16px #fff;
    color: #fff;
}
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 1</label>
</div>
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 2</label>
</div>
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 3</label>
</div>
<div class="radio">
    <input value="" type="radio" name="radios">
    <label>Radio Button 4</label>
</div>

答案 1 :(得分:1)

你的CSS没什么问题。在收音机的情况下你唯一需要注意的是,单选按钮总是在组中工作。所以你必须给他们起名字(name attribute

<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 1</label>
</div>
<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 2</label>
</div>
<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 3</label>
</div>
<div class="radio">
    <input value="" type="radio" name="rb1">
    <label>Radio Button 4</label>
</div>