思路:
1.label绑定复选框,选择label时则复选框被选中
2.隐藏原来的复选框,利用:after或:before伪样式在label前或后添加一个空白字符,设置宽高和圆角
3.点击label或圆角时设置圆角颜色,因为三者是一起的,所以三者一起变化
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
/* 最外层div */
.div1 {
width: 800px;
height: 800px;
max-height: 400px;
max-width: 400px;
left: 50%;
position: absolute;
margin-left: -200px;
top: 50%;
margin-top: -200px;
}
/* 隐藏原来的表单 */
input {
display: none;
}
/* 将label设置为块级元素 */
label {
width: 100%;
height: 50px;
display: inline-block;
line-height: 50px;
position: relative;
text-align: center;
border-top: 1px solid #eee;
font-size: 16px;
font-family: '微软雅黑';
}
label:active {
background: #EEEEEE;
}
/* 在label前或后添加一个空白字符,设置宽高,设置圆角使之成为圆形 */
label:after {
content: "";
width: 20px;
height: 20px;
border: 1px solid #e2d1c3;
border-radius: 20px;
display: inline-block;
position: absolute;
top: 15px;
left: 15px;
}
/* label与check是绑定的,点击label会被checked,设置被选中时check选框背景 */
input:checked+label:after{
background-color: #e2d1c3;
}
/* 被选中时label背景 */
input:checked+label{
background:-webkit-linear-gradient(left top,#fdfcfb,#e2d1c3);
}
</style>
</head>
<body>
<div class="div1">
<input type="checkbox" id="checkbox1" />
<label for="checkbox1">选项一</label>
<input type="checkbox" id="checkbox2" />
<label for="checkbox2">选项二</label>
<input type="checkbox" id="checkbox3" />
<label for="checkbox3">选项三</label>
<input type="checkbox" id="checkbox4" />
<label for="checkbox4">选项四</label>
<input type="checkbox" id="checkbox5" />
<label for="checkbox5">选项五</label>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.regular-checkbox{
display: none;
}
.label-box {
border: 1px solid #cacece;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
padding: 9px;
border-radius: 20px;
display: inline-block;
position: relative;
}
.regular-checkbox+.label-box:active,
.regular-checkbox:checked+.label-box {
background-color: #003399;
box-shadow: 0 1px 2px rgba(0, 51, 204, 0.05), inset 0px 1px 3px rgba((0, 51, 204, 0.1);
}
</style>
</head>
<body>
<input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /><label for="checkbox-1-1" class="label-box"></label>这是第一个复选框
<input type="checkbox" id="checkbox-1-2" class="regular-checkbox" /><label for="checkbox-1-2" class="label-box"></label>这是第二个复选框
<input type="checkbox" id="checkbox-1-3" class="regular-checkbox" /><label for="checkbox-1-3" class="label-box"></label>这是第三个复选框
<input type="checkbox" id="checkbox-1-4" class="regular-checkbox" /><label for="checkbox-1-4" class="label-box"></label>这是第四个复选框
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.regular-checkbox{
display: none;
}
.label-box {
border: 1px solid #cacece;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
padding: 9px;
border-radius: 20px;
display: inline-block;
position: relative;
font-size: 14px;
padding: 5px 10px;
}
.regular-checkbox+.label-box:active,
.regular-checkbox:checked+.label-box {
/* background-color: #003399; */
border: 1px solid #003399;
box-shadow: 0 1px 2px rgba(0, 51, 204, 0.05), inset 0px 1px 3px rgba((0, 51, 204, 0.1);
}
</style>
</head>
<body>
<input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /><label for="checkbox-1-1" class="label-box">这是第一个复选框</label>
<input type="checkbox" id="checkbox-1-2" class="regular-checkbox" /><label for="checkbox-1-2" class="label-box">这是第二个复选框</label>
<input type="checkbox" id="checkbox-1-3" class="regular-checkbox" /><label for="checkbox-1-3" class="label-box">这是第三个复选框</label>
<input type="checkbox" id="checkbox-1-4" class="regular-checkbox" /><label for="checkbox-1-4" class="label-box">这是第四个复选框</label>
</body>
</html>