悬停时如何更改选中的单选按钮的颜色?

How to change color of checked radio button on hover?

提问人:Suton 提问时间:10/23/2023 更新时间:10/23/2023 访问量:44

问:

<!-- 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;
    }

我使用了带有输入单选标签的 hover 属性,但不起作用。

HTML CSS 悬停 单选组

评论


答:

0赞 Brett Donald 10/23/2023 #1

不知道你为什么要这样做,但你可以做到。

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赞 DPC 10/23/2023 #2

实现此目的的方法之一是创建自定义单选按钮。我附上了一个片段来演示这一点。例如,您可以设置 和 属性的样式来更改按钮颜色。.checkmark:after { background-color: #2196F3; }.container .checkmark:after { background-color: #2196F3; }

来源:自定义单选按钮和复选框

.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>

1赞 Jaswinder Kaur 10/23/2023 #3

 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>