3

I have two problems with CSS. Is there a way set different colors for input radio "YES" and "NO"? For example NO with red backgroud and YES with blue background?

How to delete a space between radios?

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

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
TheMan
  • 87
  • 10

5 Answers5

4

Just add these css in your code :-

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

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

It may help you.

Harsh Sanghani
  • 1,666
  • 1
  • 14
  • 32
1

For no space between radios add

font-size:0px;

to switch-field and for backgrounds add this props

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

UPDATED FIDDLE

Batu.Khan
  • 3,060
  • 2
  • 19
  • 26
1

Checkout working snippet below:

.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;
}
<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>
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
0

As to your first question, yes, using an attribute selector.

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

As to your second question, the gap is causeed by whitespace in the HTML which affects inline/inline-block elements.

Solutions for this can be found in this SO Question

Community
  • 1
  • 1
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

This fiddle solves the color problem.

I added classes to the labels:

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

And used those classes to assign colors:

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

.switch-field input + label.no {
  background-color: red;
}
Mike Covington
  • 2,147
  • 1
  • 16
  • 26