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

时间:2018-09-09 00:31:26

标签: html5 css3 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>

1 个答案:

答案 0 :(得分:1)

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