如何在CSS中对复选框应用2种不同的颜色?
P粉949190972
P粉949190972 2024-03-29 09:03:27
0
1
371

我在我的页面上使用复选框。效果很好。不过,我想要一个浅蓝色的复选框,另一个浅黄色的复选框。目前两个复选框都是浅蓝色的。我怎样才能把另一颗变成浅黄色?

这是我所拥有的。

input[type=checkbox] {
  position: relative;
  cursor: pointer;
margin-right: 10px;
}
input[type=checkbox]:before {
  content: "";
  display: block;
  position: absolute;
  width: 16px;
  height: 16px;
  top: 0;
  left: 0;
  border: 1px solid #99AFC1;
  border-radius: 3px;
  background-color: lightblue;
  padding: 1px;
}
input[type=checkbox]:checked::before {
  background-color: lightblue;
}

input[type=checkbox]:checked::after {
  content: "";
  display: block;
  width: 5px;
  height: 10px;
  border: solid #ffffff;
  border-width: 0 2px 2px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  top: 2px;
  left: 6px;
}
<p><label><input type="checkbox" class="filter" value="Test1">Test1</label> <label><input type="checkbox" class="filter" value="Test2">Test2</label></p>

P粉949190972
P粉949190972

全部回复(1)
P粉539055526

*这在 Chrome、Edge 和 Firefox 中呈现相同的效果。

只需为您想要的浅黄色提供一个即可。
对于跨浏览器方法,我使用了 进行样式设置。

label.checkbox input[type="checkbox"] {
  display:none;
}
label.checkbox span {
  display:inline-block;
  border: 1px solid Gray;
  border-radius: 4px;
  width: 18px;
  height: 18px;
  background-color: lightblue;
  vertical-align: top;
  position: relative;
}
label.checkbox :checked + span {
  width: 18px;
  height: 18px;
}
label.checkbox :checked + span:after {
  content: '14';
  font-size: 100%;
  position: absolute;
  top: -12%;
  left: 12%;
  color: lightyellow;
}

/* Make the above your default checkbox, and the below can be used for special ones. */

label.checkbox.difcol span {
  background-color: lightyellow; /* New Box Color */
}
label.checkbox.difcol :checked + span:after {
  color: lightblue; /* Corresponding New Checkmark Color */
}
<p>
<label class="checkbox"><input type="checkbox"/><span></span></label> Test1
<label class="checkbox difcol"><input type="checkbox"/><span></span></label> Test2
</p>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!