css设置移动端checkbox和radio样式
复选框Checkbox是Web应用常用控件,随处可见,原生的复选框控件一般就像下面这样:
这取决于操作系统和浏览器,有些时候,这种样子并不能满足设计要求,这时需要更为精致的复选框样式。以往只有少数浏览器才支持对这类控件应用样式,比如拿到这样一张设计图:

blue.png
在过去,想要通过简单地修改样式达成这种设计效果根本不行,不过,现在借助强大的CSS3属性appearance可以对该类控件有空前的样式控制能力:
input[type="checkbox"] {
-webkit-appearance: none;
}
这样设置该属性值为none就去掉了复选框原有的呈现方式,变成了一个普普通通的元素,然后就可以为之应用样式了,添加如下样式:
input[type="checkbox"] {
-webkit-appearance: none;
background: #fff url(i/blue.png);
height: 22px;
vertical-align: middle;
width: 22px;
}
通过结合使用状态伪类选择器:checked可以为选中状态下的checkbox设置不同的样式,用以从视觉上区别:
input[type="checkbox"]:checked {
background-position: -48px 0;
}
此时点击复选框,可以看到复选框样式的变化效果,另外,根据那张设计图片所示还得加上获取焦点,禁用等状态的样式:
input[type="checkbox"]:focus,
input[type="checkbox"]:hover {
background-position: -24px 0;
outline: none;
}
input[type="checkbox"]:checked {
background-position: -48px 0;
}
input[type="checkbox"][disabled] {
background-position: -72px 0;
}
input[type="checkbox"][disabled]:checked {
background-position: -96px 0;
}
因为图片已经事先合并成一张了,简单修改一下background-position就可以了,同时前面几个选择器的优先级(权重)一样,所以书写顺序很重要。
兼容性
目前只兼容Webkit系列浏览器;虽然Firefox也实现了替代的-moz-appearance属性,不过控件原有的背景颜色、边框样式无法修改,暂时也不大好用;IE 系列暂时不支持该属性,更详细的兼容情况查看Caniuse/appearance。因此需要为IE浏览器清除掉背景图片的影响:
input[type="checkbox"] {
background: #fff url(i/blue.png);
background: none\0;
*background: none;
...
}
为了兼容更多的主流浏览器,需要寻求另外的解决方案,在所有主流浏览器里,点击关联某个复选框的label时,产生的效果和点击元素自身相同,会切换复选框控件的选中状态。浏览器的这种行为给了我们一个至关重要的挂钩,既然能依靠label元素来控制原生复选框的状态,那么就可以不必直接操作实际的复选框元素,而把操作和样式都转移到与之关联的label元素上去:
<input id="example" type="checkbox">
<label for="example"></label>
确保label元素的for属性的值和复选框input的id值一致,同时将label元素放置于input之后,这样CSS可以通过相邻兄弟选择器(Adjacent sibling selector)定位到这个label元素并为之应用样式:
input[type="checkbox"] + label:before {
background: #fff url(i/blue.png);
content: " ";
height: 22px;
left: 0;
position: absolute;
width: 22px;
}
有了样式化的label元素来提供交互,原生的checkbox控件就显得有点多余了,虽然可以用display: none把它隐藏掉,不过隐藏后的表单元素是不能获得焦点的,所以最好的方式还是用label元素把它遮住,这样既能支持键盘交互,同时当图片加载失败的时候,又能保证原生的控件可用:
input[type="checkbox"] {
box-sizing: border-box;
left: 4px;
margin: 0;
padding: 0;
position: absolute;
top: 3px;
}
图片要足够大能将原生的checkbox控件完全遮挡住,因为这里用到了绝对定位,所以需要增加一个定位参照:
<!-- HTML -->
<div class="checkbox">
<input id="exampleCheckbox" type="checkbox">
<label for="exampleCheckbox"></label>
</div>
/* CSS */
.checkbox {
min-height: 24px;
padding-left: 24px;
position: relative;
}
左边预留内边距是为了排版更美观,同时,和之前一样,搭配上其它状态下的样式:
input[type="checkbox"]:focus + label:before,
input[type="checkbox"] + label:hover:before {
background-position: -24px 0;
}
input[type="checkbox"]:checked + label:before {
background-position: -48px 0;
}
input[type="checkbox"][disabled] + label:before {
background-position: -72px 0;
}
input[type="checkbox"][disabled]:checked + label:before {
background-position: -96px 0;
}
兼容性
只要支持CSS3 selectors的浏览器基本上都能兼容,同时具备原生控件的绝大多数交互特性。IE 8 不支持 :checked 伪类选择器,将伪元素 :before 修改为双冒号 ::before 可以去掉对IE 8的影响:
input[type="checkbox"] + label::before { ... }
关于伪元素生成内容的兼容性见CSS Generated content for pseudo-elements。诚然,上面的方法假设了支持 :checked 伪类选择器的浏览器同时也支持双冒号伪元素写法,而不支持的浏览器则都不支持,这是一种不太好的方式,这种假设事实上也是不正确的,造成了部分老旧浏览器不可用的问题,如果使用:not()选择器则更为合理,使用:not(:checked)来为未选中的控件添加样式,:not()和:checked同属一个规范css3-selectors,兼容性应该一致CSS3 selectors。不过写法有点变化,:checked和:not(:checked)都需要添加上基本的样式:
input[type="checkbox"]:checked + label:before,
input[type="checkbox"]:not(:checked) + label:before {
background: #fff url(i/blue.png);
content: " ";
height: 22px;
left: 0;
position: absolute;
width: 22px;
}
input[type="checkbox"]:not(:checked):focus + label:before,
input[type="checkbox"]:not(:checked) + label:hover:before {
background-position: -24px 0;
}
input[type="checkbox"]:checked + label:before {
background-position: -48px 0;
}
input[type="checkbox"][disabled]:not(:checked) + label:before {
background-position: -72px 0;
}
input[type="checkbox"][disabled]:checked + label:before {
background-position: -96px 0;
}
查看简单示例,对于那些并不支持:checked伪类选择器的浏览器(比如 IE 8),则可以借助javaScript来根据控件状态修改真正的class属性达到区分不同状态的目的,比如根据是否被选中来添加或删除一个checked的class:
// jQuery
$('input[type="checkbox"]').on('change', function() {
$(this)[$(this).prop('checked') ? 'addClass' : 'removeClass']('checked');
});
/* CSS */
input[type="checkbox"].checked + label:before { ... }
有了前面的基础,要制作类似于开关按钮的控件也是非常简单的了,还是熟悉的结构:
<div class="checkbox">
<input id="example" type="checkbox">
<label for="example">Check</label>
</div>
首先勾勒出开关的形状,一个圆角矩形中间放一个圆形按钮:
input[type="checkbox"]:checked + label,
input[type="checkbox"]:not(:checked) + label {
background-color: #e0e0e0;
border-radius: 24px;
cursor: pointer;
display: inline-block;
height: 24px;
position: relative;
text-indent: -9999px;
width: 48px;
}
input[type="checkbox"]:checked + label:after,
input[type="checkbox"]:not(:checked) + label:after {
background-color: #fff;
border-radius: 20px;
content: " ";
height: 20px;
left: 2px;
position: absolute;
top: 2px;
width: 20px;
}

