整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

CSS美化单选框 radio 、多选框 checkbox 和 switch开关按钮

adio、checkbox和switch应该是一个比较常用的html标签,尤其是在中后台ERP系统里面更为常见。不过浏览器自带的样式不怎么好看,而且不同浏览器效果也不一样。出于美化和统一视觉效果的需求,自定义样式就被提出来了。

实现思路

纯css实现的主要手段是利用label标签的模拟功能。label的for属性可以关联一个具体的input元素,即使这个input本身不可被用户可见,有个与它对应的label后,用户可以直接通过和label标签交互来替代原生的input——而这给我们的样式模拟留下了空间。简而言之就是:

隐藏原生input,样式定义的过程留给label (那为什么不直接改变checkbox的样式?因为checkbox作为浏览器默认组件,样式更改上并没有label那么方便,很多属性对checkbox都是不起作用的,比如background,而label在样式上基本和div一样'任人宰割')

而在选择事件上,由于css的“相邻选择符(E+F)”的存在,让我们可以直接利用html的默认checkbox,免去了js模拟选择的麻烦。

准备知识

DEMO的部分CSS3属性只写了webkit前缀,所以建议用webkit内核的浏览器查看本页案例,当然只要你给样式补上对应的浏览器前缀,就可以实现更多样式匹配

HTML代码:

 <!-- input的id必须有,这个是label进行元素匹配所必需的 -->
 <!-- 可以看到每个input的id和label的“for”属性对应同一字符串 -->
<input type="checkbox" id="checkbox01" />
<label for="checkbox01"></label>

<input type="checkbox" id="checkbox02" />
<label for="checkbox02"></label>

<input type="checkbox" id="checkbox03" />
<label for="checkbox03"></label>

<input type="checkbox" id="checkbox04" />
<label for="checkbox04"></label>

HTML构建完成,接下来是对应的css:

/* 隐藏所有checkbox */
input[type='checkbox'] {
 display: none;
}
/* 对label进行模拟.背景图片随便拼凑的,不要吐槽品味*/
/* transition效果是做个背景切换效果,这里单纯演示而已,实际上这个过渡不加更自然*/
label {
 display: inline-block;
 width: 60px;
 height: 60px;
 position: relative;
 background: url(//www.chitanda.me/images/blank.png);
 background-position: 0 0px;
 -webkit-transition: background 0.5s linear;
}
/* 利用相邻选择符和checkbox`:checked`的状态伪类来模拟默认选中效果(就是点击后那个勾号的效果) */
/*如果这段代码注释,点击后将没有任何反馈给用户*/
/*因为label本身是没有点击后被选中的状态的,checkbox被隐藏后,这个状态只能手动模拟*/
input[type='checkbox']:checked+label {
 background-position: 0 -60px;
}

上面代码看起来好像也可以了。不过仔细想想,貌似缺了点什么:选项对应的提示文字

对css不了解的新人可能这时候第一反应就是在label后面用p标签或者span标签来添加文字。不过这种方式都不怎么优雅。个人建议用css的::before和::after伪元素(::before和:before是一个东西。不过为了把“伪元素”和“伪类”区分出来,W3C建议的写法是伪元素用::而伪类用:)

/* 伪元素的生效很简单,定义`content`就好,其余的属性和普通div一样 */
label::after {
 content: attr(data-name);
 /*利用attr可以减少css代码量,data-name写在html部分的label属性里*/
 display: inline-block;
 position: relative;
 width: 120px;
 height: 60px;
 left: 100%;
 vertical-align: middle;
 margin: 10px;
}

当然既然可以用::after模拟label的文字,那也就可以用::before模拟label的checkbox样式,这里就不做解析了。

这里提一下伪类和伪元素的区分:

1)伪类:存在的意义是为了通过选择器找到那些不存在于DOM树中的信息以及不能被常规CSS选择器获取到的信息。 伪类由一个冒号:开头,冒号后面是伪类的名称和包含在圆括号中的可选参数。

常用的伪类:

:active 向被激活的元素添加样式。 
:focus 向拥有键盘输入焦点的元素添加样式。 
:hover 当鼠标悬浮在元素上方时,向元素添加样式。 
:link 向未被访问的链接添加样式。 
:visited 向已被访问的链接添加样式。 
:first-child 向元素的第一个子元素添加样式。 
:checked 向选中的控件元素添加样式

