checkbox复选框可能是网站中常用的HTML元素,但大多数人并不满意它的默认样式,这篇文章就讲讲如何实现input checkbox复选框控件 样式美化效果。::before和::after伪元素的用法
方案一:纯css方法
<meta charset="utf-8">
<style type="text/css">
form {
border: 1px solid #ccc;
padding: 20px;
width: 300px;
margin: 30px auto;
}
.wrapper {
margin-bottom: 10px;
}
.box {
display: inline-block;
width: 20px;
height: 20px;
margin-right: 10px;
position: relative;
border: 2px solid orange;
vertical-align: middle;
}
.box input {
opacity: 0;
}
.box span {
position: absolute;
top: -10px;
right: 3px;
font-size: 30px;
font-weight: bold;
font-family: Arial;
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
color: orange;
}
input[type="checkbox"] + span {
opacity: 0;
}
input[type="checkbox"]:checked + span {
opacity: 1;
}
</style>
<form action="#">
<div class="wrapper">
<div class="box">
<input type="checkbox" checked="checked" id="username" /><span>√</span>
</div>
<label for="username">我是选中状态</label>
</div>
<div class="wrapper">
<div class="box">
<input type="checkbox" id="userpwd" /><span>√</span>
</div>
<label for="userpwd">我是未选中状态</label>
</div>
</form>
方案二:配合js解决方法
<meta charset="utf-8"/>
<style type="text/css">
.custom-checkbox{
border:1px solid red;
position:relative;
height:30px;
}
.custom-checkbox input{
position:absolute;
}
.custom-checkbox label{
padding-left:20px;
position:absolute;
top:-1px;
left:0;
background:url(images/bg-unchecked.png) no-repeat top 4px left 3px;
}
.custom-checkbox label.right{
background:url(images/bg-checked.png) no-repeat top 2px left 3px;
}
</style>
<body>
<div class="custom-checkbox">
<input type="checkbox" id="test"/><label for="test">已阅读并同意相关服务条款</label>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$('.custom-checkbox label').click(function(){
if($(this).hasClass('right')){
$('.custom-checkbox label').removeClass("right");
}else{
$('.custom-checkbox label').addClass("right");
}
});
</script>
</body>
<label for="test" onselectstart="return false;" style="-moz-user-select:none;">已阅读并同意相关服务条款</label>
方案三:配合js简化版
<label class="agree-label" onselectstart="return false;" style="-moz-user-select:none;">
<i class="cus-checked"></i>同意 <a href=":;" target="_blank">某某服务条款</a>
</label>
.agree-label {
display: inline-block;
font-size: 18px;
}
.agree-label i {
display: inline-block;
width: 21px;
height: 22px;
margin-bottom: -4px;
margin-right: 6px;
background: url(images/checkedbox.png) no-repeat;
}
.agree-label i.cus-checked {
background-position: 0 -22px;
}
$('.agree-label').click(function() {
if ($('.agree-label i').hasClass('cus-checked')) {
$('.agree-label i').removeClass("cus-checked");
} else {
$('.agree-label i').addClass("cus-checked");
}
});
第三种方案兼容IE8。本文作者starof,本文来源:http://www.cnblogs.com/starof/p/4904929.html