如何更改复选框背景色与周围的白色

时间:2019-05-14 19:11:31

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

3 个答案:

答案 0 :(得分:1)

您可以使用CSS box-shadow属性:

.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;
  box-shadow: inset 0px 0px 0px 4px white
}
<div class="checkbox">
  <input type="checkbox" class="input-assumpte" id="input-confidencial" />
  <label for="input-confidencial">Confidencial</label>
</div>

答案 1 :(得分:1)

插入阴影足以满足此需求

box-shadow:0 1px 2px rgba(0, 0, 0, 0.05),
    inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 0 0 0 2px #0e4caa ,inset  0 0 0 4px white;

演示

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

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

.checkbox label {}

.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;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 0 0 0 2px #0e4caa, inset 0 0 0 4px white;
  padding: 5px;
}
<div class="checkbox">
  <input type="checkbox" class="input-assumpte" id="input-confidencial" />
  <label for="input-confidencial">Confidencial</label>
</div>

具有负偏移的轮廓也可以工作:

  outline: solid white 2px;
  outline-offset: -5px;

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

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

.checkbox label {}

.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;
  outline: solid white 2px;
  outline-offset: -5px;
  padding: 5px;
}
<div class="checkbox">
  <input type="checkbox" class="input-assumpte" id="input-confidencial" />
  <label for="input-confidencial">Confidencial</label>
</div>

答案 2 :(得分:1)

您真正想要的是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上。

  

background-clip CSS属性设置元素是否为背景   在边框,填充框或内容框的下方扩展。

仅需提一下,还有一种使用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>