JBH
JBH

Reputation: 1958

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

I'm using checkboxes based on Brad Bodine's examples here. I'm thrilled with the abilities his examples gave me, but I do have one question.

I'm using slideThree (see the CSS below). It works like a charm, but I would like to change the background color of the parent DIV based on the position of the slider. I thought that maybe one of the two examples below would do it:

.slideThree input[type=checkbox]:checked { background: green; }
.slideThree input[type=checkbox]:checked + div { background: green; }

But neither one worked. Is it possible to change the background based on the selected state of the INPUT?

HTML

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

CSS

/* .slideThree */
.slideThree {
  width: 80px;
  height: 26px;
  background: #333;
  margin: 20px auto;
  position: relative;
  border-radius: 50px;
  box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
}
.slideThree:after {
  content: 'OFF';
  color: #000;
  position: absolute;
  right: 10px;
  z-index: 0;
  font: 12px/26px Arial, sans-serif;
  font-weight: bold;
  text-shadow: 1px 1px 0px rgba(255, 255, 255, 0.15);
}
.slideThree:before {
  content: 'ON';
  color: #27ae60;
  position: absolute;
  left: 10px;
  z-index: 0;
  font: 12px/26px Arial, sans-serif;
  font-weight: bold;
}
.slideThree label {
  display: block;
  width: 34px;
  height: 20px;
  cursor: pointer;
  position: absolute;
  top: 3px;
  left: 3px;
  z-index: 1;
  background: #fcfff4;
  background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: linear-gradient(to bottom, #fcfff4 0%, #dfe5d7 40%, #b3bead  100%);
  border-radius: 50px;
  -webkit-transition: all 0.4s ease;
  transition: all 0.4s ease;
  box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
}
.slideThree input[type=checkbox] {
  visibility: hidden;
}
.slideThree input[type=checkbox]:checked + label {
  left: 43px;
}

/* end .slideThree */

Upvotes: 0

Views: 2293

Answers (1)

JBH
JBH

Reputation: 1958

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

Upvotes: 3

Related Questions