How to change color of checked radio button on hover?

169 Views Asked by At
<!-- This is my html input tag -->
 <input class="custom-radio" type="radio" name="selected_center" value="{{center.id}}"
                    id="selected_center_{{center.id}}">

<!-- Only CSS I am using for my custom color using accent property but cant change the color on hover -->
 input[type='radio'] {
        accent-color: #0098ff;
    }

I have used hover property with input radio tag but not working.

3

There are 3 best solutions below

0
Brett Donald On

Not sure why you would want to do this, but you can do it.

body {
  margin: 1em;
  font-family: sans-serif;
}
.custom-radio>label {
  display: block;
  font-size: 1.5em;
  margin: 0.25em 0;
}

.custom-radio>label>input[type=radio] {
  transform: scale(2);
  vertical-align: middle;
  margin-right: 1em;
}

.custom-radio>label:nth-child(1)>input[type=radio] {
  accent-color: red;
}

.custom-radio>label:nth-child(2)>input[type=radio] {
  accent-color: darkorange;
}

.custom-radio>label:nth-child(3)>input[type=radio] {
  accent-color: green;
}

.custom-radio>label:hover {
  color: blue;
}

.custom-radio>label:hover>input[type=radio] {
  accent-color: blue;
}

.custom-radio>label>input[type=radio]:not(:checked) {
  cursor: pointer;
}
<div class="custom-radio">
  <label><input type="radio" name="radio1"> Stop</label>
  <label><input type="radio" name="radio1"> Wait</label>
  <label><input type="radio" name="radio1"> Go</label>
</div>

0
Al John On

One of the ways to achieve this is by creating a custom radio button instead. I've attached a snippet to demonstrate this. You could style the .checkmark:after { background-color: #2196F3; } and .container .checkmark:after { background-color: #2196F3; } properties for example to change the buttons color.

Source: custom radio buttons and checkboxes

.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;
}
.container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
  border-radius: 50%;
}
.container:hover input ~ .checkmark {
  background-color: #ccc;
}
.container input:checked ~ .checkmark {
  background-color: #2196F3;
}
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}
.container input:checked ~ .checkmark:after {
  display: block;
}
.container .checkmark:after {
    top: 9px;
    left: 9px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: white;
}
<label class="container">One
  <input type="radio" checked="checked" name="radio">
  <span class="checkmark"></span>
</label>
<label class="container">Two
  <input type="radio" name="radio">
  <span class="checkmark"></span>
</label>
<label class="container">Three
  <input type="radio" name="radio">
  <span class="checkmark"></span>
</label>
<label class="container">Four
  <input type="radio" name="radio">
  <span class="checkmark"></span>
</label>

0
Jaswinder Kaur On

 input[type='radio'] {
        accent-color: #0098ff;
    }
 input[type='radio']:hover {
        accent-color: red;
    }
<input class="custom-radio" type="radio" name="selected_center" value="{{center.id}}" id="selected_center_{{center.id}}" checked>