首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何仅用css更改选中单选按钮的标签颜色

如何仅用css更改选中单选按钮的标签颜色
EN

Stack Overflow用户
提问于 2022-02-03 12:00:32
回答 2查看 61关注 0票数 0

当选中特定的单选按钮时,我想更改标签的颜色,但是我做不到,这是我的代码。

html

代码语言:javascript
复制
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="arb" value="arbitration">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="landacq" value="land acquisation">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="comcourt" value="comercial court">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="others" value="others">
<!-- SUB LAW TYPE -->
<article class="civil-sub-law-type-nav">
    <ul class="civil-sub-law-type-ul">
        <li class="civil-sub-law-type-list">
            <label for="arb" class="civil-sub-law-type-label">Arbitration Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="landacq" class="civil-sub-law-type-label">Land Acquisation Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="comcourt" class="civil-sub-law-type-label">Comercial Court Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="others" class="civil-sub-law-type-label">Others</label>
        </li>
    </ul>
</article>

css

代码语言:javascript
复制
.civil-sub-law-type:checked ~ .civil-sub-law-type-label {
  background-color: red;
}

请建议我该怎么做

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-03 12:29:17

首先,您必须明确说明每个输入标签对,通常是通过指定的id。

其次,您必须使用~ *,而不仅仅是~,因为标签不是复选框的同级元素,而是这些兄弟姐妹的后代。

代码语言:javascript
复制
#arb:checked ~ * label[for="arb"] {
  background-color: red;
}

#landacq:checked ~ * label[for="landacq"] {
  background-color: red;
}

#comcourt:checked ~ * label[for="comcourt"] {
  background-color: red;
}

#others:checked ~ * label[for="others"] {
  background-color: red;
}
代码语言:javascript
复制
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="arb" value="arbitration">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="landacq" value="land acquisation">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="comcourt" value="comercial court">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="others" value="others">
<!-- SUB LAW TYPE -->
<article class="civil-sub-law-type-nav">
    <ul class="civil-sub-law-type-ul">
        <li class="civil-sub-law-type-list">
            <label for="arb" class="civil-sub-law-type-label">Arbitration Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="landacq" class="civil-sub-law-type-label">Land Acquisation Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="comcourt" class="civil-sub-law-type-label">Comercial Court Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="others" class="civil-sub-law-type-label">Others</label>
        </li>
    </ul>
</article>

这是一种不同的、更可伸缩的方法,可以计算元素,而不是使用ids。还将选择器分组在一起,因为它们都使用相同的颜色。

代码语言:javascript
复制
.civil-sub-law-type:nth-child(1):checked ~ 
    * .civil-sub-law-type-list:nth-child(1) > label,
.civil-sub-law-type:nth-child(2):checked ~ 
    * .civil-sub-law-type-list:nth-child(2) > label,
.civil-sub-law-type:nth-child(3):checked ~ 
    * .civil-sub-law-type-list:nth-child(3) > label,
.civil-sub-law-type:nth-child(4):checked ~ 
    * .civil-sub-law-type-list:nth-child(4) > label {
  background-color: red;
}
代码语言:javascript
复制
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="arb" value="arbitration">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="landacq" value="land acquisation">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="comcourt" value="comercial court">
<input class="civil-sub-law-type" type="radio" name="civil-sublaw" id="others" value="others">
<!-- SUB LAW TYPE -->
<article class="civil-sub-law-type-nav">
    <ul class="civil-sub-law-type-ul">
        <li class="civil-sub-law-type-list">
            <label for="arb" class="civil-sub-law-type-label">Arbitration Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="landacq" class="civil-sub-law-type-label">Land Acquisation Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="comcourt" class="civil-sub-law-type-label">Comercial Court Cases</label>
        </li>
        <li class="civil-sub-law-type-list">
            <label for="others" class="civil-sub-law-type-label">Others</label>
        </li>
    </ul>
</article>

票数 2
EN

Stack Overflow用户

发布于 2022-02-03 12:18:20

您的标签和输入应该相邻。

代码语言:javascript
复制
.radio:checked + label {
  color: red;
}
代码语言:javascript
复制
<ul>
  <li>
    <input class="radio" name="test" type="radio" />
    <label>
      Test Label
    </label>
  </li>
  <li>
    <input class="radio" name="test" type="radio" />
    <label>
      Test Label 2
    </label>
  </li>
</ul>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70970906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档