在JS Fiddle Example之后,我有一个标签和一个复选框输入:
<label for="accept">Accept</label>
<input id="accept" type="checkbox">
我需要设置复选框的样式,所以我尝试了以下操作:
<label for="accept">Accept</label>
<input id="accept" type="checkbox">
在这种情况下,该复选框停止工作,我可以为其设置样式。
然后我交换了Label和Checkbox Input的位置:
<input id="accept" type="checkbox">
<label for="accept">Accept</label>
在这种情况下,它可以正常工作,设置样式,但是输入和标签显示在同一行。
我想设置复选框输入的样式,并在不同行中设置标签和复选框。
CSS如下:
input[type="checkbox"] {
opacity: 0;
}
label {
position: relative;
display: block;
padding-left: 22px;
}
label::before, label::after {
position: absolute;
content: "";
display: block;
}
label::before{
height: 16px;
width: 16px;
border: 1px solid;
left: 0px;
top: 3px;
}
label::after {
height: 5px;
width: 9px;
border-left: 2px solid;
border-bottom: 2px solid;
transform: rotate(-45deg);
left: 4px;
top: 7px;
}
input[type="checkbox"] + label::after {
content: none;
}
input[type="checkbox"]:checked + label::after {
content: "";
}
input[type="checkbox"]:focus + label::before {
outline: rgb(59, 153, 252) auto 5px;
}
注意:在我的示例中,我也无法使复选框与标签垂直对齐。
答案 0 :(得分:0)
form input[type="checkbox"] {
opacity: 0;
}
form .checkicon {
position: relative;
display: block;
height: 16px;
width: 16px;
border: 1px solid;
margin-bottom:5px;
}
form .checkicon::after {
position: absolute;
content: "";
display: block;
}
form .checkicon::after {
height: 5px;
width: 9px;
border-left: 2px solid;
border-bottom: 2px solid;
transform: rotate(-45deg);
left: 3px;
top:3px
}
form input[type="checkbox"] + .checkicon::after {
content: none;
}
form input[type="checkbox"]:checked + .checkicon::after {
content: "";
}
form input[type="checkbox"]:focus + .checkicon::before {
outline: rgb(59, 153, 252) auto 5px;
}
div label {
display: block;
}
h4 { margin: 12px 0; }
<form>
<input id="accept1" type="checkbox">
<label class="checkicon" for="accept1"></label>
<label>Accept</label>
</form>
就这样!希望有帮助!
答案 1 :(得分:0)
JSFiddle链接:https://jsfiddle.net/dh72qb5f/12/
HTML
<div>
<label for="accept1">
<input id="accept1" type="checkbox">
<span class="checkbox-custom"></span>
Accept
</label>
</div>
CSS:
input[type="checkbox"] {
position: absolute;
opacity: 0;
cursor: pointer;
}
div{
position: relative;
}
label {
margin-left: 25px;
}
.checkbox-custom {
position: absolute;
top: 0px;
left: 0px;
height: 12px;
width: 12px;
background-color: transparent;
border-radius: 5px;
border: 2px solid #ccc;
}
.checkbox-custom::after {
position: absolute;
content: "";
left: 12px;
top: 12px;
height: 0px;
width: 0px;
border-radius: 5px;
border: solid #ccc;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(0deg) scale(0);
-ms-transform: rotate(0deg) scale(0);
transform: rotate(0deg) scale(0);
opacity: 1;
}
input:checked ~ .checkbox-custom {
background-color: #ccc;
border-radius: 5px;
transform: rotate(0deg) scale(1);
opacity: 1;
border: 2px solid #ccc;
}
input:checked ~ .checkbox-custom::after {
transform: rotate(45deg) scale(1);
opacity: 1;
left: 3px;
top: 0px;
width: 4px;
height: 8px;
border: solid #ffffff;
border-width: 0 2px 2px 0;
background-color: transparent;
border-radius: 0;
}