我需要仅使用 html 和 CSS 创建自定义复选框。到目前为止我有:
HTML/CSS:
.checkbox-custom {
opacity: 0;
position: absolute;
}
.checkbox-custom,
.checkbox-custom-label {
display: inline-block;
vertical-align: middle;
margin: 5px;
cursor: pointer;
}
.checkbox-custom + .checkbox-custom-label:before {
content: '';
background: #fff;
border-radius: 5px;
border: 2px solid #ddd;
display: inline-block;
vertical-align: middle;
width: 10px;
height: 10px;
padding: 2px;
margin-right: 10px;
text-align: center;
}
.checkbox-custom:checked + .checkbox-custom-label:before {
content: "";
display: inline-block;
width: 1px;
height: 5px;
border: solid blue;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
border-radius: 0px;
margin: 0px 15px 5px 5px;
}
<div>
<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
<label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
<input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
<label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>
checked
复选框应该是一个带有方框的复选标记,而不仅仅是一个复选标记。寻找答案,我只发现了使用 UTF-8 字符或图像显示复选标记的情况。我无法将这些中的任何一个用于我想要完成的事情。我只能使用纯 CSS 和 HTML。有什么建议么?
代码笔在这里: http ://codepen.io/alisontague/pen/EPXagW?editors=110
原文由 alisontague 发布,翻译遵循 CC BY-SA 4.0 许可协议
问题是您对方形边框和复选标记使用了相同的伪元素。简单的解决方案是继续使用
:before
伪元素作为边框,然后使用:after
伪元素作为复选标记。更新示例
您必须 相 对于父元素
.checkbox-custom
标签元素绝对定位:after
伪元素。这是更新后的代码: