纯CSS复杂的问题

时间:2017-03-06 21:03:51

标签: css

我正在努力想出一个纯粹的css花式复选框。而且我已经成功实现了90%的目标,唯一的例外是我看到刺激性的标准chechbox出现在我喜欢的复选框下面。我无法摆脱它。有什么帮助吗?

.checkboxFive {
  width: 12px;
  margin: 12px 100px;
  position: relative;
}

.checkboxFive label {
  cursor: pointer;
  position: absolute;
  width: 12px;
  height: 12px;
  top: 0;
  left: 0;
  background: #eee;
  border: 1px solid #ddd;
}

.checkboxFive label:after {
  opacity: 0;
  content: '';
  position: absolute;
  width: 11px;
  height: 4px;
  background: transparent;
  top: 1px;
  left: 3px;
  border: 2px solid #333;
  border-top: none;
  border-right: none;
  transform: rotate(-45deg);
}

.checkboxFive label:hover::after {
  opacity: 0.5;
}

.checkboxFive input[type=checkbox]:checked + label:after {
  opacity: 1;
}
<div class="checkboxFive">
  <input type="checkbox" value="1" id="checkboxFiveInput" name="" />
  <label for="checkboxFiveInput"></label>
</div>

3 个答案:

答案 0 :(得分:2)

向checkboxFiveInput添加display:none

.checkboxFive {
  width: 12px;
  margin: 12px 100px;
  position: relative;
}

.checkboxFive input {
   display:none;
 }

.checkboxFive label {
  cursor: pointer;
  position: absolute;
  width: 12px;
  height: 12px;
  top: 0;
  left: 0;
  background: #eee;
  border: 1px solid #ddd;
}

.checkboxFive label:after {
  opacity: 0;
  content: '';
  position: absolute;
  width: 11px;
  height: 4px;
  background: transparent;
  top: 1px;
  left: 3px;
  border: 2px solid #333;
  border-top: none;
  border-right: none;
  transform: rotate(-45deg);
}

.checkboxFive label:hover::after {
  opacity: 0.5;
}

.checkboxFive input[type=checkbox]:checked + label:after {
  opacity: 1;
}
<div class="checkboxFive">
  <input type="checkbox" value="1" id="checkboxFiveInput" name="" />
  <label for="checkboxFiveInput"></label>
</div>

答案 1 :(得分:2)

您可以使用position和coordonates从屏幕中删除它。

displayvisibility可能会成为辅助功能问题)

&#13;
&#13;
.checkboxFive {
  width: 12px;
  margin: 12px 100px;
  position: relative;
}

.checkboxFive label {
  cursor: pointer;
  position: absolute;
  width: 12px;
  height: 12px;
  top: 0;
  left: 0;
  background: #eee;
  border: 1px solid #ddd;
}

.checkboxFive label:after {
  opacity: 0;
  content: '';
  position: absolute;
  width: 11px;
  height: 4px;
  background: transparent;
  top: 1px;
  left: 3px;
  border: 2px solid #333;
  border-top: none;
  border-right: none;
  transform: rotate(-45deg);
}

.checkboxFive label:hover::after {
  opacity: 0.5;
}
#checkboxFiveInput {
position:absolute;
left:-9999px;
}
.checkboxFive input[type=checkbox]:checked + label:after {
  opacity: 1;
}
&#13;
<div class="checkboxFive">
  <input type="checkbox" value="1" id="checkboxFiveInput" name="" />
  <label for="checkboxFiveInput"></label>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

创建自定义复选框的第一步是将visibility: hidden添加到input元素。在您的情况下,您的CSS将如下所示:

.checkboxFive #checkboxFiveInput {
    visibility: hidden;
}

有关构建自定义复选框的详细信息,可以看到此link