Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox CSS cannot change background color for the checkbox [duplicate]

I want to change my checkbox to this color #FA9E57. I am using bootstrap v 4.6.

This is my checkbox code:

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/[email protected]/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>
like image 593
Gerry Avatar asked Dec 20 '25 15:12

Gerry


2 Answers

You can use accent-color CSS property to change the background-color of both checkbox and radio buttons.

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

This will only show the color once the input is checked/selected.

like image 99
p.durga shankar Avatar answered Dec 22 '25 09:12

p.durga shankar


You can't modify checkbox color.

Instead, create pseudo elements with background custom background color, like this :

.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/[email protected]/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 element is used to set background color, and :after element contain svg for checkmark.

like image 44
Aryqs Ipsum Avatar answered Dec 22 '25 09:12

Aryqs Ipsum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!