我制作了一个复选框样式,它使用了 label 标签。见下文
是否可以在没有 label 标签的情况下仍然保留整个复选框的 CSS?所以我只有输入标签,但仍然是 CSS。这是复选框的 CSS。
.control {
font-size: 18px;
position: relative;
display: block;
margin-bottom: 15px;
padding-left: 30px;
cursor: pointer;
}
.control input {
position: absolute;
z-index: -1;
opacity: 0;
}
.control__indicator {
position: absolute;
top: 2px;
left: 0;
width: 20px;
height: 20px;
background: #e6e6e6;
}
.control--radio .control__indicator {
border-radius: 50%;
}
.control:hover input ~ .control__indicator,
.control input:focus ~ .control__indicator {
background: #ccc;
}
.control input:checked ~ .control__indicator {
background: orange;
}
.control:hover input:not([disabled]):checked ~ .control__indicator,
.control input:checked:focus ~ .control__indicator {
background: #ecb53a;
}
.control input:disabled ~ .control__indicator {
pointer-events: none;
opacity: .6;
background: #e6e6e6;
}
.control__indicator:after {
position: absolute;
display: none;
content: '';
}
.control input:checked ~ .control__indicator:after {
display: block;
}
.control--checkbox .control__indicator:after {
top: 4px;
left: 8px;
width: 3px;
height: 8px;
transform: rotate(45deg);
border: solid black;
border-width: 0 2px 2px 0;
}
<label class="control control--checkbox">
<input type="checkbox"/>
<div class="control__indicator"></div>
</label>
我希望你们能帮助我。
原文由 user5838715 发布,翻译遵循 CC BY-SA 4.0 许可协议
================================================ =================
Css 解决方案(适用于 Chrome,不适用于 FF、IE)
您可以使用 css 将自定义样式应用于复选框
:after
,:checked
。请参阅下面的代码或 jsfiddle
HTML
CSS
小提琴 https://jsfiddle.net/guruling/evfr3kk3/
希望这会帮助您应用您的复选框自定义样式。
================================================ =================
新更新 :: 使用 jQuery 的跨浏览器解决方案
HTML
CSS
JS
小提琴 https://jsfiddle.net/guruling/2rsa7ghn/48/