2)伪元素:伪元素在DOM树中创建了一些抽象元素,这些抽象元素是不存在于文档语言里的(可以理解为html源码);

注意: css3为了区分伪类和伪元素,规定伪类前面有一个冒号,伪元素前面有两个冒号

常用伪元素:

关于伪元素的讲解,可以参考CSS伪类与伪元素总是傻傻分不清,这份总结够面试用了

::before 为作用元素的第一个子节点插入dom中
::after 为作用元素的最后一个子节点插入dom中
  • 同:都是通过选择器为元素添加样式
  • 异:伪元素会创建一个元素,但不是真正的Html元素,伪类相当于为一个元素创建一个class样式

实例

自定义radio

html代码:

<input type="radio" id="radio">
<label for="radio"></label>

css代码:

input{
 display:none;
}
label {
 display: inline-block;
 width: 30px;
 height: 30px;
 border: 1px solid #333;
 border-radius: 50%;
 position: relative;
}
label::after {
 -webkit-transition: all .5s ease;
 -moz-transition: all .5s ease;
 -o-transition: all .5s ease;
 -ms-transition: all .5s ease;
 transition: all .5s ease;
 cursor: pointer;
 position: absolute;
 width: 16px;
 height: 16px;
 border-radius: 50%;
 top: 50%;
 left: 50%;
 margin-top:-8px;
 margin-left:-8px;
 z-index: 1;
 content: '';
 border:1px solid #333;
}
input:checked+label::after{
 background:red;
}

实现效果:

点击前和点击后:

自定义checkbox

漂亮的checkbox长这样的,看着就很可爱

我们可以不追求那么完美的样式,可以实现下面简单好看的样式就可以

html代码:

<input type="checkbox" id="checkbox">
<label for="checkbox"></label>

css代码:

input{
 display:none;
}
label {
 display: inline-block;
 width: 30px;
 height: 30px;
 border: 1px solid #333;
 position: relative;
}
label::after {
 -webkit-transition: opacity .5s ease;
 -moz-transition: opacity .5s ease;
 -o-transition: opacity .5s ease;
 -ms-transition: opacity .5s ease;
 transition: opacity .5s ease;
 cursor: pointer;
 position: absolute;
 content: '';
 opacity: 0;
}
input:checked+label::after{
 border: 2px solid #d73d32;
 border-top: none;
 border-right: none;
 -webkit-transform: rotate(-45deg);
 -ms-transform: rotate(-45deg);
 transform: rotate(-45deg);
 width:20px;
 height:10px;
 top:50%;
 margin-top:-8px;
 left:50%;
 margin-left:-10px;
 opacity: 1.0;
}

实现效果:

点击前和点击后:

自定义switch

继续分享一个iOS风格的switch开关按钮,样子也非常常见,如图:

主要是使用了<input ?type="checkbox">来模拟实现,具体的HTML:

html 代码:

<label><input class="mui-switch" type="checkbox"> 默认未选中</label>

<label><input class="mui-switch" type="checkbox" checked> 默认选中</label>

<label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默认未选中,简单的背景过渡效果,加mui-switch-animbg类即可</label>

<label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默认选中</label>

<label><input class="mui-switch mui-switch-anim" type="checkbox"> 默认未选中,过渡效果,加 mui-switch-anim
类即可</label>

<label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默认选中</label>

在实际的使用中后来又增加了两个过渡效果,分别加?mui-switch-animbg和mui-switch-anim?类即可,具体效果查看下面的demo页面。

CSS代码(SCSS导出的,排版有些奇怪):

css 代码:

剩下部分

这里给出具体的css,方便大家复制本地实现

