纯css美化复选框单选框(通过添加修改背景图片实现)
原生的复选框单选框无法满足工作中的需求,更多是通过ui图决定复选框单选框样子,通过添加修改背景图片实现更加灵活,可修改性更强.
原始复选框单选框效果图:
背景图主要用到两张图片(选中前选中后):
通过伪元素绝对定位,相对与复选框单选框,给他们加上背景图片将原有的复选框单选框覆盖即可,代码如下
input{
position: relative;
}
//选中前
input::before {
content: "";
//伪元素大小
width: 24px;
height: 27px;
//相对与复选框单选框位置
position: absolute;
top: -10px;
left: 0;
//背景图片设置
background-image: url("../img/nocheck.png");//未选中的时候的样子
background-size: 20px 20px;
background-position: 0px 7px;
background-repeat: no-repeat;
background-color: #3cab9f;
}
//选中后
input:checked::before {
content: "";
background-image: url("../img/check.png");//选中的时候的样子
background-size: 24px 27px;
background-position: center;
}