0

I have a form with differently colored radio buttons borders.

On hover I change the color of the radio button, the check circle and the label accordingly to the border color surrounding it using the "li:nth-child(an+b)" property.

The problem is that when I select an option from the radio buttons (so the radio button becomes "checked") I cannot change the color of the label correspondingly to the border color. I managed to change everything except the label color.

The Fiddle: https://jsfiddle.net/g65wrexa/7/

My HTML code:

         <form>
                    <ul class="MyForm" id="radioButtons">
                        <li>
                            <label for="editor_mole">
                                <input type="radio" id="editor_mole" name="selector" value="M">Opt1
                                <div class="check"></div>
                            </label>
                        </li>

                        <li>
                            <label>
                                <input type="radio" id="editor_hair" name="selector" value="H">Opt2
                                <div class="check"></div>
                            </label>

                        </li>

                        <li>
                            <label>
                                <input type="radio" id="editor_pore" name="selector" value="P">Opt3
                                <div class="check"></div>
                            </label>
                        </li>

                        <li>
                            <label>
                                <input type="radio" id="editor_vessel" name="selector" value="v"> Opt4
                                <div class="check"></div>
                            </label>
                        </li>

                        <li>
                            <label>
                                <input type="radio" id="editor_black_pen" name="selector" value="black_pen"> Opt5
                                <div class="check"></div>
                            </label>
                        </li>
                    </ul>
                </form>

My CSS Code:

.form ul {
    list-style: none;
    display: none;
    height: 100%;
    width: 100%;
    margin: 10px;
    padding: 0px;
}
ul.MyForm li {
    color: #4169E1;
    display: inline-block;
    position: relative;
    float: left;
    width: 100%;
    height: 50px;
}
ul.MyForm li input[type=radio] {
    position: absolute;
    visibility: hidden;
}
ul.MyForm li label {
    display: inline-block;
    position: relative;
    float: left;
    font-weight: 500;
    font-size: 1.1em;
    padding: 10px 20px 25px 27px;
    margin-bottom: 2px auto;
    height: 1px;
    width: 100px;
    z-index: 9;
    border: 2px solid green;
    border-width: medium;
    cursor: pointer;
    -webkit-transition: all 0.25s linear;
}
ul.MyForm li:nth-child(2) label {
    border-color: #800000;
}
ul.MyForm li:nth-child(3) label {
    border-color: #00CED1;
}
ul.MyForm li:nth-child(4) label {
    border-color: red;
}
ul.MyForm li:nth-child(5) label {
    border-color: black;
}
ul.MyForm li .check {
    display: block;
    position: absolute;
    border: 3px solid #4169E1;
    border-radius: 80%;
    height: 20px;
    width: 20px;
    top: 11px;
    left: 1px;
    z-index: 5;
    transition: border .25s linear;
    -webkit-transition: border .25s linear;
}
ul.MyForm li:nth-child(1):hover .check {
    border: 3px solid green;
}
ul.MyForm li:nth-child(2):hover .check {
    border: 3px solid #800000;
}
ul.MyForm li:nth-child(3):hover .check {
    border: 3px solid #00CED1;
}
ul.MyForm li:nth-child(4):hover .check {
    border: 3px solid red;
}
ul.MyForm li:nth-child(5):hover .check {
    border: 3px solid black;
}
ul.MyForm li:nth-child(1):hover label {
    color: green;
}
ul.MyForm li:nth-child(2):hover label {
    color: #800000;
}
ul.MyForm li:nth-child(3):hover label {
    color: #00CED1;
}
ul.MyForm li:nth-child(4):hover label {
    color: red;
}
ul.MyForm li:nth-child(5):hover label {
    color: black;
}
ul.MyForm li .check::before {
    display: block;
    position: absolute;
    content: '';
    border-radius: 100%;
    height: 8px;
    width: 8px;
    top: 1px;
    right: 1px;
    left: 1.05px;
    bottom: 1.05px;
    margin: auto;
    transition: background 0.25s linear;
    -webkit-transition: background 0.25s linear;
}


ul.MyForm li:nth-child(1) input[type=radio]:checked~.check {
    border: 3px solid green;
}
ul.MyForm li:nth-child(2) input[type=radio]:checked~.check {
    border: 3px solid #800000;
}
ul.MyForm li:nth-child(3) input[type=radio]:checked~.check {
    border: 3px solid #00CED1;
}
ul.MyForm li:nth-child(4) input[type=radio]:checked~.check {
    border: 3px solid red;
}
ul.MyForm li:nth-child(5) input[type=radio]:checked~.check {
    border: 3px solid black;
}


ul.MyForm li:nth-child(1) input[type=radio]:checked~.check::before {
    background: green;
}
ul.MyForm li:nth-child(2) input[type=radio]:checked~.check::before {
    background: #800000;
}
ul.MyForm li:nth-child(3) input[type=radio]:checked~.check::before {
    background: #00CED1;
}
ul.MyForm li:nth-child(4) input[type=radio]:checked~.check::before {
    background: red;
}
ul.MyForm li:nth-child(5) input[type=radio]:checked~.check::before {
    background: black;
}

ul.MyForm li:nth-child(1) input[type=radio]:checked+ label {
    color: green;
}

ul.MyForm li:nth-of-type(1) input[type="radio"]:checked + label {
  color: green;
}

What I've tried so far to select the labels of the radio buttons and it didn't work:

ul.MyForm li:nth-child(1) input[type=radio]:checked+ label {
        color: green;
    }

    ul.MyForm li:nth-of-type(1) input[type="radio"]:checked + label {
      color: green;
    }

I am looking for a CSS solution. I checked the previous questions, and hope that this is not a duplicate.

RMS
  • 1,021
  • 2
  • 11
  • 28
  • @J.Titus : "this" being the solution I tried but didn't work or something from here: http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector ? cause I've tried and didn't work so maybe I didn't get right what you are suggesting. – RMS Sep 05 '16 at 15:44
  • I edited the fiddle and changed the html structure and it was working for at least the first radio button (I think that's the only one you tried setting up with your updated CSS?) – J. Titus Sep 05 '16 at 15:48
  • @J.Titus: yep, it worked brilliantly! this is the solution. – RMS Sep 05 '16 at 15:56