在项目开发中我们经常会遇到需要更改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></p>
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就是多选的啦。
注:隐藏和伪类定位是关键
以上就是css更改input单选和多选的样式详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号