Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set background color for unchecked checkbox

Tags:

css

checkbox

I am trying to set the background color of a non-checked checkbox to white and I cannot find a way to do it. It keeps the blue color also when it's checked and also when unchecked. I've tried with:

input[type=checkbox]:not(:checked) {
    background-color: white;
}

and also:

input[type=checkbox]:after { (this one I don't think it's even valid)
    background-color: white;
}

My code for the moment is:

input[type=checkbox] {
  position: relative;
  cursor: pointer;
}

input[type=checkbox]:before {
  content: "";
  display: block;
  position: absolute;
  width: 16px;
  height: 16px;
  top: 0;
  left: 0;
  border: 1px solid #99AFC1;
  border-radius: 3px;
  background-color: #00AEEF;
  padding: 1px;
}

input[type=checkbox]:checked:after {
  content: "";
  display: block;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  top: 2px;
  left: 6px;
}
<div class="container">
  <input type="checkbox" name="test">
</div>

If someone has any ideas, I will appreciate it. Thank you

like image 764
IleNea Avatar asked Jan 30 '26 03:01

IleNea


1 Answers

Here is an example solution based on your code:

input[type=checkbox] {
  position: relative;
  cursor: pointer;
}

input[type=checkbox]:before {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  top: 0;
  left: 0;
  border: 1px solid #99AFC1;
  border-radius: 3px;
  padding: 1px;
  background-color: #FFFFFF;
}

input[type=checkbox]:checked:before {
  background-color: #00AEEF;
}

input[type=checkbox]:checked:after {
  content: "";
  display: block;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  top: 2px;
  left: 6px;
}
<div class="container">
    <input type="checkbox" name="test">
</div>
like image 72
ajobi Avatar answered Feb 02 '26 00:02

ajobi



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!