<style>
 .mui-switch {
 width: 52px;
 height: 31px;
 position: relative;
 border: 1px solid #dfdfdf;
 background-color: #fdfdfd;
 box-shadow: #dfdfdf 0 0 0 0 inset;
 border-radius: 20px;
 border-top-left-radius: 20px;
 border-top-right-radius: 20px;
 border-bottom-left-radius: 20px;
 border-bottom-right-radius: 20px;
 background-clip: content-box;
 display: inline-block;
 -webkit-appearance: none;
 user-select: none;
 outline: none;
 }
 .mui-switch:before {
 content: '';
 width: 29px;
 height: 29px;
 position: absolute;
 top: 0px;
 left: 0;
 border-radius: 20px;
 border-top-left-radius: 20px;
 border-top-right-radius: 20px;
 border-bottom-left-radius: 20px;
 border-bottom-right-radius: 20px;
 background-color: #fff;
 box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
 }
 .mui-switch:checked {
 border-color: #64bd63;
 box-shadow: #64bd63 0 0 0 16px inset;
 background-color: #64bd63;
 }
 .mui-switch:checked:before {
 left: 21px;
 }
 .mui-switch.mui-switch-animbg {
 transition: background-color ease 0.4s;
 }
 .mui-switch.mui-switch-animbg:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-animbg:checked {
 box-shadow: #dfdfdf 0 0 0 0 inset;
 background-color: #64bd63;
 transition: border-color 0.4s, background-color ease 0.4s;
 }
 .mui-switch.mui-switch-animbg:checked:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-anim {
 transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s;
 }
 .mui-switch.mui-switch-anim:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-anim:checked {
 box-shadow: #64bd63 0 0 0 16px inset;
 background-color: #64bd63;
 transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s;
 }
 .mui-switch.mui-switch-anim:checked:before {
 transition: left 0.3s;
 }
 /*# sourceMappingURL=mui-switch.css.map */
</style>

如果你喜欢scss,那么代码更加简洁

@mixin borderRadius($radius:20px) {
 border-radius: $radius;
 border-top-left-radius: $radius;
 border-top-right-radius: $radius;
 border-bottom-left-radius: $radius;
 border-bottom-right-radius: $radius;
 }
 $duration: .4s;
 $checkedColor: #64bd63;
 .mui-switch {
 width: 52px;
 height: 31px;
 position: relative;
 border: 1px solid #dfdfdf;
 background-color: #fdfdfd;
 box-shadow: #dfdfdf 0 0 0 0 inset;
 @include borderRadius();
 background-clip: content-box;
 display: inline-block;
 -webkit-appearance: none;
 user-select: none;
 outline: none;
 &:before {
 content: '';
 width: 29px;
 height: 29px;
 position: absolute;
 top: 0px;
 left: 0;
 @include borderRadius();
 background-color: #fff;
 box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
 }
 &:checked {
 border-color: $checkedColor;
 box-shadow: $checkedColor 0 0 0 16px inset;
 background-color: $checkedColor;
 &:before {
 left: 21px;
 }
 }
 &.mui-switch-animbg {
 transition: background-color ease $duration;
 &:before {
 transition: left 0.3s;
 }
 &:checked {
 box-shadow: #dfdfdf 0 0 0 0 inset;
 background-color: $checkedColor;
 transition: border-color $duration, background-color ease $duration;
 &:before {
 transition: left 0.3s;
 }
 }
 }
 &.mui-switch-anim {
 transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration;
 &:before {
 transition: left 0.3s;
 }
 &:checked {
 box-shadow: $checkedColor 0 0 0 16px inset;
 background-color: $checkedColor;
 transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3;
 &:before {
 transition: left 0.3s;
 }
 }
 }
 }

链接文章

https://www.html.cn/archives/9274

https://segmentfault.com/a/1190000003711140

没有觉得默认的HTML 的SELECT 的下拉选择框丑到爆呢,一点没有让人想看第二遍的欲望呢。

Select-or-Die基于 jQuery 的下拉框美化插件来了(Select-or-Die 还另外提供了 3 套皮肤)

GitHub地址:https://github.com/vestman/Select-or-Die/ (没有下载地址的都是耍流氓)

首先上效果图

select美化插件 图片来源网络

Select-or-Die 的兼容性怎么样?

浏览器兼容:兼容 IE8 及以上 IE 浏览器和其他主流现代浏览器。

jQuery 兼容:兼容 1.7 及以上版本。

