我已经将复选框的样式设置为按钮,但是选中该复选框时,它不会应用我为按钮编写的样式。

只是想知道我在做什么错,希望它不是太明显。

HTML

<div class="customCheckBox"style="margin-top:10px; margin: 10px auto;">
        <input class="checkbox" type="checkbox" name="check1" id="check1">
        <label class="checklabel" for="check1">Directors, Managers & Professionals</label>
</div>


的CSS

body {
font-family: 'Source Sans Pro', Arial, sans-serif;
font-size: 14px;
line-height: 1.429;
margin: 0;
padding: 0;
color: #000000;
}

.customCheckBox{
    height: 60px;
    display:flex;
    width:190px;
    color: #702082;
    font-weight: 600;
    border:3px solid #702082;
    position: relative;
    align-items: center;
    transition: all 0.2s;
}

.customCheckBox:hover{
    background-color: #702082;
    color: white;
    -moz-box-shadow: 3px 3px 10px rgba(0, 0, 0,0.25);
    -webkit-box-shadow: 3px 3px 10px rgba(0, 0, 0, .25);
    box-shadow: 3px 3px 10px rgba(0, 0, 0, .25);
}
.checklabel {
    text-align:center;
    font-size:1.1em;
}

.checkbox {
    position:absolute;
    right:-3px;
    top:-3px;
    width: 190px;
    height: 60px;
    -webkit-appearance: none;
    -moz-appearance: none;
    -ms-appearance: none;
    appearance: none;
    border:none;
    padding:0;
    border-radius:0;
    transition:.3s ease;
    outline:0;
}

.checkbox:checked + .customCheckBox {
    background-color: #702082;
    color: white;
}


这也是它的一个小提琴http://jsfiddle.net/pjtmzLy5/1/

最佳答案

应该是这样的;

在css中,+表示上一个旁边的内容。
在您的情况下.checkbox:checked + .checklabel
因此,如果checkbox是:checked而不是checklabel样式;

您需要为.checklabel添加默认样式,因此增加height&width继承应该可以解决问题。而不是添加更多样式,例如justify-content & align-items使其居中;



body {
font-family: 'Source Sans Pro', Arial, sans-serif;
font-size: 14px;
line-height: 1.429;
margin: 0;
padding: 0;
color: #000000;
}

.customCheckBox{
    height: 60px;
    width:190px;
    color: #702082;
    font-weight: 600;
    border:3px solid #702082;
    position: relative;
    align-items: center;
    transition: all 0.2s;
}

.customCheckBox:hover{
    background-color: #702082;
    color: white;
    -moz-box-shadow: 3px 3px 10px rgba(0, 0, 0,0.25);
    -webkit-box-shadow: 3px 3px 10px rgba(0, 0, 0, .25);
    box-shadow: 3px 3px 10px rgba(0, 0, 0, .25);
}
.checklabel {
    display:flex;
    justify-content: center;
    align-items: center;
    text-align:center;
    font-size:1.1em;
    height: inherit;
    width: inherit;
}

.checkbox {
    position:absolute;
    right:-3px;
    top:-3px;
    width: 190px;
    height: 60px;
    -webkit-appearance: none;
    -moz-appearance: none;
    -ms-appearance: none;
    appearance: none;
    border:none;
    padding:0;
    border-radius:0;
    transition:.3s ease;
    outline:0;
}

.checkbox:checked + .checklabel {
    background-color: #702082;
    color: white;
}

<div class="customCheckBox"style="margin-top:10px; margin: 10px auto;">
        <input class="checkbox" type="checkbox" name="check1" id="check1">
        <label class="checklabel" for="check1">Directors, Managers & Professionals</label>
</div>





我建议您从.checkbox中删除​​所有样式,因为您将其隐藏起来只需要

.checkbox {display: none}

09-25 18:53