【问题标题】:Checkbox CSS cannot change background color for the checkbox复选框 CSS 无法更改复选框的背景颜色
【发布时间】:2022-01-10 14:58:57
【问题描述】:

我想将我的复选框更改为此颜色#FA9E57。 我正在使用 bootstrap v 4.6。

这是我的 CSS 和 html 复选框。

input[type=checkbox]{
    width: 20px;
    height: 20px;
    background-color: #FA9E57;
}

input[type=checkbox]:checked{
    background-color: #FA9E57;
}
   

 <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="register.css">
    <title><%=judul %></title>
    
    <body>
    <div class="form-group my-4">
       <div class="form-check">
         <input class="form-check-input" type="checkbox" id="privacy">
         <label class="form-check-label ml-3" for="privacy">
          Agree privacy
         </label>
       </div>
    </div>
    </body>

非常感谢!

【问题讨论】:

    标签: html css bootstrap-4


    【解决方案1】:

    你可以在css中使用accent-color属性来改变复选框和单选按钮的背景颜色。

    input[type=checkbox] {
      accent-color: red;
    }
    

    【讨论】:

      【解决方案2】:

      试试这个。

      复选框的css和html

      .container {
        display: block;
        position: relative;
        padding-left: 35px;
        margin-bottom: 12px;
        cursor: pointer;
        font-size: 22px;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
      }
      
      /* Hide the browser's default checkbox */
      .container input {
        position: absolute;
        opacity: 0;
        cursor: pointer;
        height: 0;
        width: 0;
      }
      
      /* Create a custom checkbox */
      .checkmark {
        position: absolute;
        top: 0;
        left: 0;
        height: 25px;
        width: 25px;
        background-color: #FA9E57;
      }
      
      /* On mouse-over, add a grey background color */
      .container:hover input ~ .checkmark {
        background-color: #FA9E57;
      }
      
      /* When the checkbox is checked, add a blue background */
      .container input:checked ~ .checkmark {
        background-color: #FA9E57;
      }
      
      /* Create the checkmark/indicator (hidden when not checked) */
      .checkmark:after {
        content: "";
        position: absolute;
        display: none;
      }
      
      /* Show the checkmark when checked */
      .container input:checked ~ .checkmark:after {
        display: block;
      }
      
      /* Style the checkmark/indicator */
      .container .checkmark:after {
        left: 9px;
        top: 5px;
        width: 5px;
        height: 10px;
        border: solid white;
        border-width: 0 3px 3px 0;
        -webkit-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
      }
      <label class="container">Agree privacy
        <input type="checkbox" checked="checked">
        <span class="checkmark"></span>
      </label>

      【讨论】:

        【解决方案3】:

        您不能修改复选框颜色。

        相反,创建具有自定义背景颜色的伪元素,如下所示:

        .form-check {
          position: relative;
        }
        
        input[type=checkbox] {
          width: 20px;
          height: 20px;
        }
        
        input[type=checkbox]:checked+label::before {
          content: "";
          display: block;
          position: absolute;
          text-align: center;
          height: 20px;
          width: 20px;
          left: 0;
          top: 5px;
          background-color: #FA9E57;
          font-family: "Montserrat";
          border-radius: 2px;
          border: 1px solid rgb(150 150 150 / 30%);
        }
        
        input[type=checkbox]:checked+label::after {
          content: url('data:image/svg+xml; utf8, <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="white" viewBox="0 0 24 24"><path d="M20.285 2l-11.285 11.567-5.286-5.011-3.714 3.716 9 8.728 15-15.285z"/></svg>');
          display: block;
          position: absolute;
          left: 3px;
          top: 3px;
        }
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
        <link rel="preconnect" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="register.css">
        
        <div class="form-group my-4">
           <div class="form-check">
             <input class="form-check-input" type="checkbox" id="privacy">
             <label class="form-check-label ml-3" for="privacy">
              Agree privacy
             </label>
           </div>
        </div>

        :before 元素用于设置背景颜色,:after 元素包含 svg 用于勾选标记。

        【讨论】:

        • 感谢您对自定义复选框的回答和帮助
        【解决方案4】:

        请添加一些css代码。

        input[type=checkbox], input[type=checkbox]:checked {
          -moz-appearance:none;
          -webkit-appearance:none;
          -o-appearance:none;
        }
        

        为了让您的代码正常工作,您需要上面的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-17
          • 2015-05-17
          • 2020-05-19
          • 2018-06-30
          • 1970-01-01
          • 2022-12-18
          • 1970-01-01
          相关资源
          最近更新 更多