Home > Net >  Changing color of the checkbox
Changing color of the checkbox

Time:03-24

I am working on a project and I need the background of the checkbox to be yellow and the color of the tick to be white.

        <div >
             <input type="checkbox" name="disclaimer" id="rad-1" required="" />
             <label for="rad-1">Yes, please!</label>
        </div>

This is my HTML and The styling for it is written below

#rad-1 {
        height: 20px;
        width: 20px;
        accent-color: yellow;
    }

The background becomes yellow but the color of tick becomes black I have tried "Color: white;" and "background-color:white;" but none of these work and the tick mark stays black.

here's how it looks enter image description here

CodePudding user response:

I leave you a possible example: How TO - Custom Checkbox

First we hide the browser's default radio button

.container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}

and now we create a custom radio button

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
  border-radius: 50%;
}

FULL EXAMPLE

body {
  background-color: grey;
}


/* The container */

.container {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 22px;
  color: white;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


/* Hide the browser's default radio button */

.container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}


/* Create a custom radio button */

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
  border-radius: 50%;
}


/* On mouse-over, add a grey background color */

.container:hover input~.checkmark {
  background-color: #ccc;
}


/* When the radio button is checked, add a blue background */

.container input:checked~.checkmark {
  background-color: yellow;
}


/* Create the indicator (the dot/circle - hidden when not checked) */

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}


/* Show the indicator (dot/circle) when checked */

.container input:checked~.checkmark:after {
  display: block;
}


/* Style the indicator (dot/circle) */

.container .checkmark:after {
  top: 9px;
  left: 9px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: white;
}
<h1>Custom Radio Buttons</h1>
<label >One
  <input type="radio" checked="checked" name="radio">
  <span ></span>
</label>
<label >Two
  <input type="radio" name="radio">
  <span ></span>
</label>
<label >Three
  <input type="radio" name="radio">
  <span ></span>
</label>
<label >Four
  <input type="radio" name="radio">
  <span ></span>
</label>

CodePudding user response:

There is no efficient way of doing so (As far as I now), but there are some tolls like https://doodlenerd.com/html-control/css-checkbox-generator to generate custom checkboxes where you can edit a lot of stuff.

They work with custom indicators, which get activated, when you click on them using a label.

CodePudding user response:

I had to apply some styles to the checkbox and faced many challenges. The below link helped me, please have a look.

How to style a checkbox using CSS

  • Related