【问题标题】:Change Bootstrap 4 checkbox background color更改 Bootstrap 4 复选框背景颜色
【发布时间】:2018-06-30 14:01:55
【问题描述】:

我想知道如何在这个给定的示例中更改 Bootstraps 4 复选框的背景颜色。

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">  

<style>  
.custom-control-label::before,   
.custom-control-label::after {  
top: .8rem;  
width: 1.25rem;  
height: 1.25rem;  
}  
</style>  


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

【问题讨论】:

    标签: html css bootstrap-4


    【解决方案1】:

    编程环境 角 10.0

    预览-

    HTML

    <!-- BLUE CHECKBOX -->
    <div class="custom-control custom-checkbox">
      <input type="checkbox" [checked]="true" class="custom-control-input" [id]="['chk-add',item.id,uniqueTag].join('-')" />
      <label class="custom-control-label" [for]="['chk-add',item.id,uniqueTag].join('-')"></label>
    </div>
    <!-- GREEN CHECKBOX -->
    <div class="custom-control-products custom-checkbox-products">
      <input type="checkbox" [checked]="true" class="custom-control-input-products" [id]="['chk-add',item.id,td.id,uniqueTag].join('-')" />
      <label class="custom-control-label-products" [for]="['chk-add',item.id,td.id,uniqueTag].join('-')"></label>
    </div>
    

    css/css

      .custom-control-products {
        position: relative;
        display: block;
        min-height: 1.5rem;
        padding-left: 1.5rem;
      }
    
      .custom-control-input-products {
        position: absolute;
        left: 0;
        z-index: -1;
        width: 1rem;
        height: 1.25rem;
        opacity: 0;
      }
      
      .custom-control-input-products:checked ~ .custom-control-label-products::before {
        color: #fff;
        border-color: #007bff;
        background-color: #007bff;
      }
      
      .custom-control-input-products:focus ~ .custom-control-label-products::before {
        box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
      }
      
      .custom-control-input-products:focus:not(:checked) ~ .custom-control-label-products::before {
        border-color: #80bdff;
      }
      
      .custom-control-input-products:not(:disabled):active ~ .custom-control-label-products::before {
        color: #fff;
        background-color: #b3d7ff;
        border-color: #b3d7ff;
      }
      
      .custom-control-input-products[disabled] ~ .custom-control-label-products, .custom-control-input-products:disabled ~ .custom-control-label-products {
        color: #6c757d;
      }
      
      .custom-control-input-products[disabled] ~ .custom-control-label-products::before, .custom-control-input-products:disabled ~ .custom-control-label-products::before {
        background-color: #e9ecef;
      }
      
      .custom-control-label-products {
        position: relative;
        margin-bottom: 0;
        vertical-align: top;
      }
      
      .custom-control-label-products::before {
        position: absolute;
        top: 0.25rem;
        left: -1.5rem;
        display: block;
        width: 1rem;
        height: 1rem;
        pointer-events: none;
        content: "";
        background-color: #fff;
        border: #adb5bd solid 1px;
      }
      
      .custom-control-label-products::after {
        position: absolute;
        top: 0.25rem;
        left: -1.5rem;
        display: block;
        width: 1rem;
        height: 1rem;
        content: "";
        background: no-repeat 50% / 50% 50%;
      }
      
      .custom-control-input-products:checked ~ .custom-control-label-products::before {
        color: #fff;
        border-color: #007bff;
        background-color: #17a2b8a8 !important;
      }
    
      .custom-checkbox-products .custom-control-label-products::before {
        border-radius: 0.25rem;
      }
      
      .custom-checkbox-products .custom-control-input-products:checked ~ .custom-control-label-products::after {
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath fill='%23fff' d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26l2.974 2.99L8 2.193z'/%3e%3c/svg%3e");
      }
      
      .custom-checkbox-products .custom-control-input-products:indeterminate ~ .custom-control-label-products::before {
        border-color: #007bff;
        background-color: #007bff;
      }
      
      .custom-checkbox-products .custom-control-input-products:indeterminate ~ .custom-control-label-products::after {
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4' viewBox='0 0 4 4'%3e%3cpath stroke='%23fff' d='M0 2h4'/%3e%3c/svg%3e");
      }
      
      .custom-checkbox-products .custom-control-input-products:disabled:checked ~ .custom-control-label-products::before {
        background-color: rgba(0, 123, 255, 0.5);
      }
      
      .custom-checkbox-products .custom-control-input-products:disabled:indeterminate ~ .custom-control-label-products::before {
        background-color: rgba(0, 123, 255, 0.5);
      }
    

    【讨论】:

      【解决方案2】:

      你可以使用下面的css,让它不勾选的时候变成红色,勾选的时候变成黑色

      .custom-control-label:before{
        background-color:red;
      }
      .custom-checkbox .custom-control-input:checked~.custom-control-label::before{
        background-color:black;
      }
      

      可以通过以下代码更改箭头的颜色

      .custom-checkbox .custom-control-input:checked~.custom-control-label::after{
        background-image:url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='red' d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26 2.974 7.25 8 2.193z'/%3E%3C/svg%3E");
      }
      

      此代码将使刻度变为红色,您可以通过将fill='red' 值更改为您选择的颜色来更改颜色。

      编辑:注意,如果指定 RGB 颜色,例如。 #444444 使用 %23 作为哈希值,例如。 %23444444

      或者你可以使用任何你喜欢的图片来代替。

      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
      
      <style>
          .custom-control-label:before{
              background-color:red;
          }
          .custom-checkbox .custom-control-input:checked~.custom-control-label::before{
              background-color:black;
          }
          .custom-checkbox .custom-control-input:checked~.custom-control-label::after{
              background-image:url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='red' d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26 2.974 7.25 8 2.193z'/%3E%3C/svg%3E");
          }
          .custom-control-input:active~.custom-control-label::before{
              background-color:green;
          }
         
          /** focus shadow pinkish **/
          .custom-checkbox .custom-control-input:focus~.custom-control-label::before{
              box-shadow: 0 0 0 1px #fff, 0 0 0 0.2rem rgba(255, 0, 247, 0.25); 
          }
      </style>  
      
      <div class="custom-control form-control-lg custom-checkbox">  
          <input type="checkbox" class="custom-control-input" id="customCheck1">  
          <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>  
      </div>

      编辑:在来自@cprcrack 的请求后添加了焦点颜色(粉红色)

      【讨论】:

      • 很好,但是改变里面的图标颜色怎么样?
      • 添加了您必须使用的 CSS 才能更改刻度图标
      • 按住鼠标点击时出现的浅蓝色是怎么回事?
      • @shaneparsons 更新了代码 sn-p,现在它应该显示为绿色。
      • 对焦时周围出现的浅蓝色阴影是怎么回事?
      【解决方案3】:

      我想在这里添加一个答案,它是 knetsi 答案的更简单和更通用的版本,适用于那些可能对根据@987654321 进行颜色更改不感兴趣的人@伪类。

      我只是想定义一个类my-error,我可以在复选框中添加或删除它以更改其颜色,在这种情况下以反映错误情况。无论是否选中该框,颜色都保持不变。

      这是它在代码中的样子:

      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
      
      <style>
        .custom-checkbox .custom-control-input.my-error~.custom-control-label::before{
          background-color:orangered;
        }
      </style>
      
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input my-error" id="customCheck1">
        <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-21
        • 1970-01-01
        • 1970-01-01
        • 2020-05-19
        • 2011-08-17
        • 2020-03-06
        • 2015-05-17
        • 1970-01-01
        相关资源
        最近更新 更多