单选框(radio)自定义样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//html
<input type="radio" />
 
//css部分
<style>
    /**
    * 单选框自定义样式
    **/
    input[type=radio]{
        /*去除浏览器默认样式*/
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        /*自定义样式*/
        position: relative;
        display: inline-block;
        vertical-align: top;
        width: 20px;
        height: 20px;
        border: 1px solid #00bfff;
        outline: none;
        cursor: pointer;
        /*设置为圆形,看起来是个单选框*/
        -webkit-border-radius: 20px;
        -moz-border-radius: 20px;
        border-radius: 20px;
    }
 
    /**
    * 单选框 选中之后的样式
    **/
    input[type=radio]:after{
        content: '';
        position: absolute;
        width: 12px;
        height: 12px;
        display: block;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: auto;
        background: #00bfff;
        -webkit-border-radius: 12px;
        -moz-border-radius: 12px;
        border-radius: 12px;
        -webkit-transform: scale(0);
        -moz-transform: scale(0);
        transform: scale(0);
        /*增加一些动画*/
        -webkit-transition : all ease-in-out 300ms;
        -moz-transition : all ease-in-out 300ms;
        transition : all ease-in-out 300ms;
    }
    input[type=radio]:checked:after{
        -webkit-transform: scale(1);
        -moz-transform: scale(1);
        transform: scale(1);
    }
</style>

appearance 属性介绍

appearance是css3的属性,他的意思是使得标签的样式像他设定的参数一样;

他总共有7个参数;

normal->> 将元素呈现为常规元素。

icon->> 将元素呈现为图标(小图片)。

window->> 将元素呈现为视口。

button->> 将元素呈现为按钮。

menu->> 将元素呈现为一套供用户选择的选项。

field->> 将元素呈现为输入字段。

none->> 去除浏览器默认样式

:checked伪类介绍

:checked同样是css3中的一个伪类,他的作用是某个标签被选中时来使用的,使用方法和:hover :active :link这些伪类一样;

上面我在radio后面加了一个伪类:after,他要稍微比定义的单选框要小点,这样显示出来更加美观一点,在未选中之前让他scale(0),然后配合动画,在选中的时候scale(1),这样就有一个渐变填充的动画了;

那么radio的自定义样式就这样了,最后呈现的样式如下图:

复选框(checkbox)自定义样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* html代码
**/
<input type="checkbox" />
 
/**
* css代码
**/
<style>
    input[type=checkbox]{
        margin: 50px;
        /*同样,首先去除浏览器默认样式*/
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        /*编辑我们自己的样式*/
        position: relative;
        width: 20px;
        height: 20px;
        background: transparent;
        border:1px solid #00BFFF;
        -webkit-border-radius: 4px;
        -moz-border-radius: 4px;
        border-radius: 4px;
        outline: none;
        cursor: pointer;
    }
    input[type=checkbox]:after{
        content: '√';
        position: absolute;
        display: block;
        width: 100%;
        height: 100%;
        background: #00BFFF;
        color: #fff;
        text-align: center;
        line-height: 18px;
        /*增加动画*/
        -webkit-transition: all ease-in-out 300ms;
        -moz-transition: all ease-in-out 300ms;
        transition: all ease-in-out 300ms;
        /*利用border-radius和opacity达到填充的假象,首先隐藏此元素*/
        -webkit-border-radius: 20px;
        -moz-border-radius: 20px;
        border-radius: 20px;
        opacity: 0;
    }
    input[type=checkbox]:checked:after{
        -webkit-border-radius: 0;
        -moz-border-radius: 0;
        border-radius: 0;
        opacity: 1;
    }
</style>

写法和radio思路一致,首先都是去除浏览器样式,然后自定义样式,这里填充的那种动画,就是利用渐现和圆弧填充为四个角的这么个思路,其实css还是有很多地方挺有意思的,大家平时多挖掘就会发现了;

最后样式如下:

再来看一种开关模式的复选框;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
input[type=checkbox]{
    /*同样,首先去除浏览器默认样式*/
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    /*编辑我们自己的样式*/
    position: relative;
    background: #fff;
    border: 1px solid #00BFFF;
    width: 56px;
    height: 28px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    /*增加动画*/
    -webkit-transition: all ease-in-out 300ms;
    -moz-transition: all ease-in-out 300ms;
    transition: all ease-in-out 300ms;
    outline: none;
    cursor: pointer;
}
input[type=checkbox]:after{
    content: 'off';
    position: absolute;
    left: 2px;
    top: 2px;
    display: block;
    width: 22px;
    height: 22px;
    background: #00BFFF;
    color: #fff;
    text-align: center;
    line-height: 22px;
    /*增加动画*/
    -webkit-transition: all ease-in-out 300ms;
    -moz-transition: all ease-in-out 300ms;
    transition: all ease-in-out 300ms;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    font-size: 12px;
}
input[type=checkbox]:checked{
    background: #00BFFF;
}
input[type=checkbox]:checked:after{
    content: 'on';
    left: 30px;
    background: #fff;
    color: #00BFFF;
}

