css input checkbox复选框控件 样式美化的多种方案

更新日期: 2018-05-10阅读: 5.4k标签: 表单

checkbox复选框可能是网站中常用的html元素,但大多数人并不满意它的默认样式,这篇文章就讲讲如何实现input checkbox复选框控件 样式美化效果。


方案一:纯css方法

单选按钮和复选按钮都有选中和未选中状态。要设置这两个按钮默认样式稍微复杂点。该文通过:checked选择器配合其他表情实现自定义样式。举例:使用:checked选择器模拟实现复选框样式。

<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解决方法

原理:label和input外面套一层.custom-checkbox作为父元素相对定位。

input绝对定位于左上角,label也绝对定位于左上角,覆盖input的方块。通过给label设置padding-left和背景图来模拟未选中状态。选中时加一个.right类更改背景图片为选中状态的背景图。通过js点击事件,label被点击时在选中与未选中状态切换。

用到图片: 

   

代码如下:

<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简化版

2的方案用2次绝对定位有点麻烦,因为用了<input type="checkbox">的复选框,如果要求没那么复杂,简单用的<i>标签模拟一下复选框也一样可以实现效果。

html:

<label class="agree-label" onselectstart="return false;" style="-moz-user-select:none;">
        <i class="cus-checked"></i>同意 <a href=":;" target="_blank">某某服务条款</a>
</label>

css:

.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;
    }

js:

$('.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



链接: https://www.fly63.com/article/detial/749

Html5中input新增的表单元素和属性介绍。

input标签主要用于Web表单的创建交互,以便接受来自用户的数据。 我们通过更改type属性的值,来实现不同的输入类型。这篇文章主要讲解html5中新增的表单属性。

Validate表单验证插件之异步操作

使用ajax方式进行验证某个元素的值(只是验证元素的值,而不是ajax方式提交表单),默认会提交当前验证的值到请求的地址,如果要提交其它的值,可以使用data选项。

vue2表单验证组件_vee-validate的使用教程

vee-validate基于vue2的表单验证组件,这篇文章主要讲解它的安装,引用,基础使用,内置的校验规则,自定义校验规则。Validator是以$validator被组件自动注入到Vue实例的,同时也可以独立的进行调用

关于input的一些问题解决方法分享

input是我们接受来自用户的数据常用标签,在前端开发中:移动端底部input被弹出的键盘遮挡。控制input显/隐密码。在input中输入emoji表情导致请求失败。input多行输入显示换行。输入框首尾清除空格-trim()、在input中监听键盘事件

input输入限制只能为数字

input输入限制只能为数字的2种方法,通过onkeypress事件和onkeyup事件,输不上任何非数字字符。加上正则匹配不能输入非数字字符就可以了

小程序/网页实现textarea高度随内容自动改变

textarea默认的高度不是对着内容变化,而是随着内容增多,出现了滚动条。目前的需求是实现一个能够输入的textarea,并且高度跟着内容变化。以及小程序的textarea高度随内容自动改变的实现

input,textarea限制字数,实时绑定

input,textarea限制字数,实时绑定的方式:1.在input 或 textarea中加属性 、 2.js判断,拓展: 实时绑定功能:二种输入标签的实时绑定方式 。 需求:框后面有显示字数

在HTML中限制input 输入框只能输入纯数字

使用 onkeyup 事件,有 bug ,那就是在中文输入法状态下,输入汉字之后直接回车,会直接输入字母,使用 onchange 事件,在输入内容后,只有 input 丧失焦点时才会得到结果,并不能在输入时就做出响应,使用 oninput 事件,完美的解决了以上两种问题

谷歌浏览器禁止表单自动填充

在项目开发期间发现谷歌浏览器有记住密码的功能,该功能有个问题就是一遇到input type=password就开始自动填充,同一个账户还好,就是bug了。找了一堆解决方案终于找到了办法,下面分享一下解决方案。

HTML常用标签之<form>标签

在HTML中,<form></form>标记对用来创建一个表单,即定义表单的开始和结束位置,在标记对之间的一切都属于表单的内容。每个表单元素开始于form元素,可以包含所有的表单控件

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!