用css实现html中单选框样式改变
我们都知道,input的单选框是一个小圆框,不能直接更改样式。但是我们在很多网页中看到的单选框样式可不仅限于默认的那个样式(看上去没啥新意,也比较丑)。那么,接下来我将介绍下如何实现该功能。
首先,让我们来看下单选框的实现:
在html中的input元素中,将其type属性值设置为radio,即为单选框,此时只需将要设置的单选选项的name属性设置成一致的,即可实现单选功能。
1 2 3 4 | 代码实现如下: < input type="radio" name="gender" value="male">男 < input type="radio" name="gender" value="female">女 |
要实现单选框样式改变,只需以下几步:
1、首先给出input的type属性为radio,设置相同name。同时用label将相应字段关联上该input属性(当label的for属性值与input的id属性值相同时 ,即可关联。关联上之后,点击字段即可选中相应的单选框)。
1 2 3 4 5 6 7 8 | html中:< br > < div > < input type="radio" name="paixu" id="paixu1" checked> < label for="paixu1" style="cursor:pointer">按热门排序</ label > < input type="radio" name="paixu" id="paixu2"> < label for="paixu2" style="cursor:pointer">按时间排序</ label > < input type="radio" name="paixu" id="paixu3"> < label for="paixu3" style="cursor:pointer">按评价排序</ label > </ div > |
2、设置input的display属性为none,将比较丑的那个框去掉。然后用伪元素添加相应的框。即在label中,用::before添加一个空心圆圈。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | div>input{ display: none; } div>label{ position: relative; margin-right: 34px; } div>label::before{ display: inline-block; content: ""; width: 16px; height: 16px; border-radius: 50%; border: 1px solid rgb(219, 219, 219); margin-right: 6px; vertical-align: bottom; } |
3、在input单选框在选中的时候,原有before的单选框变为红色实心框,同时添加label::after为白色圆,并将其定位到before的红色实心框中间。从而达到重叠的效果,实现红色圆框+白色圆心的效果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | div>input:checked+label::before{ background-color : rgb ( 239 , 66 , 56 ); } div>input:checked+label::after{ display : inline- block ; content : "" ; width : 6px ; height : 6px ; border-radius: 50% ; position : absolute ; left : 6px ; bottom : 6px ; background-color : white ; } |
是不是挺简单的呢?通过这样的方法还可以设置很多样式呢。
完整代码如下:
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 | <! DOCTYPE html> < html lang="en"> < head > < meta charset="UTF-8"> < meta name="viewport" content="width=device-width, initial-scale=1.0"> < meta http-equiv="X-UA-Compatible" content="ie=edge"> < title >Document</ title > < style > div>input{ display: none; } div>label{ position: relative; margin-right: 34px; } div>label::before{ display: inline-block; content: ""; width: 16px; height: 16px; border-radius: 50%; border: 1px solid rgb(219, 219, 219); margin-right: 6px; vertical-align: bottom; } div>input:checked+label::before{ background-color: rgb(239, 66, 56); } div>input:checked+label::after{ display: inline-block; content: ""; width: 6px; height: 6px; border-radius: 50%; position: absolute; left: 6px; bottom: 6px; background-color: white; } </ style > </ head > < body > < div > < input type="radio" name="paixu" id="paixu1" checked> < label for="paixu1" style="cursor:pointer">按热门排序</ label > < input type="radio" name="paixu" id="paixu2"> < label for="paixu2" style="cursor:pointer">按时间排序</ label > < input type="radio" name="paixu" id="paixu3"> < label for="paixu3" style="cursor:pointer">按评价排序</ label > </ div > </ body > </ html > |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 开发的设计和重构,为开发效率服务