【问题标题】:Custom Checkbox / Radio Buttons Focus自定义复选框/单选按钮焦点
【发布时间】:2019-10-06 00:43:18
【问题描述】:

我使用以下方法创建了自定义复选框/收音机:

<label class="container">One
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>

<label class="container">Two
  <input type="checkbox">
  <span class="checkmark"></span>
</label>

<label class="container">Three
  <input type="checkbox">
  <span class="checkmark"></span>
</label>

<label class="container">Four
  <input type="checkbox">
  <span class="checkmark"></span>
</label> 
/* Customize the label (the container) */
.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;
}

/* Hide the browser's default checkbox */
.container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
  background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
  background-color: #2196F3;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
} 

见:https://codepen.io/abluegem/pen/xxxxqez 还有:https://www.w3schools.com/howto/howto_css_custom_checkbox.asp

现在我面临“焦点”状态的问题。这些按钮没有焦点状态。我尝试添加自己的 CSS,但它不起作用。

如您所见,主要元素/标签是没有焦点状态的“标签”。并且有输入标签,但它是隐藏的。

我想知道我是否可以在这些按钮上设置焦点状态。

【问题讨论】:

  • 我不明白为什么人们如此沉迷于投票。这是一个有效的问题。 +1。
  • @LGSon 好吧,他是社区的新手,他仍然试图尽可能正确地解释它。他肯定会随着时间的推移而学习,但如果他一开始就被否决,他将无法提高自己并做出进一步的贡献。
  • @Pixel Potter:为了清晰和符合准则,我将示例代码添加到您的问题中。

标签: css checkbox radio-button usability


【解决方案1】:

要在自定义复选框中设置焦点样式,您需要在真正的复选框具有焦点时更改不同元素的样式。

添加这个css:

    input:focus + .checkmark
    {
      background-color: red;
    }

参考:focusing on an element changes another's CSS

【讨论】:

  • 当其他用户回答的问题不符合一般 SO 准则时,这里的许多用户都投了反对票,我也是。既然你现在说得有点正确,我收回了我的反对票。原因主要是为了尽量保持帖子的合理质量水平,如果一个问题太糟糕,任何答案都与所问的内容没有真正的关系,因此除了被问到的人之外,没有增加太多价值。 ..这不是 SO 的意义所在,成为单个用户的调试团队。
  • 如果你读过this comment of mine,你可能会更好地理解为什么我认为这是一个不好的问题。
  • @LGSon 很公平,这对我来说真的很有意义。
猜你喜欢
  • 2012-01-07
  • 2016-08-15
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2019-06-03
  • 1970-01-01
相关资源
最近更新 更多