html – 选择单选按钮时更改单选按钮标签的颜色?

参见英文答案 > Is there a CSS parent selector?26个
当选择单选按钮时,有没有办法为单选按钮的颜色变化设置标签

我希望标签在悬停状态下与绿色相同,但我似乎无法弄清楚如何实现这一目标?

label.btn span {
  font-size: 16px;
}
i.fa {
  font-size: 24px;
}
i.fa.fa-dot-circle-o {
  font-size: 24px;
}
label input[type="radio"] {
  color: #c8c8c8;
  display: inline;
}
label input[type="radio"] ~ i.fa.fa-dot-circle-o {
  display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-circle-o {
  display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-dot-circle-o {
  color: #78a025;
  display: inline;
  font-size: 24px;
}
label:hover input[type="radio"] ~ i.fa {
  color: #78A025;
}
label input[type="checkBox"] ~ i.fa.fa-square-o {
  color: #c8c8c8;
  display: inline;
}
label input[type="checkBox"] ~ i.fa.fa-check-square-o {
  display: none;
}
label input[type="checkBox"]:checked ~ i.fa.fa-square-o {
  display: none;
}
label input[type="checkBox"]:checked ~ i.fa.fa-check-square-o {
  color: #78A025;
  display: inline;
}
label:hover input[type="checkBox"] ~ i.fa {
  color: #78a025;
}
div[data-toggle="buttons"] label.active {
  color: #78a025;
}
div[data-toggle="buttons"] label {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 14px;
  font-weight: normal;
  line-height: 2em;
  text-align: left;
  white-space: nowrap;
  vertical-align: top;
  cursor: pointer;
  background-color: none;
  border: 0px solid #c8c8c8;
  border-radius: 3px;
  color: #c8c8c8;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}
div[data-toggle="buttons"] label:hover {
  color: #78A025;
}
div[data-toggle="buttons"] label:active,div[data-toggle="buttons"] label.active {
  -webkit-Box-shadow: none;
  Box-shadow: none;
}
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet">

<div class="row">
  <div class="col-xs-12">
    <br>Vertical radio:
    <br>
    <div class="btn-group btn-group-vertical" data-toggle="buttons">
      <label class="btn">
        <input type="radio" name='gender1' checked><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i>  <span>  Male</span>
      </label>
      <label class="btn">
        <input type="radio" name='gender1'><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i><span> Female</span>
      </label>
    </div>

  </div>
</div>

<div class="row">
  <div class="col-xs-12">
    <hr>Vertical checkBox:
    <br>
    <div class="btn-group btn-group-vertical" data-toggle="buttons">
      <label class="btn">
        <input type="checkBox" name='email1' checked><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i>  <span> Marketing Email</span>
      </label>
      <label class="btn">
        <input type="checkBox" name='email2'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Alert Email</span>
      </label>
      <label class="btn">
        <input type="checkBox" name='email3'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Account Email</span>
      </label>
    </div>


  </div>
</div>

这是一个用于演示目的的编码器cdpn.io/ENMMOz

解决方法

您无法设置标签的样式(因为我们在CSS中没有父选择器),但您可以为radiobutton或复选框设置文本样式 – 预期结果 – 使用:
label input[type="radio"]:checked ~ span {
  color: #78a025;
}
label input[type="checkBox"]:checked ~ span {
  color: #78a025;
}

见下面的演示:

label.btn span {
  font-size: 16px;
}
i.fa {
  font-size: 24px;
}
i.fa.fa-dot-circle-o {
  font-size: 24px;
}
label input[type="radio"] {
  color: #c8c8c8;
  display: inline;
}
label input[type="radio"] ~ i.fa.fa-dot-circle-o {
  display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-circle-o {
  display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-dot-circle-o {
  color: #78a025;
  display: inline;
  font-size: 24px;
}
label:hover input[type="radio"] ~ i.fa {
  color: #78A025;
}
label input[type="checkBox"] ~ i.fa.fa-square-o {
  color: #c8c8c8;
  display: inline;
}
label input[type="checkBox"] ~ i.fa.fa-check-square-o {
  display: none;
}
label input[type="checkBox"]:checked ~ i.fa.fa-square-o {
  display: none;
}
label input[type="checkBox"]:checked ~ i.fa.fa-check-square-o {
  color: #78A025;
  display: inline;
}
label:hover input[type="checkBox"] ~ i.fa {
  color: #78a025;
}
div[data-toggle="buttons"] label.active {
  color: #78a025;
}
div[data-toggle="buttons"] label {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 14px;
  font-weight: normal;
  line-height: 2em;
  text-align: left;
  white-space: nowrap;
  vertical-align: top;
  cursor: pointer;
  background-color: none;
  border: 0px solid #c8c8c8;
  border-radius: 3px;
  color: #c8c8c8;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}
div[data-toggle="buttons"] label:hover {
  color: #78A025;
}
div[data-toggle="buttons"] label:active,div[data-toggle="buttons"] label.active {
  -webkit-Box-shadow: none;
  Box-shadow: none;
}
label input[type="radio"]:checked ~ span {
  color: #78a025;
}
label input[type="checkBox"]:checked ~ span {
  color: #78a025;
}
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet">

<div class="row">
  <div class="col-xs-12">
    <br>Vertical radio:
    <br>
    <div class="btn-group btn-group-vertical" data-toggle="buttons">
      <label class="btn">
        <input type="radio" name='gender1' checked><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i>  <span>  Male</span>
      </label>
      <label class="btn">
        <input type="radio" name='gender1'><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i><span> Female</span>
      </label>
    </div>

  </div>
</div>

<div class="row">
  <div class="col-xs-12">
    <hr>Vertical checkBox:
    <br>
    <div class="btn-group btn-group-vertical" data-toggle="buttons">
      <label class="btn">
        <input type="checkBox" name='email1' checked><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i>  <span> Marketing Email</span>
      </label>
      <label class="btn">
        <input type="checkBox" name='email2'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Alert Email</span>
      </label>
      <label class="btn">
        <input type="checkBox" name='email3'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Account Email</span>
      </label>
    </div>


  </div>
</div>

相关文章

操作步骤 1、进入elasticsearch的plugin,进入ik。进入config。 2、在config下面建立以.dic为后缀的字典...
lengend data数据中若存在&#39;&#39;,则表示换行,用&#39;&#39;切割。
代码实现 option = { backgroundColor: &amp;#39;#080b30&amp;#39;, tooltip: { trigger: &...
问题原因 原因在于直接在js中取的变量并复制给var变量。 于是就变成这样。 解决办法 var data = &#...
前言 最近做了一个调查问卷导出的功能,需求是将维护的题目,答案,导出成word,参考了几种方案之后,选...
对于很多人来说,用字符编码都是熟能生巧,而不清楚为什么是那样的字符编码,所以我在这列了一个表,翻...