0

I've styled a checkbox like a button however when the checkbox is checked it doesn't apply the style that I have wrote for the button.

Just wondering what I am doing wrong hopefully it's not too obvious.

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

Here Is a Jfiddle of it too http://jsfiddle.net/pjtmzLy5/1/.

2 Answers2

3

Should be like this;

In css + means what is right next to the previous one. In your case .checkbox:checked + .checklabel. So if checkbox is :checked than style the checklabel;

You need to add default styles for .checklabel so adding height&width inherit should do the trick; than add more styles like justify-content & align-items to center it;

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>

I Would suggest you to remove all the styles from .checkbox since you are hiding it you only need

.checkbox {display: none}
Mike Trinh
  • 1,271
  • 2
  • 9
  • 19
1

Move the checkbox outside of the div tag:

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

The + selector in CSS means .customCheckBox directly following .checkbox:checked.

See this answer: https://stackoverflow.com/a/1139776/1825032

Ewan Roycroft
  • 87
  • 1
  • 1
  • 8