I am trying to use CSS to select some elements base on radio
button state.
What I would like to do is if a certain radio
button is checked I want to go down a couple levels and unhide
an overlay so that the user can interact with the disabled radio
buttons.
I know you can use sibling selectors like ~
and >
to select siblings of the current div
, but how would I select a parent element of a checked
radio button and then traverse down the DOM to find my desired selector?
I can easily do this with JS but I would prefer CSS if it is possible.
Here is a CodePen
You can see that I have four radio
buttons. Two of which are indented slightly.
What I would like to accomplish is if someone selects the Testing2 radio
I want the .overlay
that contains the opacity
to be set to display: none
.
I need to also maintain the same structure without reorganizing the HTML.
Using sibling selectors are relatively new to me so I am probably not using them correctly in this case.
I have tried:
#radio-1:checked ~ .overlay {
display: none;
}
#radio-1:checked > .radio-bump > .radio-group-2 ~ .overlay {
display: none;
}
Here is my code:
.overlay {
background: #fff;
z-index: 1;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
opacity: 0.7;
}
.radio-group-2 {
position: relative;
}
.radio-bump {
margin-left: 20px;
}
#radio-1:checked ~ .overlay {
display: none;
}
#radio-1:checked > .radio-bump > .radio-group-2 ~ .overlay {
display: none;
}
<div class="main-container">
<div class="shipping-container">
<div class="radio">
<input type="radio" id="radio" name="radio-group-1">
<label for="radio">Testing1</label>
</div>
<div class="radio-1">
<input type="radio" id="radio-1" name="radio-group-1">
<label for="radio-1">Testing2</label>
</div>
<div class="radio-bump">
<div class="radio-group-2">
<div class="overlay"></div>
<div class="radios">
<input type="radio" id="radio-2" name="radio-group-2">
<label for="radio-2">Testing3</label>
</div>
<div class="radios">
<input type="radio" id="radio-3" name="radio-group-2">
<label for="radio-3">Testing4</label>
</div>
</div>
</div>
</div>
</div>