css改变checkbox的背景

注意:此方法不兼容火狐浏览器,只有谷歌的兼容性比较好!!!!!!!!!11

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;
  }

posted on 2019-12-04 12:34  小雨子1993  阅读(984)  评论(0编辑  收藏  举报

导航