CSS Checkbox单独选中的背景颜色

时间:2018-04-30 03:04:50

标签: html css

我正在尝试找到一种方法,在选中时为每个复选框(.option-input)提供自己的背景颜色。

.option-input {
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  -o-appearance: none;
  appearance: none;
  position: relative;
  top: 13.33333px;
  right: 0;
  bottom: 0;
  left: 0;
  height: 40px;
  width: 40px;
  transition: all 0.15s ease-out 0s;
  background: #cbd1d8;
  border: none;
  color: #fff;
  cursor: pointer;
  display: inline-block;
  margin-right: 0.5rem;
  outline: none;
  position: relative;
  z-index: 1000;
}

.option-input:checked {
  background: #40e0d0;
}

.option-input:checked::before {
  height: 40px;
  width: 40px;
  position: absolute;
  content: '';
  display: inline-block;
  font-size: 26.66667px;
  text-align: center;
  line-height: 40px;
}

.option-input:checked::after {
  -webkit-animation: click-wave 0.65s;
  -moz-animation: click-wave 0.65s;
  animation: click-wave 0.65s;
  background: #40e0d0;
  content: '';
  display: block;
  position: relative;
  z-index: 100;
}

.option-input {
  border-radius: 50%;
}

.option-input::after {
  border-radius: 50%;
}
<div class="check-wrap">
  <div class="status-btn">
    <label>
    <input data-trigger="1" type="checkbox" class="option-input blue" checked />
    Checkbox 1
  </label>
  </div>
  <div class="status-btn">
    <label>
    <input data-trigger="2" type="checkbox" class="option-input red" checked />
    Checkbox 2
  </label>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

您可以尝试:

.red {
background: red;
}

.blue {
background: blue;
}

&#13;
&#13;
.option-input {
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  -o-appearance: none;
  appearance: none;
  position: relative;
  top: 13.33333px;
  right: 0;
  bottom: 0;
  left: 0;
  height: 40px;
  width: 40px;
  transition: all 0.15s ease-out 0s;
  background: #cbd1d8;
  border: none;
  color: #fff;
  cursor: pointer;
  display: inline-block;
  margin-right: 0.5rem;
  outline: none;
  position: relative;
  z-index: 1000;
}

.option-input:checked {
  background: #40e0d0;
}

.option-input:checked::before {
  height: 40px;
  width: 40px;
  position: absolute;
  content: '';
  display: inline-block;
  font-size: 26.66667px;
  text-align: center;
  line-height: 40px;
}

.option-input:checked::after {
  -webkit-animation: click-wave 0.65s;
  -moz-animation: click-wave 0.65s;
  animation: click-wave 0.65s;
  background: #40e0d0;
  content: '';
  display: block;
  position: relative;
  z-index: 100;
}

.option-input {
  border-radius: 50%;
}

.option-input::after {
  border-radius: 50%;
}

.red {
background: red;
}

.blue {
background: blue;
}
&#13;
<div class="check-wrap">
  <div class="status-btn">
    <label>
    <input data-trigger="1" type="checkbox" class="option-input blue" checked />
    Checkbox 1
  </label>
  </div>
  <div class="status-btn">
    <label>
    <input data-trigger="2" type="checkbox" class="option-input red" checked />
    Checkbox 2
  </label>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

除非您想要包含JavaScript,否则您可以做的最好的事情是为您想要的每种不同背景颜色添加一个单独的类。

.option-input {
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  -o-appearance: none;
  appearance: none;
  position: relative;
  top: 13.33333px;
  right: 0;
  bottom: 0;
  left: 0;
  height: 40px;
  width: 40px;
  transition: all 0.15s ease-out 0s;
  background: #cbd1d8;
  border: none;
  color: #fff;
  cursor: pointer;
  display: inline-block;
  margin-right: 0.5rem;
  outline: none;
  position: relative;
  z-index: 1000;
}

.blue:checked {
  background: #0000ff;
}
.red:checked {
  background: #ff0000;
}

.option-input:checked::before {
  height: 40px;
  width: 40px;
  position: absolute;
  content: '';
  display: inline-block;
  font-size: 26.66667px;
  text-align: center;
  line-height: 40px;
}

.option-input:checked::after {
  -webkit-animation: click-wave 0.65s;
  -moz-animation: click-wave 0.65s;
  animation: click-wave 0.65s;
  background: #40e0d0;
  content: '';
  display: block;
  position: relative;
  z-index: 100;
}

.option-input {
  border-radius: 50%;
}

.option-input::after {
  border-radius: 50%;
}