无法设置复选框的HTML / CSS样式

时间:2018-11-27 23:56:10

标签: javascript html css css3 checkbox

我无法在某些HTML复选框上进行任何样式设置。任何CSS无法更改复选框的外观:

HTML:

<input type="checkbox" id="checkbox-1-1" class="regular-checkbox" />
    <nput type="checkbox" id="checkbox-1-2" class="regular-checkbox" />
    <input type="checkbox" id="checkbox-1-3" class="regular-checkbox" />
    <input type="checkbox" id="checkbox-1-4" class="regular-checkbox" />

CSS:

.regular-checkbox {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

复选框未更改。谁能告诉我我在做什么错?我认为这在CSS中是可能的。

2 个答案:

答案 0 :(得分:1)

让我知道是否有帮助,或者您需要一些解释。

<!DOCTYPE html>
<html>
<style>
  .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;
    height: 0;
    width: 0;
  }
  
  .checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 25px;
    width: 25px;
    background-color: #eee;
  }
  
  .container:hover input~.checkmark {
    background-color: #ccc;
  }
  
  .container input:checked~.checkmark {
    background-color: #2196F3;
  }
  
  .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);
  }
</style>

<body>

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

</body>

</html>

答案 1 :(得分:0)

无法在CSS中设置复选框输入(也不显示收音机)的样式。

但是您可以创建一个自定义复选框并添加一个隐藏的复选框或类似的东西。大多数人这样做是因为它可以轻松地跨浏览器兼容。

相关问题