css - 如何更改复选框背景颜色为白色

标签 css css-selectors

我正在寻找一种解决方案,如何使用复选框周围的填充或白色来更改 css 背景颜色,我尝试使用边距、填充高度等进行尝试,但没有成功。 因此,当用户单击该复选框时,它会显示背景颜色,它正在工作,但我希望在该复选框周围有白色填充,这可能吗?

.checkbox {
  margin: 0 0 1em 2em;
}
.checkbox .tag {
  color: #595959;
  display: block;
  float: left;
  font-weight: bold;
  position: relative;
  width: 120px;
}
.checkbox label {
  display: inline;
}
.checkbox .input-assumpte {
  display: none;
}
.input-assumpte + label:after {
  background-color: #fafafa;
  border: 1px solid #cacece;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05),
    inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
  display: inline-block;
  transition-duration: 0.3s;
  width: 16px;
  height: 16px;
  content: "";
  margin-left: 10px;
  padding: 5px;
}
.input-assumpte:checked + label:after {
  background-color: #0e4caa;
  padding: 5px;
}
<div class="checkbox">
							<input type="checkbox" class="input-assumpte" id="input-confidencial" />
							<label for="input-confidencial">Confidencial</label>
						</div>

最佳答案

你想要的正是background-clip , 简单:

.checkbox {
    margin: 0 0 1em 2em;
}

.checkbox .tag {
    color: #595959;
    display: block;
    float: left;
    font-weight: bold;
    position: relative;
    width: 120px;
}

.checkbox label {
    display: inline;
}

.checkbox .input-assumpte {
    display: none;
}

.input-assumpte + label::after {
    background-color: #fafafa;
    border: 4px solid #0e4caa;
    background-clip: content-box;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
    display: inline-block;
    transition-duration: 0.3s;
    width: 16px;
    height: 16px;
    content: "";
    margin-left: 10px;
    padding: 5px;
}

.input-assumpte:checked + label::after {
    background-color: #0e4caa;
    padding: 5px;
}
<div class="checkbox">
    <input type="checkbox" class="input-assumpte" id="input-confidencial" />
    <label for="input-confidencial">Confidencial</label>
</div>

也在 JSFiddle 上.

The background-clip CSS property sets whether an element's background extends underneath its border box, padding box, or content box.

顺便提一下,还有一种使用outline 的额外边框解决方案,但它扩展了元素:

.checkbox {
    margin: 0 0 1em 2em;
}

.checkbox .tag {
    color: #595959;
    display: block;
    float: left;
    font-weight: bold;
    position: relative;
    width: 120px;
}

.checkbox label {
    display: inline;
}

.checkbox .input-assumpte {
    display: none;
}

.input-assumpte + label::after {
    background-color: #fafafa;
    border: 4px solid #fafafa;
	outline: 4px solid #0e4caa;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
    display: inline-block;
    transition-duration: 0.3s;
    width: 16px;
    height: 16px;
    content: "";
    margin-left: 10px;
    padding: 5px;
}

.input-assumpte:checked + label::after {
    background-color: #0e4caa;
    padding: 5px;
}
<div class="checkbox">
    <input type="checkbox" class="input-assumpte" id="input-confidencial" />
    <label for="input-confidencial">Confidencial</label>
</div>

关于css - 如何更改复选框背景颜色为白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56137079/

相关文章:

javascript - 一个div作为全屏背景

动态改变div宽度的Javascript

html - 为什么我的按钮有这个轮廓? CSS

php - 发现 fatal error : While Transfering website One server to another Server

jquery - CSS 选择器和 jQuery 过滤器的区别?

css - 如何使用悬停选择器显示 "uncle"元素

html - 带有填充的代码元素扩展到其容器之外

css - CSS3 上的否定伪类对 child 不起作用

html - css中的 '>'符号是什么意思?

html - 伪选择器 :last-child not working