I want to change span background image when it's child input is checked, any way to do that using CSS?
<span style='background-image:'url(bla bla)''><input id='child'/>span to be changed</span>
I want to change span background image when it's child input is checked, any way to do that using CSS?
<span style='background-image:'url(bla bla)''><input id='child'/>span to be changed</span>
There is currently no way to select the parent of an element in CSS.
Anyway you can use :has()
pseudo-class, that is still not supported by any browser. .
With :has()
your code will be something like this:
span:has(> #child.checked) { /* styles */ }
But the best thing is to do it with JavaScript.
Here is one trick to achieve the goal without :has
span {
display: inline-block;
width: 200px;
height: 50px;
border: 1px solid red;
background-color: transparent;
position: relative;
}
#child:before {
content: '';
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
#child:checked:before {
background: red;
z-index: -1;
}
<span style='background-image:'url(bla bla)''><input id='child' type="checkbox"/>span to be changed</span>