通过label标签重置input[radio]样式

本文介绍了如何利用label标签来重置input[type=radio]的样式,通过将input与label绑定,并结合span元素展示说明文字,实现美观的自定义样式。

一、基于默认的input[radio]标签很丑,很多时候都需要重写样式,下面就介绍下通过label标签重置input[radio]标签的方法;

二、准备首先我们需要给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>




<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>work</title> <style> button{ width: 231px; height: 50px; padding: 0; margin: 0; border: 0; } </style> </head> <body> <form> <fieldset> <h1>青春不常在,抓紧谈恋爱</h1> <hr /> <legend>征婚信息表</legend> <label>昵称:<input type="text" placeholder="请输入昵称" /></label> <br /><br /> 性别: <label for="male" ><img src="./day02/man.jpg" alt=""><input type="radio" name="" id="male" checked />男</label > <label for="female"><img src="./day02/women.jpg" alt=""><input type="radio" name="" id="female" />女</label> <br /><br /> 婚姻状况: <label for="nomarry" ><input type="radio" name="miss" id="nomarry" />未婚</label > <label for="marry" ><input type="radio" name="miss" id="marry" />已婚</label > <label> <input type="radio" name="miss" />保密 </label> <br /><br /> 喜欢的类型:<label><input type="checkbox" />可爱</label> <label> <input type="checkbox" />性感</label> <label> <input type="checkbox" />萝莉 </label> <label><input type="checkbox" />大叔</label> <label ><input type="checkbox" />小鲜肉 <input type="checkbox" />大叔</label ><br /><br /> 个人介绍:<br /><br /> <textarea cols="50" rows="10" required></textarea> <h3>我承诺</h3> <ul> <li>年满18岁、单身</li> <li>抱着严肃的态度</li> <li>真诚寻找另一半</li> </ul> <label><input type="checkbox" name="" id="" />我同意所有条款</label> <br /><br /> <button><img src="./day02/btn.png"></button> <button type="reset" >重置</button> </fieldset> </form> </body> </html> 检查代码,为什么运行出来的底部两个按钮不在同一行上
09-02
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值