css - 如何在选中时更改背景颜色

标签 css bulma

因此,关于传统上如何执行此操作有很多答案,但我使用的是 CSS 库 Bulma,但这些解决方案似乎都不起作用。我只需要能够在选中时更改复选框的颜色。

我已经尝试使用 !important 使其工作但没有改变。

<div >
 <label class="checkboxes" for="exact" id="color">
 <input type="checkbox" id="exact" value="exact" checked>
  Exact
 </label>
</div>

这是添加了 css 库的 fiddle 。 https://jsfiddle.net/yqwp4eoh/

最佳答案

您不能更改默认的复选框背景。你必须隐藏它并制作一个自定义的,要么将它的 width/height 设置为 0,要么设置 display: none 来隐藏它,然后使用 span:before/:after 伪类来应用您的样式。

这是来自 W3Schools 的详细示例:

/* 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);
}
<div>
  <label class="container">Exact
    <input type="checkbox" value="exact" checked="checked">
    <span class="checkmark"></span>
  </label>
</div>

关于css - 如何在选中时更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54542777/

相关文章:

html - 选中复选框时删除文本

javascript - 无法设置 buefy 模态宽度

html - 如何撤消 bulma 嵌套元素中的 is_mobile?

css - 如何让文件上传Buefy组件x%宽高?

html - 如何阻止 Webpack-3 的 PurifyCSSPlugin 清理太多?

jquery - 使用 jquery 调整固定位置 div 的大小和居中

jquery - 如何控制动态加载到iframe中的内容的CSS?

css - 我怎样才能让 bool 玛按钮全宽?

javascript - CSS 类不适用于 html

jquery - 使用 Bootstrap 3 的 Twitter Typeahead 上的 CSS 问题