CSS设置复选框check样式

思路:

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>

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值