css改变checkbox的背景

徘徊边缘 提交于 2019-12-06 12:01:38

checkbox默认不支持修改背景颜色,利用伪类元素实现,基本原理是利用after/ before插入新的元素。然后利用新元素的背景颜色或背景图片覆盖掉原来的样式。

原始样子

 

要实现的样子

 

<input type="checkbox" />
input[type=checkbox] {
    cursor: pointer;
    position: relative;
  }
  
  input[type=checkbox]:after {
    position: absolute;
    width: 10px;
    height: 10px;
    content: " ";
    background-color: rgb(200,237,255);
    visibility: visible;
    border-top: 1px solid #ccc;
    border-left: 1px solid #ccc;
  }
  
  input[type=checkbox]:checked:after {
    content: "✔";
    position: absolute;
    line-height: 11px;
    left: 0;
    top: 0;
  }

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!