为什么我的仅CSS单选按钮没有得到检查?

问题描述 投票:0回答:1

我正在尝试创建带有标签的仅CSS自定义收音机,但我无法弄清为什么未对其进行检查。

我希望当我单击自定义收音机时,应该对其进行检查。实际上,实际结果是,当我单击自定义收音机时,它没有得到检查。

请说明为什么会这样。

非常感谢!

这里是:

.choice {
  display: block;
  width: fit-content;
  margin: 0.25em 0 0 1.5em;
  padding-top: 0.2em;
  padding-left: 0.2em;
  font-size: 1.5em;
  cursor: pointer;
}

.container {
  cursor: pointer;
  top: -0.125em;
  position: relative;
}

input {
  position: absolute;
  opacity: 0;
  visibility: hidden;
}

.checkmark {
  display: inline-block;
  position: relative;
  height: 0.8em;
  width: 0.8em;
  outline-style: none;
  box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
  background-color: rgba(0, 0, 0, 0);
  border: 0.1em solid #2185d0;
  border-radius: 50%;
  transition: all 0.5s;
  cursor: pointer;
}

.checkmark:after {
  content: "";
  width: 0.6em;
  height: 0.6em;
  position: relative;
  top: 50%;
  left: 50%;
  background-color: #2185d0;
  border-radius: 50%;
  display: block;
  cursor: pointer;
  transform: translate(-50%, -50%) scale(0);
  transition: all 0.2s;
}

.choice:hover input~.checkmark {
  box-shadow: 0 0 0 0.25em rgba(33, 133, 208, 0.3);
  background-color: rgba(33, 133, 208, 0.3);
}

input[checked]~.checkmark:after {
  transform: translate(-50%, -50%) scale(1);
}
<div class="choice">
  <input type="radio" name="radio" id="radio1" />
  <span class="checkmark"></span>
  <label for="radio1" class="container">One</label>
</div>
css radio-button label radio
1个回答
0
投票

谢谢Temani Afif

:checked选择器匹配每个选中的元素(仅适用于单选按钮和复选框)和元素。

我犯了使用input[checked]而不是input:checked的错误

https://www.w3schools.com/cssref/sel_checked.asp

© www.soinside.com 2019 - 2024. All rights reserved.