css改变checkbox的背景

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

猜你喜欢

转载自www.cnblogs.com/yuNotes/p/11982183.html
今日推荐