As per your current markup the label
is the parent of the input
and not a sibling but the selector that you are using is a sibling selector (+
) and hence it doesn't select or change the opacity of the label. In CSS, we cannot select and style the parent element based on it's children's state and hence your markup needs to be changed.
Once the markup is changed, we can select and style the label
by using the below selector:
input[type=checkbox]:checked + label {
background: url(http://www.djscimmia.com/new-web/_assets/b615_150px.png);
opacity: 1;
}
The above selector selects the label
which is the adjacent (immediate next) sibling of the check box which is checked and change its opacity
and background-image
.
input[type=checkbox].equipment {
display: none;
}
label {
display: inline-block;
padding: 0 0 0 0px;
-webkit-transition: opacity 0.25s;
-moz-transition: opacity 0.25s;
-o-transition: opacity 0.25s;
transition: opacity 0.25s;
}
.equipment-lab {
height: 150px;
width: 150px;
border-radius: 5px;
}
input[type=checkbox]:checked + label {
background: url(http://www.djscimmia.com/new-web/_assets/b615_150px.png);
opacity: 1;
}
.speaker {
background: url(http://www.djscimmia.com/new-web/_assets/b615_150px.png);
opacity: 0.5;
cursor: pointer;
}
.subwoofer {
background: url(http://www.djscimmia.com/new-web/_assets/b1800hp_150px.png);
opacity: 0.5;
cursor: pointer;
}
.smokemachine {
background: url(http://www.djscimmia.com/new-web/_assets/smoke_150px.png);
opacity: 0.5;
cursor: pointer;
}
<input type="checkbox" name="equipment" value="speaker" id="speaker" class="equipment">
<label class="equipment-lab speaker" for="speaker">
</label>
<input type="checkbox" name="equipment" value="subwoofer" id="subwoofer" class="equipment">
<label class="equipment-lab subwoofer" for="subwoofer">
</label>
<input type="checkbox" name="equipment" value="smoke-machine" id="smoke-machine" class="equipment">
<label class="equipment-lab smokemachine" for="smoke-machine">
</label>
If you want the images for each of them to be different then you can write multiple selectors like below and add the appropriate background
setting to it.
#speaker:checked + label.speaker {
background: url(http://www.djscimmia.com/new-web/_assets/b1800hp_150px.png);
opacity: 1;
}
#subwoofer:checked + label.subwoofer {
background: url(http://www.djscimmia.com/new-web/_assets/smoke_150px.png);
opacity: 1;
}
#smoke-machine:checked + label.smokemachine {
background: url(http://www.djscimmia.com/new-web/_assets/b615_150px.png);
opacity: 1;
}
input[type=checkbox].equipment {
display: none;
}
label {
display: inline-block;
padding: 0 0 0 0px;
-webkit-transition: opacity 0.25s;
-moz-transition: opacity 0.25s;
-o-transition: opacity 0.25s;
transition: opacity 0.25s;
}
.equipment-lab {
height: 150px;
width: 150px;
border-radius: 5px;
}
#speaker:checked + label.speaker {
background: url(http://www.djscimmia.com/new-web/_assets/b1800hp_150px.png);
opacity: 1;
}
#subwoofer:checked + label.subwoofer {
background: url(http://www.djscimmia.com/new-web/_assets/smoke_150px.png);
opacity: 1;
}
#smoke-machine:checked + label.smokemachine {
background: url(http://www.djscimmia.com/new-web/_assets/b615_150px.png);
opacity: 1;
}
.speaker {
background: url(http://www.djscimmia.com/new-web/_assets/b615_150px.png);
opacity: 0.5;
cursor: pointer;
}
.subwoofer {
background: url(http://www.djscimmia.com/new-web/_assets/b1800hp_150px.png);
opacity: 0.5;
cursor: pointer;
}
.smokemachine {
background: url(http://www.djscimmia.com/new-web/_assets/smoke_150px.png);
opacity: 0.5;
cursor: pointer;
}
<input type="checkbox" name="equipment" value="speaker" id="speaker" class="equipment">
<label class="equipment-lab speaker" for="speaker">
</label>
<input type="checkbox" name="equipment" value="subwoofer" id="subwoofer" class="equipment">
<label class="equipment-lab subwoofer" for="subwoofer">
</label>
<input type="checkbox" name="equipment" value="smoke-machine" id="smoke-machine" class="equipment">
<label class="equipment-lab smokemachine" for="smoke-machine">
</label>
For creating a gradual transition between the checked and unchecked states, just add a transition
setting to the label
(this is already there in your snippet but doesn't have much of a visual effect as the duration is very small).
label {
display: inline-block;
padding: 0 0 0 0px;
-webkit-transition: opacity 0.25s;
-moz-transition: opacity 0.25s;
-o-transition: opacity 0.25s;
transition: opacity 0.25s;
}
One thing to note is that only opacity
change can be transitioned and the background
change will be instantaneous because background
is not a transitionable property as per specs. That said you can still use the sprites concept to animate the background-position
and thus produce a gradual change effect. A sample is available here.
(Disclaimer: The image used in that demo is not mine, it was picked from here).
Note: Just in case you have the same misunderstanding as Gene R in comments, I am just showing how to replace images. The images used are just samples and can be replaced with any required image URL.