本篇文章给大家分享的是关于css如何改变默认的radio和checkbox的样式 ,内容很不错,有需要的朋友可以参考一下,希望可以帮助到大家。
利用css的label的伪类(::before)代替checkbox和radio效果:
优点:需要图片来调整选中前和选中后的样式,纯css搞定
缺点:兼容性,IE8以下不支持
代码:
立即学习“前端免费学习笔记(深入)”;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>css改变默认的radio和checkbox的样式</title>
<style>
input[type="radio"],
input[type='checkbox'] {
display: none;
}
input[type='radio']+label::before,
input[type='checkbox']+label::before {
content: '';
display: inline-block;
width: 15px;
height: 15px;
margin-right: 6px;
border-radius: 100%;
vertical-align: middle;
border: 1px solid #E4E4E4;
background: #FFFFFF;
background-image: url('https://i.loli.net/2018/07/20/5b517d0bf066a.png');
background-position: 0px 0px;
}
input[type='radio']:hover+label::before,
input[type='checkbox']:hover+label::before {
background-position: -15px 0px;
}
input[type='radio']:checked+label::before,
input[type='checkbox']:checked+label::before {
background-position: -15px -15px;
}
</style>
</head>
<body>
<p>单选框</p>
<input type="radio" name="sex" value="man" id="man" checked>
<label for="man">男</label>
<input type="radio" name="sex" value="female" id="female">
<label for="female">女</label>
<p>多选框</p>
<input type="checkbox" name="fruits" value="apple" id="apple" checked>
<label for="apple">苹果</label>
<input type="checkbox" name="fruits" value="orange" id="orange">
<label for="orange">橙子</label>
</body>
</html>相关推荐:
以上就是css如何改变默认的radio和checkbox的样式的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号