CSS 实现自定义样式的单选框与多选框
来源:互联网 发布:淘宝上的dota2 编辑:程序博客网 时间:2024/06/05 02:05
前端在开发的过程中,经常会遇见使用单选框以及多选框的情况,但是默认的选框按钮的样式单一,一般我们需要去自定义一些选框按钮的样式;
通常情况下,单选、多选为方便自定义样式,一般会采用input+label去实现,这里实现的原理主要是运用了label标签的for属性;
for 属性规定 label 与哪个表单元素绑定。
1. 单选框
实现方式:input type=radio + label
HTML:
<body> <div class="box"> <input type="radio" id="radio1" name="radio" checked="checked"/><label for="radio1">选项一</label> </div> <div class="line"></div> <div class="box"> <input type="radio" id="radio2" name="radio"/><label for="radio2">选项二</label> </div> <div class="line"></div> <div class="box"> <input type="radio" id="radio3" name="radio"/><label for="radio3">选项三</label> </div> <div class="line"></div> <div class="box"> <input type="radio" id="radio4" name="radio"/><label for="radio4">选项四</label> </div> <div class="line"></div> <div class="box"> <input type="radio" id="radio5" name="radio"/><label for="radio5">选项五</label> </div> <div class="line"></div></body>
CSS:
<style type="text/css"> *{ margin: 0px; padding: 0px; border: none; box-sizing: border-box; outline: none; } .box{ width: 100%; height: 50px; text-align: center; } input{ display: none; } label{ width: 100%; height: 100%; display: inline-block; position: relative; line-height: 50px; color: #999; } label:active{ background: #EEEEEE; } label:after{ content: "";/*必须设置*/ display: inline-block; width: 20px; height: 20px; border: 1px solid green; position: absolute; top: 15px; left: 15px; border-radius: 20px; } input:checked+label:after{ background-color: green; } .line{ width: 100%; height: 1px; background: #ccc; opacity: 0.2; } </style>
2. 多选框
实现方式:input type=checkbox + label
HTML:
<body> <div class="box"> <input type="checkbox" id="checkbox1"/><label for="checkbox1">选项一</label> </div> <div class="line"></div> <div class="box"> <input type="checkbox" id="checkbox2"/><label for="checkbox2">选项二</label> </div> <div class="line"></div> <div class="box"> <input type="checkbox" id="checkbox3"/><label for="checkbox3">选项三</label> </div> <div class="line"></div> <div class="box"> <input type="checkbox" id="checkbox4"/><label for="checkbox4">选项四</label> </div> <div class="line"></div> <div class="box"> <input type="checkbox" id="checkbox5"/><label for="checkbox5">选项五</label> </div> <div class="line"></div> </body>
css:
<style type="text/css"> *{ margin: 0px; padding: 0px; border: none; box-sizing: border-box; outline: none; } .box{ width: 100%; height: 50px; } input{ display: none; } label{ width: 100%; height: 50px; display: inline-block; line-height: 50px; position: relative; text-align: center; } label:active{ background: #EEEEEE; } label:after{ content: ""; width: 20px; height: 20px; border: 1px solid green; border-radius: 20px; display: inline-block; position: absolute; top: 15px; left: 15px; } input:checked+label:after{ background-color: green; } .line{ width: 100%; height: 1px; background: #CCCCCC; opacity: 0.3; } </style>
CSS自定义选框样式Demo下载
阅读全文
0 0
- CSS 实现自定义样式的单选框与多选框
- 最简单清晰的纯css实现的单选框radio复选框checkbox自定义样式
- css 实现自定义下拉框样式
- CSS自定义滚动条样式与颜色
- 更改checkbox默认样式、radio默认样式,CSS实现自定义
- xml的命名空间与css样式实现
- css样式实现的箭头
- 自定义CSDN Blog的CSS样式
- 自定义CSDN Blog的CSS样式
- 自定义CSDN Blog的CSS样式
- 参数报表使用自定义的CSS样式
- Chrome自定义CSS样式的方法
- 自定义光标样式的CSS代码
- 浅谈CSS自定义下拉列表的样式
- 自定义CSDN Blog的CSS样式
- 自定义css样式
- EXTJS4 自定义CSS样式
- CSS 自定义列表样式
- 阿里巴巴开源DataSource项目-Druid
- 实现ListView的下拉刷新和上拉加载
- 机器学习笔记十四 朴素贝叶斯实例
- linux I/O状态实时监控iostat
- 四种方法解析XML文档:Dom、SAX、JDOM、dom4j
- CSS 实现自定义样式的单选框与多选框
- 左程云_算法与数据结构 — 链表问题 — 06环形单链表的约瑟夫问题_normal&advanced
- LOGBACK 配置: 用 XML
- Spark RDD和Dataset区别
- shellScript之util语句
- 【脚本语言系列】关于Python结构化文本文件处理JSON,你需要知道的事
- Spinal Tap Case
- 机器学习笔记十五 AdaBoost(未完)
- com.microsoft.sqlserver.jdbc.SQLServerException: 将截断字符串或二进制数据。