Change Parent Div on Input[Type=Checkbox]:Checked with CSS

Change parent div on input[type=checkbox]:checked with css

No way to select a parent with CSS only (until CSS4), so you must use JS..

See this post that talking about it here.

How to change color of parent label when checking radio

With much lesser coding and without JavaScript, you can achieve what you are looking for as below.

I have used + css selector to select the sibling label instead of parent. And I am changing the bg-color as required.

If you are planning to apply the background-color for the whole row/including checkbox, you can customise the label/checkbox and it would work as expected too

label {
display: inline-block;
background-color: blue;
padding: 10px;
padding-left: 30px;
width: 200px;
cursor: pointer;
}

input:checked + label {
background-color: green;
}

input {
width: 0;
height: 0;
visibility: hidden;
position: absolute;
}

.answer-radio {
position: relative;
}

label::before,
label::after {
content: "";
display: inline-block;
position: absolute;
border-radius: 50%;
left: 5px;
top: 10px;
}

label::before {
width: 16px;
height: 16px;
border: 1px solid #ddd;
}

label::after {
width: 10px;
height: 10px;
background-color: #ddd;
left: 9px;
top: 14px;
display: none;
}

input:checked + label::after {
display: inline-block;
}
<div class="answer">
<div class="answer-radio">
<input type="radio" class="space" id="answer-1" name="answer-element" />
<label for="answer-1" class="radio-label">True</label>
</div>
<div class="answer-radio">
<input type="radio" class="space" id="answer-2" name="answer-element" />
<label for="answer-2" class="radio-label">False</label>
</div>
</div>

How to change the background of a parent DIV based on the checked state of an INPUT

My thanks to @LGSon. His solution was perfect. Here's the working modification to the HTML and addition to the CSS. I had to add the border and box-shadow elements because Firefox didn't like my pushing the SPAN behind everything, so the SPAN was coming up square, etc.

<div class="slideThree">  
<input type="checkbox" value="None" id="test" name="check" checked />
<label for="test"></label>
<span></span>
</div>

And the CSS...

.slideThree span{
display: block;
position: absolute;
width: 50px;
height: 16px;
background: red;
z-index: 0;
top: 0px;
left: 0px;
border-radius: 8px;
box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
}
.slideThree input[type=checkbox]:checked ~ span { background: green; }

Changing the Style of a specific element by Checkbox:Checked

There is a way to make this happen, but it's to use exactly the same "trick" with which you style the <label> elements, specifically by moving the <input> elements ahead of the element you wish to style.

With that in mind, if the <input> elements are preceding siblings of the <div>, then checking, and unchecking, the <input> can have an effect on the <div>, and also the original <label> elements as well.

As a crude example:

input[type="checkbox"][name^="vehicle"] {
display: absolute;
position: absolute;
opacity: 0;
}

/* styles the <div> based on the checked/unchecked state
of the <input> (this example assumes that the same
highlight colour should be used regardless of which
<input> is checked: */
input[type="checkbox"][name^="vehicle"]:checked ~ div {
background-color: #ccc;
}

/* this is where it becomes obvious that JavaScript (or,
ideally, a CSS selector that can refer to an attribute-
variable) makes more sense; though with a CSS
pre-processor this can be written effectively enough.
Here when the #vehicle1 element is checked the <label>
descendents with a "for" attribute equal to "vehicle1" of
later-sibling <ul> elements are selected and styled: */
#vehicle1:checked~ul label[for=vehicle1] {
background-color: gray;
}

/* as above, for the "vehicle2" id and for attributes: */
#vehicle2:checked~ul label[for=vehicle2] {
background-color: gray;
}

#vehicle3:checked~ul label[for=vehicle3] {
background-color: gray;
}

ul li {
display: inline-block;
padding: 20px;
margin: 20px;
}

ul li label {
padding: 20px;
cursor: pointer;
}
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<div class="modify-box">
BOX TO CHANGED
</div>
<ul>
<li>
<label for="vehicle1"> Bike</label><br>
</li>
<li>
<label for="vehicle2"> Car</label><br>
</li>
<li>
<label for="vehicle3"> Boat</label><br>
</li>
</ul>

How to style the parent label of a checked radio input

A possibility

At my time of posting, I am not exactly sure what the desired layout should be, but there is one specific problem in the attempted CSS that needs to be addressed.

The adjacent siblings selector:

... separates two selectors and matches the second element only if it immediately follows the first element.

If the <input> is a child of the <label>, it isn't adjacent, so while:

label.radio input[type="radio"]:checked + label

is looking for a label immediately following a :checked input inside a label with the class .radio, nothing like that exists.

To alter the styling of the label in this case, would require a selector that affected the parent, which currently isn't possible.

So, to select the label of the :checked input, we need the label to be adjacent, not the parent.

We can use the for="id" attribute:

A <label> can be associated with a control either by placing the control element inside the <label> element, or by using the for attribute.

As I said, I'm not exactly sure what the desired layout should be, but here's an example using the for attribute, that doesn't look too bad.

div {  display: inline-block;  position: relative;}label {  background: #fcb608;  padding: 2px 10px 2px 1.5em;  border: 1px solid transparent; /* keeps layout from jumping */}input {  position: absolute;}input[type="radio"]:checked + label {  background: #000;  border-color: green;  color: white;}
<div>  <input id="id1" type="radio" name="ad_caroserie" value="0">  <label for="id1" class="radio">Berlina</label></div><div>  <input id="id2" type="radio" name="ad_caroserie" value="1">  <label for="id2" class="radio">Break</label></div><div>  <input id="id3" type="radio" name="ad_caroserie" value="2">  <label for="id3" class="radio">Cabrio</label></div>

Apply to style to parent on hover child and if parent is enabled

Try this

& input[type=checkbox]:checked {
color: #007884;
cursor: pointer;
}

https://jsbin.com/hasuwumuxe/edit?html,css



Related Topics



Leave a reply



Submit