所以我一直在读,css中没有父选择器(这有点奇怪)。仅使用CSS选中复选框时,这是否表示我无法更改.fake-checkbox
上的背景吗?
代码看起来像这样:
<div class="checkbox">
<span class="fake-checkbox" id="chkbx">
<input type="checkbox" />
<svg class="svg svg-icon" viewBox="0 0 20 20">
<path d="M7.97832489 13.4912738 3.56842362 9.01683836 2 10.6248752 8.17428734 17 18 4.49099934 16.1436545 3z"></path>
</svg>
</span>
<label for="chkbx">Recurring object</label>
</div>
理想的选择器将是:
input:checked < span { background-color: green; }
可以将<input type="checkbox"/>
从<span>
中移出:
.selector * {
box-sizing: border-box;
}
.selector {
display: inline-block;
}
.selector input {
position: absolute;
min-width: 1em;
width: 100%;
height: 100%;
z-index: 2;
opacity: 0;
margin: 0;
padding: 0;
cursor: pointer;
}
.selector .checkbox {
display: flex;
}
.selector .checkbox label {
display: inline-block;
}
.fake-checkbox {
position: relative;
height: 21px;
width: 21px;
border: solid 1px #Fff;
display: inline-block;
border-radius: 3px;
}
.selector .checkbox.p-is-hover,
.selector .checkbox.p-is-indeterminate {
display: none;
}
/* SVG style */
.selector.p-svg .checkbox .svg {
width: 16px;
height: 16px;
align-items: center;
border: 0;
opacity: 0;
fill: #fff;
}
.selector.p-svg input:checked ~ span .svg {
opacity: 1;
}
.selector .checkbox.left .fake-checkbox,
.selector .checkbox.left .fake-checkbox {
left: 0;
}
.selector .checkbox.right .fake-checkbox,
.selector .checkbox.right .fake-checkbox {
right: 0;
}
.selector .checkbox.left label {
margin-left: 8px;
}
.selector .checkbox.right {
flex-direction: row-reverse;
}
.selector .checkbox.right label {
margin-right: 8px;
}
input:checked ~ span {
background-color: green;
}
body {
color: #fff;
background-color: #1D1E22
}
<div class="selector p-svg">
<div class="checkbox left">
<input type="checkbox" />
<span class="fake-checkbox" id="chkbx">
<svg class="svg svg-icon">
<path d="M7.97832489 13.4912738 3.56842362 9.01683836 2 10.6248752 8.17428734 17 18 4.49099934 16.1436545 3z">
</path>
</svg>
</span>
<label for="chkbx">Recurring object</label>
</div>
</div>