如何更改bootstrap复选框的颜色?

时间:2016-05-27 23:54:45

标签: css twitter-bootstrap

我有以下HTML代码:

<div class="container">
  <form name="queryForm" class="form-inline text-center">
    <p class="checkbox-inline">
      <input type="checkbox" name="optionsRadios" id="checkOther" value="other" ng-model="formData.other" ng-true-value="'other'" ng-init="formData.other='other'">Other</p>
   </form>
 </div>

,结果是:

enter image description here

将此颜色更改为给定颜色的最简单方法是什么,例如D7B1D7?

5 个答案:

答案 0 :(得分:4)

我已根据您的CodePen调整.squaredThree班级作为示例。如果你想看一个实例,我的答案底部有一个小提琴链接。

这里是更新后的CSS:

/* .squaredThree */
.squaredThree {
  position: relative;
  float:left;
}

.squaredThree label {
  width: 20px;
  height: 20px;
  cursor: pointer;
  position: absolute;
  top: 0;
  left: 0;
  background: #D7B1D7;
  border-radius: 4px;
  box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.4);
}

.squaredThree label:after {
  content: '';
  width: 9px;
  height: 5px;
  position: absolute;
  top: 4px;
  left: 4px;
  border: 3px solid #fcfff4;
  border-top: none;
  border-right: none;
  background: transparent;
  opacity: 0;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

.squaredThree label:hover::after {
  opacity: 0.3;
}

.squaredThree input[type=checkbox] {
  visibility: hidden;
}

.squaredThree input[type=checkbox]:checked + label:after {
  opacity: 1;
}
/* end .squaredThree */

我已将您的HTML更新为包含其他label,因为原始label用作伪复选框。这是您更新的HTML:

<div class="container">
  <form name="queryForm" class="form-inline">
    <div class="squaredThree">
      <input type="checkbox" name="optionsRadios" id="checkOther" value="other" ng-model="formData.other" ng-true-value="'other'" ng-init="formData.other='other'">
      <label for="checkOther"></label>
    </div>
    <label class="label-text">Other</label>
  </form>
</div>

请注意,label .squaredThree之外存在额外的divlabel的类别为.label-text,因为需要一些额外的样式规则才能将文字稍微移到checkbox的右侧:

.label-text {
  position: relative;
  left: 10px;
}

最后,checkbox中支票的大小不是很正确,因此需要额外的规则来纠正这个问题:

*,
::before,
::after {
  box-sizing: initial;
}

Fiddle Demo显示了上述内容。

答案 1 :(得分:2)

如果您使用引导程序,并且需要更改复选框的颜色背景,只需覆盖以下样式即可:

.custom-checkbox .custom-control-input:checked ~ .custom-control-label::before {
  background-color: green!important;
}

.custom-checkbox .custom-control-input:checked:focus ~ .custom-control-label::before {
  box-shadow: 0 0 0 1px #fff, 0 0 0 0.2rem rgba(0, 255, 0, 0.25)
}
.custom-checkbox .custom-control-input:focus ~ .custom-control-label::before {
  box-shadow: 0 0 0 1px #fff, 0 0 0 0.2rem rgba(0, 0, 0, 0.25)
}
.custom-checkbox .custom-control-input:active ~ .custom-control-label::before {
  background-color: #C8FFC8; 
}
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>


    <div class="myTest custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" id="customCheck1">
      <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
    </div>

答案 2 :(得分:0)

input[type=checkbox]没有background-color属性。您可以使用其他方式获得理想的结果:

  1. 您可以使用div中的复选框,然后根据需要设置div的样式。

    <div class="row">
      <input type="checkbox" />
    </div>
    
  2. 您可以使用以下伪元素:

      input[type=checkbox] {
         cursor: pointer;
      }
    
     input[type=checkbox]:after {
      content: " ";
      background-color: #D7B1D7;
      display: inline-block;
      visibility: visible;
     }
    
     input[type=checkbox]:checked:after {
       content: "\2714";
     }
    

答案 3 :(得分:0)

.custom-checkbox .custom-control-input:checked ~ .custom-control-label::before {
  border-color: #D7B1D7;
  background-color: #D7B1D7;
}

答案 4 :(得分:0)

AndrewRIGHT答案的简单版本:

.custom-checkbox .custom-control-input:checked ~ .custom-control-label::before {
    background-color: #333;
    border-color: #333;
}