0

input[type="checkbox"]:checked + label:after{
                border: 2px solid red;
                background-color: red;
        }
<div class="input-field col s12">
        <input id="cb1" name="testCheck" type="checkbox" required>
        <label for="cb1">Option 1</label>
</div>

<div class="input-field col s12">
        <input id="cb2" name="testCheck" type="checkbox" required>
        <label for="cb2">Option 2</label>
</div>

<div class="input-field col s12">
    <input id="cb3" name="testCheck" type="checkbox" required>
    <label for="cb3">Option 3</label>
</div>

I have a list of checkboxes

<div class="input-field col s12">
        <input id="cb1" name="testCheck" type="checkbox" required>
        <label for="cb1">Option 1</label>
</div>

<div class="input-field col s12">
        <input id="cb2" name="testCheck" type="checkbox" required>
        <label for="cb2">Option 2</label>
</div>

<div class="input-field col s12">
    <input id="cb3" name="testCheck" type="checkbox" required>
    <label for="cb3">Option 3</label>
</div>

I use the following CSS to change the color of the check boxes when checked:

input[type="checkbox"]:checked + label:after{
                border: 2px solid red;
                background-color: red;
        }

that does not work. Interestingly, the same CSS:

input[type="radio"]:checked + label:after{
                border: 2px solid red;
                background-color: red;
        }

works for radiobutton list.

I want something like that:

enter image description here https://d2d3qesrx8xj6s.cloudfront.net/img/screenshots/68b1d126c5b51eb4267aed2e5fd751db1c72aa1b.jpeg

Brett DeWoody
  • 50,328
  • 25
  • 121
  • 168
Coding Duchess
  • 5,487
  • 11
  • 75
  • 157
  • Are you sure that CSS works for radio inputs? Because you can't actually use sibling selectors and pseudo-elements together. – FluffyKitten Oct 16 '17 at 16:38
  • Possible duplicate of [Can I target a :before or :after pseudo-element with a sibling combinator?](https://stackoverflow.com/questions/7735267/can-i-target-a-before-or-after-pseudo-element-with-a-sibling-combinator) – FluffyKitten Oct 16 '17 at 16:38
  • What are you hoping to accomplish by using `:after` with the `label`? – j08691 Oct 16 '17 at 16:40

3 Answers3

0

If that's the entirety of your code it won't work as is. You'll need to give the :after psuedo element some additional properties. Namely content: "";

Update - In a comment you mentioned you just want to add a border to the checkbox. To do that use outline. Like this:

input[type="checkbox"]:checked {
  outline: 3px solid red;
}
<div class="input-field col s12">
  <input id="cb1" name="testCheck" type="checkbox" required>
  <label for="cb1">Option 1</label>
</div>

<div class="input-field col s12">
  <input id="cb2" name="testCheck" type="checkbox" required>
  <label for="cb2">Option 2</label>
</div>

<div class="input-field col s12">
  <input id="cb3" name="testCheck" type="checkbox" required>
  <label for="cb3">Option 3</label>
</div>
Brett DeWoody
  • 50,328
  • 25
  • 121
  • 168
0

I don't see any purpose in the code you posted for the :after - there is nothing after your label to highlight, so I'm not sure why you are trying to style it.

If you remove it from your rule, the CSS highlights the label in red. I think this is what you are trying to do?

input[type="checkbox"]:checked + label{
     border: 2px solid red;
     background-color: red;
}

Working example:

input[type="checkbox"]:checked + label{
                border: 2px solid red;
                background-color: red;
        }
<div class="input-field col s12">
        <input id="cb1" name="testCheck" type="checkbox" required>
        <label for="cb1">Option 1</label>
</div>

<div class="input-field col s12">
        <input id="cb2" name="testCheck" type="checkbox" required>
        <label for="cb2">Option 2</label>
</div>

<div class="input-field col s12">
    <input id="cb3" name="testCheck" type="checkbox" required>
    <label for="cb3">Option 3</label>
</div>
FluffyKitten
  • 12,639
  • 10
  • 31
  • 45
0

I found a solution that worked for me. I added a class to each checkbox - class="filled-in" and changed my css:

input[type="checkbox"].filled-in:checked + label:after{
    border: 2px solid red;
    background-color: red;
}
Coding Duchess
  • 5,487
  • 11
  • 75
  • 157