选中的效果只要简单修改下外框的背景色和中间按钮的位置就行:
input[type="checkbox"]:checked + label {
background-color: #8c8;
}
input[type="checkbox"]:checked + label:after {
left: 26px;
}

不过这种跳跃式变化实在是太生硬了,添加点过渡效果,看上去更平滑:
input[type="checkbox"]:checked + label,
input[type="checkbox"]:not(:checked) + label {
-webkit-transition: background-color 0.3s;
transition: background-color 0.3s;
}
input[type="checkbox"]:checked + label:after,
input[type="checkbox"]:not(:checked) + label:after {
-webkit-transition: left 0.3s;
transition: left 0.3s;
}
点击就能看到效果,对于中间的按钮部分使用CSS3 Transforms来替代left效果更好,速度更快:
input[type="checkbox"]:checked + label:after,
input[type="checkbox"]:not(:checked) + label:after {
-webkit-transition:
left -webkit-transform 0.3s; -o-transition: -o-transform 0.3s; transition:left transform 0.3s; } input[type="checkbox"]:checked + label:after {left: 26px; -webkit-transform: translateX(24px); -ms-transform: translateX(24px); -o-transform: translateX(24px); transform: translateX(24px);}
不支持CSS3 Transforms的浏览器仍然可以看到背景色的变化,不影响可用性,详见CSS3 Transforms。关于性能问题,请参考High Performance Animations。快速点击“控件”会因选中效果造成不能切换状态的情况,所以去掉“控件”可以被选中的能力:
input[type="checkbox"]:checked + label,
input[type="checkbox"]:not(:checked) + label {
(-prefix-)user-select: none;
}
这里的浏览器厂商前缀根据需要替换成相应的,查看简单示例。当然还需要提供聚焦以及禁用等状态的样式,就不在这里重复了。以上所有技术可同时适用于单选框(radio)。
本文出自:http://www.tuicool.com/articles/y67jee
css设置移动端checkbox和radio样式的更多相关文章
- Css实现checkbox及radio样式自定义
前言 checkbox和radio样式自定义在网页中是很常见的, 比如在进行表单输入时性别的选择,用户注册时选择已阅读用户协议.随着用户对产品体验要求越来越高,我们都会对checkbox和radio重 ...
- input美化 checkbox和radio样式
input美化 checkbox和radio样式 看惯了input[checkbox]和input[radio]默认样式,有没有想要改变一下呢?比如下面的样式: 比起html默认的样式,上图这些 ...
- 自定义checkbox, radio样式
17.2.25.nimil 今天开始做百度前端学院的任务,第一个是自定义checkbox, radio样式. checkbox和radio两个标签是不可以改变样式的,background-color. ...
- 自定义checkbox, radio样式总结
任务目的 深入了解html label标签 了解CSS边框.背景.伪元素.伪类(注意和伪元素区分)等属性的设置 了解CSS中常见的雪碧图,并能自己制作使用雪碧图 任务描述 参考 样例(点击查看),实现 ...
- 表单:checkbox、radio样式(用图片换掉默认样式)
checkbox.radio样式(用图片换掉默认样式) <!doctype html> <html> <head> <meta charset="u ...
- css自定义checkbox和radio样式
很常见的问题,也有许多人写过类似的文章,自己写来记录下 css代码如下: #myCheck + label,.myRadio + label{ width:16px; height:16px; bor ...
- css input checkbox和radio样式美化
参考:https://segmentfault.com/a/1190000004553258 http://www.haorooms.com/post/css_mh_ck_radio 思路都一样的,先 ...
- css设置中文字体(font-family:"黑体")后样式失效问题
做项目时偶遇的一诡异问题,同样的代码,在ff和IE7以上页面显示正常,但IE6无论怎么改都不起作用,本来以为是IE6的某些浮动bug所致,结果弄了很长时间也不行,后来不经意间把原来设定的font-fa ...
- 纯css用图片代替checkbox和radio,无js实现方法
html <ul id="is_offical_post_links"> <li> <label> <input type="c ...
随机推荐
- Codeforces Round #370 (Div. 2) D. Memory and Scores DP
D. Memory and Scores Memory and his friend Lexa are competing to get higher score in one popular c ...
- 【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句
例如:这SQL Sever中的一张数据表,想要将这张数据表中的数据 转化成一个一个的insert语句存储在txt的文档中,那么不论走到那里这个insert语句一执行,我们就能将这个数据表中的数据 ...
- 通过maven下载jar包
准备 你得先安装Maven如果不会点击后面的连接查看一二步.http://www.cnblogs.com/luoruiyuan/p/5776558.html 1.在任意目录下创建一个文件夹,其下创建一 ...
- js的一些小笔记,(不定期更新)
2个$的用法$本身并无特定意义,它表示什么意思要看是如何定义的,如果没有定义就便是两个$,可能是变量名的开始.一般是一个函数,用来代替document.getElementByIdfunction $ ...
- 编解码-protobuf
Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,Protobuf的优点. (1)在谷歌内部长期使用,产品成熟度高: (2)跨语言,支持多种语言,包括C++ ...
- CDN(内容分发网络)是什么?
尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.其目的是使用户可就近取得所需内容,解决Internet网络拥挤的状况,提高用户访问网站的响应速度. 解决CDN缓 ...
- idea 使用
1.idea对maven的兼容性优于eclipse,idea对于程序内部管理是模块,个人感觉也是优于eclipse. 2.idea默认设置编辑器字体是比较差的. 可以参考文章 http://www.3 ...
- js-错误处理与调试,JSON
错误处理与调试: 1.try-catch try{ window.someNoneXistentFunction(); }catch(error){ alert(error.message) } 2. ...
- ios 简单的倒计时验证码数秒过程实现
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) ...
- JSP通过IP获取用户(客户端)的地理位置信息
<%!//获取客户端的IP public String getRemoteIP(HttpServletRequest request) { if (request.getHeader(" ...