简体   繁体   中英

Cannot set background color for unchecked 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

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>

Add following style to your css:

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

JsFiddle LINK

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM