选中的复选框不会更改CSS

时间:2018-07-25 16:26:30

标签: html css

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

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

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/

2 个答案:

答案 0 :(得分:3)

应该是这样;

在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}

答案 1 :(得分:1)

将复选框移至div标签之外:

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

CSS中的+选择器意味着.customCheckBox 直接跟随 .checkbox:checked

查看此答案:https://stackoverflow.com/a/1139776/1825032