更改选中的单选按钮子项(CSS)的标签颜色

时间:2016-09-05 14:45:44

标签: html css css3

我有一个带有不同颜色的单选按钮边框的表单。

在悬停时,我使用" li:nth-​​child(an + b)将单选按钮,复选框和标签的颜色相应更改为围绕它的边框颜色"属性。

问题在于,当我从单选按钮中选择一个选项时(因此单选按钮变为"选中")我无法相应地更改边框颜色的标签颜色。我设法改变除标签颜色以外的所有东西。

小提琴:https://jsfiddle.net/g65wrexa/7/

我的HTML代码:

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

我的CSS代码:

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

到目前为止,我已经尝试过选择单选按钮的标签但它没有工作:

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

我正在寻找一个CSS解决方案。我检查了之前的问题,并希望这不是重复的。

0 个答案:

没有答案