css3更改input单选和多选的样式
2017-01-03 11:38
399 查看
在项目开发中我们经常会遇到需要更改input单选和多选样式的情况,今天就给大家介绍一种简单改变input单选和多选样式的办法。
在这之前先简单介绍一下:before伪类
:before 选择器向选定的元素前插入内容。使用content 属性来指定要插入的内容(content是必须的哦)。
相信这并不难以理解,接下来我们先理解思路:
(1)首先在html用label为 input 元素定义标记,也就是当你点击label标签时相应的input也会选中或取消
(2)接下来就是写css了,将input隐藏,只需要在label之前加入你的样式就好了,可以是图片也可以是自己画的圆圈,下面的这个例子是我写的圆,当然也可以替换成背景图。
input[type="radio"]+label:before是未选中状态的样式
input[type="radio"]:checked+label:before是选中状态的样式
<input type="radio" id="nationality1"><label for="nationality1">中国</label></div>
input[type="radio"]{ display:none; } input[type="radio"]+label{ position: relative; } input[type="radio"]+label:before{ content: ""; width:25px; height:25px; background: #ffffff; border:2px solid $gray; position: absolute; bottom:3px; left: -35px; border-radius: 50%; }
input[type="radio"]:checked+label:before{ content: ""; width: 25px; height: 25px; background: #34c0f5; position: absolute; bottom:3px; left: -35px; z-index: 99; border-radius: 50%; }
把radio替换成checkbox就是多选的啦。
注:隐藏和伪类定位是关键
相关文章推荐
- HTML单选按钮(Radio)样式更改以及多选按钮(Checkbox)样式更改
- 更改input等的默认样式
- CSS3样式控制input按钮效果
- CSS3 重置iphone浏览器按钮input,select等表单元素的默认样式
- File input 的样式和文字的更改方法__适用于Firefox、IE等浏览器
- 关于更改input中placeholder 样式
- input标签的radio单选,为什么成多选了
- input[type=file]样式更改以及图片上传预览
- Bootstrap中关于input里file的样式更改
- 更改<input type=file />的样式
- 更改input type="file" 的样式
- 更改上传文件的样式<input type="file>
- 更改input type=“date”等元素的默认样式
- 单选按钮、多选按钮用图片实现加样式
- css3更改radio的颜色和样式
- 自定义样式的单选、多选按钮逻辑
- File input 的样式和文字的更改方法__适用于Firefox、IE等浏览器
- 利用JavaScript更改input中radio和checkbox样式
- vue - v-model实现自定义样式の多选与单选
- vue v-model实现自定义样式多选与单选功能