0

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 */
JBH
  • 1,823
  • 1
  • 19
  • 30
  • There is no _parent selector_ in CSS – Asons Aug 02 '17 at 19:02
  • Would it therefore be possible to modify the CSS to accomodate adding a containter DIV, giving me a "non-parental" context? Or would I still be dealing with a parental context? – JBH Aug 02 '17 at 19:04
  • If it is only a background color you want to change, add one more child element, i.e. a span after the label, make it absolute positioned, z-index of minus -1 and full width/height. Then you can target that with `.slideThree input[type=checkbox]:checked ~ span { background: green; }` – Asons Aug 02 '17 at 19:07
  • @LGSon, that worked like a charm. See my edits for my final CSS, etc. Make that an answer and I'll be happy to raise your reputation. – JBH Aug 02 '17 at 19:27
  • Thanks ...there is a duplicate I haven't found yet, so I will close it as such when I find it, meanwhile, please don't add the solution to your question, post it as a self answer instead. – Asons Aug 02 '17 at 19:29

1 Answers1

3

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; }
JBH
  • 1,823
  • 1
  • 19
  • 30