通过label标签重置input[radio]样式
2018-01-12 16:40
531 查看
一、基于默认的input[radio]标签很丑,很多时候都需要重写样式,下面就介绍下通过label标签重置input[radio]标签的方法;
二、准备首先我们需要给lable标签绑定input,然后再通过一个标签包含说明文字(这里选用span标签),代码如下
五、完整代码
二、准备首先我们需要给lable标签绑定input,然后再通过一个标签包含说明文字(这里选用span标签),代码如下
<div class="wrap"> <input type="radio" id="option1" name="mode" value = "0" class="option-radio" checked /> <label id="firstLabel" for="option1"><span class="">这是一行优雅的代码</span> </label> <input type="radio" id="option2" name="mode" value = "1" class="option-radio" /> <label for="option2"><span class="">这是一行特别优雅的代码</span></label> </div>三、接下来要在样式上做文章了,先把input的原样式隐藏,然后给label标签用背景图代替原有样式。这里需要注意的是display的设置,因为label本身是行内元素,如果需要选项在一行设置inline-block,需要换行则设置block。否则无法给label设置宽高,点击文字时候将没法触发label。样式如下
.option-radio{ display: none; } .option-radio+label{ margin-top: 20px; display: block; height: 20px; width: 200px; text-indent: 23px; background: url("nocheacked.png") no-repeat left center; cursor: pointer; } .option-radio:checked+label{ background: url("cheacked.png") no-repeat left center; } #firstLabel{ width: 170px; } .wrap{ margin:15px; }四、效果图
五、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>改变单选样式</title>
</head>
<style>
.option-radio{ display: none; } .option-radio+label{ margin-top: 20px; display: block; height: 20px; width: 200px; text-indent: 23px; background: url("nocheacked.png") no-repeat left center; cursor: pointer; } .option-radio:checked+label{ background: url("cheacked.png") no-repeat left center; } #firstLabel{ width: 170px; } .wrap{ margin:15px; }
</style>
<body>
<div class="wrap">
<input type="radio" id="option1" name="mode" value = "0" class="option-radio" checked />
<label id="firstLabel" for="option1"><span class="">这是一行优雅的文字</span> </label>
<input type="radio" id="option2" name="mode" value = "1" class="option-radio" />
<label for="option2"><span class="">这是一行特别优雅的文字</span></label>
</div>
</body>
</html>
相关文章推荐
- Flex中通过设置textAlign和labelStyleName样式设置FormItem容器中标签文本对齐方式的例子
- Flex中通过textAlign样式在一个按钮(Button)控件中设置标签(label)对齐方式的例子
- 用 label 来扩展 input 标签
- css input checkbox和radio样式美化
- Razor 标签语法(1-11) Label,Text,Hidden,Password,Radio,CheckBox,DropdownList,Href,Img,Css,JS
- input标签的radio单选,为什么成多选了
- css input[type=file] input[type=radio] input[type=checkbox]样式美化,input上传按钮美化
- CSS3 重置iphone浏览器按钮input,select等表单元素的默认样式
- Bootstrap 通过一些简单的 HTML 标签和扩展的类即可创建出不同样式的表单。
- 通过citrix client 打印label 标签
- htmlcss笔记--标签默认值样式重置css reset(2)
- html中通过css改变input样式特效
- 从重置input file标签中看jQuery的 .val() 和 .attr(“value”) 区别
- 自定义input[type="radio"]的样式
- jQuery的change方法什么时候触发? 你绝对意想不到,即使你通过jQuery修改了input标签的value值(即修改内存中的DOM树中的input),也不会触发change事件,不会,不会
- 用JS实现input中的file标签的样式(当要修改file标签的字体样式时使用)
- input标签样式
- HTML学习笔记---input,form,label标签
- 怎样在radio类型的input标签中动态的加checked
- file 标签上传文件,直接样式太难看!隐藏它,通过别的按钮触发它,又会有IE兼容问题。