2

I have several containers with radio buttons. I want a container of a selected radio button to become selected:

input[type="radio"]:checked+.container {
  border: 5px solid red;
}
<div class="container">
  <input class="btn-radio" type="radio" value="123" name="rd" />
</div>


<div class="container">
  <input class="btn-radio" type="radio" value="1234" name="rd" />
</div>


<div class="container">
  <input class="btn-radio" type="radio" value="12345" name="rd" />
</div>

Why doesn't it work?

Ivan
  • 34,531
  • 8
  • 55
  • 100
Ojman
  • 51
  • 4
  • This might help you https://stackoverflow.com/questions/43589287/change-border-color-of-radio-button-when-selected – Naomi Jun 10 '18 at 14:01
  • actually your are selecting and element with class `.container` immediate next to `input` but in your html its the parent element. you might want to use additional element next to input and can use `+` for selecting it and add your style accordingly. :) – MJN Jun 10 '18 at 14:22
  • @Manjunath how to select the parent ".container"? – Ojman Jun 10 '18 at 15:38
  • in css you can't select bt you can do with jQuery or JavaScript add active class on container when input is checked – MJN Jun 10 '18 at 18:45

3 Answers3

2

Here is the working code which I commented above

input[type="radio"]:checked + .border {
    border: 5px solid red;
}

.container {
    position: relative;
    max-width: 100px;
    margin: 10px;
}

input {
    position: relative;
    z-index: 100;
}

.border {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
}
<div class="container">
    <input class="btn-radio" type="radio" value="123" name="rd" />Input 1
    <div class="border"></div>
</div>
<div class="container">
    <input class="btn-radio" type="radio" value="1234" name="rd" />Input 2
    <div class="border"></div>
</div>
<div class="container">
   <input class="btn-radio" type="radio" value="12345" name="rd" /> Input 3
     <div class="border"></div>
</div>
Rajat Jain
  • 1,339
  • 2
  • 16
  • 29
MJN
  • 610
  • 1
  • 7
  • 19
1

Here is a solution without new elements, you can use the ::before of the input to make the border on the container. Good Luck!

.container {
  position: relative;
  margin-bottom: 2em;
}

input[type="radio"]:checked::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border: 5px solid red;
}
    <div class="container">
      <p>container 123</p>
      <input class="btn-radio" type="radio" value="123" name="rd" />
    </div>


    <div class="container">
    <p>container 1234</p>
      <input class="btn-radio" type="radio" value="1234" name="rd" />
    </div>


    <div class="container">
    <p>container 12345</p>
      <input class="btn-radio" type="radio" value="12345" name="rd" />
    </div>
T04435
  • 12,507
  • 5
  • 54
  • 54
0

Use id insted of that. Give them individual id.

#id:checked + .container {
    border: 5px solid red;;
}

For better result use the id for container as well.

Ivan
  • 34,531
  • 8
  • 55
  • 100
Rajat Jain
  • 1,339
  • 2
  • 16
  • 29