1
我试图想出一个纯CSS(不使用第三方库)复杂的复选框。只要我必须渲染一个复选框,代码似乎工作正常。但是当我添加第二个复选框时,第二个元素被渲染在第一个元素的顶部。第二个问题涉及标签文本。有任何想法吗 ?渲染两个纯CSS复选框的问题
.checkboxFive {
position: relative;
}
.checkboxFive input {
display: none;
}
.checkboxFive label {
cursor: pointer;
position: absolute;
width: 12px;
height: 12px;
background: #eee;
border: 1px solid #ddd;
}
.checkboxFive label:after {
opacity: 0;
content: '';
position: absolute;
width: 11px;
height: 4px;
background: transparent;
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" name="" />
<label for="checkboxFiveInput">Option 1</label>
</div>
<div class="checkboxFive">
<input type="checkbox" value="1" name="" />
<label for="checkboxFiveInput">Option 2</label>
</div>
这里有一个选项的情况下所期望的效果。
.checkboxFive {
position: relative;
}
.checkboxFive input {
display: none;
}
.checkboxFive label {
cursor: pointer;
position: absolute;
width: 12px;
height: 12px;
background: #eee;
border: 1px solid #ddd;
}
.checkboxFive label:after {
opacity: 0;
content: '';
position: absolute;
width: 11px;
height: 4px;
background: transparent;
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="" />
\t \t <label for="checkboxFiveInput"></label>
\t </div>
这是使用'位置的缺点:绝对的;' – gyre
是的,我知道。但是,如何更新代码以保持所需的效果并同时摆脱此问题? –
这样的事情,https://lokesh-coder.github.io/pretty-checkbox/? –