html - 选中时的复选框背景颜色 - 不起作用

标签 html css bootstrap-4

我知道这个问题以前有人回答过,但没有一个答案对我有用,所以无论我做错了什么,我都需要帮助。我希望我的复选框在单击后具有绿色背景颜色和白色复选标记。我在下面添加了我的代码,在 codepen 链接中,您可以查看更多详细信息。

Codepen 链接:--> https://codepen.io/paulamourad/pen/oPpbEm

/* Customize the label (the container) */

.form-check {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 0.9em;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


/* Hide the browser's default checkbox */

.form-check input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}


/* Create a custom checkbox */

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 20px;
  width: 20px;
  border: 2px solid #D6D4D4;
}


/* Create the checkmark/indicator (hidden when not checked) */

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}


/* Show the checkmark when checked */

.form-check input:checked~.checkmark:after {
  display: block;
}


/* Style the checkmark/indicator */

.form-check .checkmark:after {
  left: 2px;
  top: 3px;
  width: 5px;
  height: 10px;
  border: solid #8ABE57;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
<label class="form-check">
                                <input class="form-check-input" type="checkbox" value="" id="checkPeriodDays">
                                <span class="checkmark"></span>
                                <label class="form-check-label ml-2" for="checkPeriodDays">
                                    Días
                                </label>
</label>

最佳答案

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

.container:hover input ~ .checkmark {
    background-color: green;
}


.container input:checked ~ .checkmark {
    background-color: green;
}

.checkmark:after {
    content: "";
    position: absolute;
    display: none;
}

.container input:checked ~ .checkmark:after {
    display: block;
}


.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);
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Checkbox Test</title>
</head>
<body>
<label class="container">Dias One
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>
<label class="container">Dias Two
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<label class="container">Dias Three
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
</body>
</html>

关于html - 选中时的复选框背景颜色 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52240405/

相关文章:

twitter-bootstrap - 当 .card-body 内容溢出时,在 .card-header 上一起设置高度和 d-flex 不起作用

html - 如何使 <hr> 标签响应?

jquery - 制作幻灯片时出现问题。不显示?

jquery - Javascript 动画/CSS 过渡上的 Renderbug Chrome(OS X 上为 36)

html - 在移动 View 中选择下拉菜单

javascript - Chrome 扩展程序获取选定的文本

css - div 100% 高度滚动

javascript - 将查询结果的颜色分配给 div

javascript - 这种计算 Canvas 元素中非白色像素的方法有什么问题?

javascript - 如何在一定高度后固定滚动 div 并在到达其他 div 后停止?