开关按钮上的不同颜色

时间:2015-12-29 08:44:28

标签: html css

CSS有两个问题。有没有办法为输入收音机“YES”和“NO”设置不同的颜色?例如,带有红色背景的“否”和带有蓝色背景的“是”?

如何删除无线电之间的空间?

<div class="switch-field">
   <input type="radio" id="resetyes" name="reset" value="YES"/>
   <label for="resetyes">YES</label>
   <input type="radio" id="resetno" name="reset" value="NO"/>
   <label for="resetno">NO</label>
</div>

的style.css:

.switch-field {
    font-family: Helvetica;
    padding: 10px;
    overflow: hidden;
}
.switch-title {
    margin-bottom: 6px;
}
.switch-field input {
    display: none;
}
.switch-field label {
    display: inline-block;
    width: 60px;
    background-color: #F2F0F0;
    color: rgba(0, 0, 0, 0.6);
    font-size: 14px;
    font-weight: bold;
    text-align: center;
    text-shadow: none;
    padding: 6px 14px;
    border: 1px solid rgba(0, 0, 0, 0.2);
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
    -webkit-transition: all 0.1s ease-in-out;
    -moz-transition: all 0.1s ease-in-out;
    -ms-transition: all 0.1s ease-in-out;
    -o-transition: all 0.1s ease-in-out;
    transition: all 0.1s ease-in-out;
}
.switch-field label:hover {
    cursor: pointer;
}
.switch-field input:checked + label {
    background-color: #5EA8EE;
    -webkit-box-shadow: none;
    box-shadow: none;
}
.switch-field label:first-of-type {
    border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
    border-radius: 0 4px 4px 0;
}

https://jsfiddle.net/t2sof0wd/

5 个答案:

答案 0 :(得分:4)

只需在代码中添加这些css: -

.switch-field input[value="YES"] + label {
  background: green;
}

.switch-field input[value="NO"] + label {
  background: red;
}

它可能对你有帮助。

答案 1 :(得分:1)

无线电之间没有空间添加

font-size:0px;

切换字段和背景添加此道具

#resetyes:checked + label:first-of-type { background-color:green; }
#resetno:checked + label:last-of-type { background-color:red; }

UPDATED FIDDLE

答案 2 :(得分:1)

以下结帐工作片段:

&#13;
&#13;
.switch-field {
  font-family: Helvetica;
  padding: 10px;
  overflow: hidden;
  font-size:0px;
}

.switch-title {
  margin-bottom: 6px;
}

.switch-field input {
  display: none;
}

.switch-field label {
  display: inline-block;
  width: 60px;
  background-color: #F2F0F0;
  color: rgba(0, 0, 0, 0.6);
  font-size: 14px;
  font-weight: bold;
  text-align: center;
  text-shadow: none;
  padding: 6px 14px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  -webkit-transition: all 0.1s ease-in-out;
  -moz-transition:    all 0.1s ease-in-out;
  -ms-transition:     all 0.1s ease-in-out;
  -o-transition:      all 0.1s ease-in-out;
  transition:         all 0.1s ease-in-out;
}

.switch-field label:hover {
    cursor: pointer;
}

.switch-field input:checked + label {
  background-color: #5EA8EE;
  -webkit-box-shadow: none;
  box-shadow: none;
}

.switch-field label:first-of-type {
  border-radius: 4px 0 0 4px;
}

.switch-field label:last-of-type {
  border-radius: 0 4px 4px 0;
}

.switch-field input:checked[value="YES"] + label {
  background-color: green;
}

.switch-field input:checked[value="NO"] + label {
  background-color: red;
}
&#13;
<div class="switch-field">
      <input type="radio" id="resetyes" name="reset" value="YES"/>
      <label for="resetyes">YES</label>
      <input type="radio" id="resetno" name="reset" value="NO"/>
      <label for="resetno">NO</label>
    </div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

关于你的第一个问题,是的,使用属性选择器。

.switch-field input[value="YES"] + label {
  background: green;
}

.switch-field {
  font-family: Helvetica;
  padding: 10px;
    overflow: hidden;
}

.switch-title {
  margin-bottom: 6px;
}

.switch-field input {
  display: none;
}

.switch-field input[value="YES"] + label {
  background: green;
}

.switch-field input[value="NO"] + label {
  background: red;
}

.switch-field label {
  display: inline-block;
  width: 60px;
  background-color: #F2F0F0;
  color: rgba(0, 0, 0, 0.6);
  font-size: 14px;
  font-weight: bold;
  text-align: center;
  text-shadow: none;
  padding: 6px 14px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  -webkit-transition: all 0.1s ease-in-out;
  -moz-transition:    all 0.1s ease-in-out;
  -ms-transition:     all 0.1s ease-in-out;
  -o-transition:      all 0.1s ease-in-out;
  transition:         all 0.1s ease-in-out;
}

.switch-field label:hover {
    cursor: pointer;
}

.switch-field input:checked + label {
  background-color: #5EA8EE;
  -webkit-box-shadow: none;
  box-shadow: none;
}

.switch-field label:first-of-type {
  border-radius: 4px 0 0 4px;
}

.switch-field label:last-of-type {
  border-radius: 0 4px 4px 0;
}
<div class="switch-field">
      <input type="radio" id="resetyes" name="reset" value="YES"/>
      <label for="resetyes">YES</label>
      <input type="radio" id="resetno" name="reset" value="NO"/>
      <label for="resetno">NO</label>
    </div>

关于你的第二个问题,差距是由HTML中的空格引起的,这会影响内联/内联块元素。

此解决方案可在此SO Question

中找到

答案 4 :(得分:0)

This fiddle解决了颜色问题。

我在标签上添加了类:

<label class="yes" for="resetyes">YES</label>
...
<label class="no" for="resetno">NO</label>

并使用这些类来分配颜色:

.switch-field input + label.yes {
  background-color: blue;
}

.switch-field input + label.no {
  background-color: red;
}