这里就是对上面普通的复选框稍微做了一些改变,首先宽度增大,自身增加一个动画,不然背景变化没有渐变;

然后伪元素位置根据选中和未选中来确定left的值和content中的值就是图中的on与off的转变;

最后样子见下图:

纯CSS3来自定义单选框radio与复选框checkbox的更多相关文章

  1. 用jquery修改默认的单选框radio或者复选框checkbox选择框样式

    默认的radio和checkbox选框很难看.我去看了一下qq注册的页面.发现单选和复选框并没有用<input>,居然是用是A标签.然后用css背景图片展示选择框,用JavaScript控 ...

  2. 微信小程序:单选框radio和复选框CheckBox

    单选框radio: 可以通过color属性来修改颜色. 复选框checkbox:

  3. JS中获取页面单选框radio和复选框checkbox中当前选中的值

    单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意< ...

  4. 【JavaScript&jQuery】单选框radio,复选框checkbox,下拉选择框select

    HTML: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...

  5. 组合框里添加复选框的方法(使用勾选的假象,用图片代替而已,并非QT原生支持)

    组合框可以看作是列表框和文本框的组合,因其占据的空间少,使用操作方便,常被界面设计人员用于界面开发设计中,在有限个输入的条件下,组合框常用来代替文本框,这样从用户使用角度来看,更趋人性化,所见即所得. ...

  6. 自动化测试基础篇--Selenium单选框(Radio)复选框(CheckBox)

    摘自:https://www.cnblogs.com/sanzangTst/p/7686602.html 一.什么是单选框.复选框? 二.单选框:radio 三.复选框:checkbox 四.判断是否 ...

  7. 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)

    原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1, ...

  8. (七)对话框,单选框(radiobox),复选框(checkbox),列表框(ListBox),组合框(CComboBox),水平滚动条(Horizontal scroll bar),微调(旋转)spincontrol,列表视图控件CListCtrl,静态控件static

    1,模态对话框和非模态对话框 // 模态对话框 void CMainFrame::OnDialogExec() { // TODO: 在此添加命令处理程序代码 // 创建对话框对象 CDialog d ...

  9. excel添加复选框和去掉复选框

    添加复选框 我测试的excel版本是最新版2016,所有版本都是找开发者工具里面包含很多工具呢,大家可以慢慢测试 excel的右上角 点击文件-->选项-->自定义功能区-->添加开 ...

随机推荐

  1. 删除oracle数据库用户的dba权限(当出现同一用户DBA可以登录,normal不能登录)“无法对SYS拥有的对象创建触发器”

    系统报错:“无法对SYS拥有的对象创建触发器”,搞不懂是什么原因了,到底这触发器要用什么用户才能建立啊? ORA-04089: 无法对 SYS 拥有的对象创建触发器 第一种方式: 首先,用sys用户a ...

  2. EF3:Entity Framework三种开发模式实现数据访问

    前言 Entity Framework支持Database First.Model First和Code Only三种开发模式,各模式的开发流程大相径庭,开发体验完全不一样.三种开发模式各有优缺点,对 ...

  3. python操作dataFrame

    python数据分析工具pandas中DataFrame和Series作为主要的数据结构. 本文主要是介绍如何对DataFrame数据进行操作并结合一个实例测试操作函数. 1)查看DataFrame数 ...

  4. [HNOI 2007] 紧急疏散

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1189 [算法] 首先 , 答案具有单调性 , 不妨二分答案” 第mid秒是否可以完成 ...

  5. 解析javascript变量

    //add by tim//提供解析javascript 脚本的变量集合 using System;using System.Collections.Generic;using System.Linq ...

  6. hdu5335(搜索)

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  7. zoj3955:Saddle Point(想法题)

    传送门 题意 给出n*m的矩阵,询问所有子矩阵中鞍点的个数 鞍点定义:在行唯一最小,在列唯一最大 分析 我们遍历每个点,计算该点对于答案的贡献即可. 每个点的贡献为\((2^{numa[i][j]}) ...

  8. thinkphp 中 dump 函数调试数组时显示不全解决方法

    在php.ini里的 xdebug 节点中,加入如下 xdebug.var_display_max_children=128 #子级节点最多显示的的字符数xdebug.var_display_max_ ...

  9. 【爬坑系列】之docker的overlay网络配置(未完,待续)

    理论知识储备: 想了解vxlan网络的知识:https://www.cnblogs.com/shuiguizi/p/10923841.html 想了解docker网络的原理知识:https://www ...

  10. 环境变量解释以及在Linux下的环境变量设置

    一.环境变量解释 环境变量是什么? 引用百度百科里面的解释:环境变量是操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows系统中的path环境变量,当要求 ...