【问题标题】:Checkbox background color when checked - not working选中时复选框背景颜色 - 不起作用
【发布时间】:2019-02-13 20:41:13
【问题描述】:

我知道这个问题之前已经回答过,但没有一个答案对我有用,所以无论我做错了什么,我都需要帮助。我希望我的复选框在单击后具有绿色背景色和白色复选标记。我在下面添加了我的代码,在 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>

【问题讨论】:

    标签: html css bootstrap-4


    【解决方案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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-27
      • 2021-01-12
      • 2022-12-18
      • 1970-01-01
      • 2013-11-29
      • 2019-10-16
      • 1970-01-01
      • 2019-03-22
      相关资源
      最近更新 更多