使css组复选框独立工作

Gho*_*tff 2 html css

我从codepen复制了一个复选框代码.它改变后我的偏好,但问题是,如果单独一个复选框(只有一个检查其上面的一个),它的工作原理是完美的.是否有任何方法可以使ecah中的ecah独立工作,而无需在每次需要时更改类和id.骗子在这里

HTML

<div class="roundedOne">
      <input type="checkbox" value="None" id="roundedOne" name="check" />
      <label for="roundedOne"></label>
</div>
 <div class="roundedOne">
      <input type="checkbox" value="None" id="roundedOne" name="check" />
      <label for="roundedOne"></label>
</div>

    <div class="roundedOne">
      <input type="checkbox" value="None" id="roundedOne" name="check" />
      <label for="roundedOne"></label>
</div>

    <div class="roundedOne">
      <input type="checkbox" value="None" id="roundedOne" name="check" />
      <label for="roundedOne"></label>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.roundedOne {
  width: 20px;
  height: 20px;
  position: relative;
  background: #fcfff4;
  border: 2px solid #77c100;
  border-radius: 3px;
}

.roundedOne label:after {
  content: '';
  width: 14.5px;
  height: 15px;
  position: absolute;
  top: 2px;
  left: 2px;
  background: #77c100;
  opacity: 0;
  border-radius: 3px;
}
.roundedOne label:hover::after {
  opacity: 0.3;
}
.roundedOne input[type=checkbox] {
  visibility: hidden;
}
.roundedOne input[type=checkbox]:checked + label:after {
  opacity: 1;
}
Run Code Online (Sandbox Code Playgroud)

bre*_*njt 5

您的所有复选框都具有相同的ID.而且你的标签for=""都指向同一个id.使它们不同,这应该解决您的问题.您的ID必须不同.您只能在给定页面上拥有特定ID的一个实例