<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>美化checkbox</title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
div input{
display: none;
}
.chc label{
background-image: url('./checkbox.png');
background-position: left center;
background-repeat: no-repeat;
background-size:20px 20px; /*把背景图片进行裁剪*/
padding-left: 20px;
}
.chc #check:checked + label{
background-image: url('./checkbox 1.png');
background-position: left center;
background-repeat: no-repeat;
background-size:20px 20px;
padding-left: 20px;
}
</style>
</head>
<body>
<div class='chc'>
<input type="checkbox" id="check" />
<label for="check">选中我</label>
</div>
</body>
</html>
图标选自阿里矢量图标库,下载的时候下载同一图标的两种不同颜色。
原有的checkbox框太难看,所以要先隐藏起来
选择器中用到了 + 的作用是同时选中伪类元素的兄弟元素