使用方法(这个才是重点)

  1. 引入CSS 和JS

    <link rel="stylesheet" href="css/selectordie.css">

    <script src="js/jquery.min.js"></script>

    <script src="js/selectordie.min.js"></script>

  2. 绑定元素美化(JavaScript)

$(function(){

$('select').selectOrDie();

});

望收藏了我写的文章的你同时可以关注一下“小海前端”,因为这些文章都是连载的,并且是经过我系统的归纳过的。

【技术等级】初级

【承接文章】《CSS实现图片精灵,原来要这样使用背景属性,前端设计师必备知识

本文小海老师为大家讲解利用CSS处理HTML中的列表,也就是CSS有关列表的属性。本文属于前端开发的初级教程,适合于刚刚开始接触CSS技术的学习者。

列表属性是指可以对HTML中的<ol></ol>标记对和<ul></ul>标记对进行样式设置的属性。这一组CSS属性包括以下三个:

  • list-style-type

  • list-style-image

  • list-style-position

一、设置列表的项目符号或编号:

CSS技术使用 list-style-type 设置列表的项目符号或编号

在HTML中,主要操作的列表有两种:

  • 无序列表:用<ul></ul>标记对实现。无序列表项中左侧的标识我们把它称为“项目符号”。

  • 有序列表:用<ol></ol>标记对实现。有序列表项中左侧的标识我们把它称为“编号”。

有序列表与无序列表

CSS技术利用 list-style-type 属性来设置HTML列表左侧标识的样式。并且在CSS看来,<ul></ul>和<ol></ol>两个标记对不再进行有序和无序的区分,使用list-style-type属性设置为哪个取值,就是对应的哪种列表。

该属性包括以下几种取值:

  • none,列表项无标记。

  • disc,默认值,标记为实心圆。

  • circle,标记为空心圆。

  • square,标记为实心方块。

  • decimal,标记为数字。

  • decimal-leading-zero,标记为0开头的数字(01、02、03 等)。

  • lower-roman,标记为小写罗马数字(i、ii、iii等)。

  • upper-roman,标记为大写罗马数字(I、II、III等)。

  • lower-alpha,标记为小写英文字母(a、b、c等)。

  • upper-alpha,标记为大写英文字母(A、B、C等)。

二、使用自定义图片来代替项目符号和编号:

CSS技术利用 list-style-image 属性来设置列表左侧的标识为指定的图片

CSS技术利用 list-style-image 属性来设置列表左侧的标识为指定的图片。

格式:list-style-image:url(Image_URL);

例如:ul{list-style-image:url(../images/01.jpg);}

三、设置列表中列表项的缩进:

CSS技术利用 list-style-position 属性来设置列表项的缩进

CSS技术利用 list-style-position 属性来设置列表项的缩进。

该属性包括以下两种取值:

  • loutside,默认值,列表项不缩进。

  • linside,列表项缩进。

这个属性使用在列表项<li></li>标记对上的,不能用在<ol></ol>或<ul></ul>之上。

文章预告

下一篇文章中,小海老师会继续为大家向下讲解CSS属性,下一次我们讲解CSS中最为重要的一组属性:定位属性。这是CSS中非常常用的一组属性,希望大家千万不要错过!

小海教材

如果大家希望得到更加全面的关于HTML和CSS技术讲解的内容,可以私信我,我会免费将小海老师自己编写的HTML和CSS的PDF教材发给你,帮助你在前端开发的道路上阔步前行。

小海声明

在头条上发表的这些文章都是从前端开发的基础开始一步一步讲起的。我非常希望能有更多的前端开发初学者通过我写的文章,逐步学到一定的知识,甚至慢慢有了入门的感觉。这些文章都是我这几年教学过程中的经验,每写一篇时我都尽量把握好措辞,用简单易懂的语言描述,同时精心设计版面,让版面更加丰富,激发阅读兴趣。所以,每一篇文章可能篇幅不长,但是都要耗费小海老师很久的时间。

希望收藏了我写的文章的你同时可以关注一下“小海前端”,因为这些文章都是连载的,并且是经过我系统的归纳过的。

关注“小海前端”,我会继续为大家奉上更加深入的前端开发文章,也希望更多的初学者跟着学下去,我们共同将前端开发的路努力坚持的走下去。