首页 > 解决方案 > 复选框样式和背景

问题描述

是否可以像这样设置复选框的样式并更改其背景!

试过阴影框和轮廓

我所能达到的最好的方法是使用 :before 方法,但仍然没有关闭

党卫军

这是我尝试过的

CSS

.table__check-box {
    background-color: red;
    position: relative;
    height: 1.75rem;
    width: 1.75rem;
    margin-right: 2.3rem;
    margin-top: 1rem;
}

.table__check-box--inactive {
    background-color: #a4a9ae;
}

.table__check-box:before {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: 5px;
    border: 2px solid #ccc;
}

HTML

<input class="table__check-box" type="checkbox">

标签: htmlcss

解决方案


输入元素没有内容,所以应该没有:before. 更好的使用table__check-box + label:before

* {
  box-sizing: border-box;
}

.table__check-box {
  opacity: .3;// Just for display
  // Uncomment:
  // display: none;
}

.table__check-box + label {
    background-color: red;
    display: inline-block;
    position: relative;
    height: 1.75rem;
    width: 1.75rem;
    margin-right: 2.3rem;
    margin-top: 1rem;
    overflow: hidden;
    border-radius: 5px;
    border: 2px solid #ccc;
}

.table__check-box:not(:checked) + label {
    background-color: #a4a9ae;
}
<div class="input-wrapper">
  <input id="active-checkbox" class="table__check-box" type="checkbox"/>
  <label for="active-checkbox"></label>
</div>


推荐阅读