使用 CSS 修改 HTML 默认单选(radio)和复选框(checkbox)样式(网页版)

2 篇文章 0 订阅

HTML 默认的单选和复选框有多丑大家都是有目共睹,所以我们UI设计的小哥哥小姐姐们在设计时候,为了美观经常会设计一些漂亮的单选或者复选框,这就要求我们前端开发童鞋必须去修改HTML单选复选框的默认样式,当然修改的方式有很多种,我在这里展示的是如何用CSS来修改。

这里我主要以我做的一个记住密码的复选框来做示例

首先上 HTML 代码

<label for="remmber">
    <input class="remmber" type="checkbox" name="remmber" id="remmber">
    <span></span>
    记住密码
</label>

CSS代码

方法一:使用纯 CSS 进行选中框和对勾的样式设置

第一步: 给原生复选框设置样式使其隐藏
.remmber {
	display: none;
}
第二步:设置未选中时复选框的样式,编写span的样式使其显示为一个正方形的黑色框
.remmber[type=checkbox]+span {
	display: inline-block;
	border-radius: .05rem;
	width: .28rem;
	height: .28rem;
	border: .02rem solid #0D1529;
	color: #0D1529;
	position: absolute;
	top: 0;
	left: -.5rem;
}

这里写图片描述

最后一步:设置复选框选中状态,使用伪类 :aftercontent 属性添加特殊符号对号表示选中
.remmber[type=checkbox]:checked+span:after {
	content: '\2714';
	position: absolute;
	font-size: 0.28rem;
	left: 0.05rem;
}

这里写图片描述

上述 content 中的特殊字符如果有需要也可以在我的另一篇文章《常用的HTML和CSS content属性特殊字符归纳》中进行查询。

方法二:使用背景图的方式进行选择框和对勾的样式设置。

这就更简单了,只需要在未选中时设置未选中的背景图

.remmber[type=checkbox]+span {
	display: inline-block;
	width: .28rem;
	height: .28rem;
	background: url(未选中图片的图片路径) no-repeat;
}

选中时设置选中的背景图就可以了。

.remmber[type=checkbox]:checked+span {
	display: inline-block;
	width: .28rem;
	height: .28rem;
	background: url(选中图片的图片路径) no-repeat;
}

上述就是我常用的使用 CSS 修改 HTML 默认复选框样式的方法,修改单选框的方法是一样的,只需要把 HTML 和 CSS 中的checkbox 改成 radio 就可以了。

如果对优化修改微信小程序初始单选复选框有需求的小伙伴,可以看我的另一篇博文《优化修改微信小程序原生单选(radio)复选(checkbox)框样式》

  • 13
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
几个风格好看的CSS3单选复选按钮美化样式,简单的表单单选框、复选框美化代码。 <!DOCTYPE HTML> <html> <head> <title>好看的CSS3单选复选按钮美化样式</title> <link rel="stylesheet" type="text/css" href="css/style.css?3.1.64" /> </head> <body>[removed][removed] <div id="holder"> <div> <div class="tag">Checkbox Small</div> <input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /><label for="checkbox-1-1"></label> <input type="checkbox" id="checkbox-1-2" class="regular-checkbox" /><label for="checkbox-1-2"></label> <input type="checkbox" id="checkbox-1-3" class="regular-checkbox" /><label for="checkbox-1-3"></label> <input type="checkbox" id="checkbox-1-4" class="regular-checkbox" /><label for="checkbox-1-4"></label> </div> <div> <div class="tag">Checkbox Big</div> <input type="checkbox" id="checkbox-2-1" class="regular-checkbox big-checkbox" /><label for="checkbox-2-1"></label> <input type="checkbox" id="checkbox-2-2" class="regular-checkbox big-checkbox" /><label for="checkbox-2-2"></label> <input type="checkbox" id="checkbox-2-3" class="regular-checkbox big-checkbox" /><label for="checkbox-2-3"></label> <input type="checkbox" id="checkbox-2-4" class="regular-checkbox big-checkbox" /><label for="checkbox-2-4"></label> </div> <div> <div class="tag">Radio Small</div> <div class="button-holder"> <input type="radio" id="radio-1-1" name="radio-1-set" class="regular-radio" checked /><label for="radio-1-1"></label><br /> <input type="radio" id="radio-1-2" name="radio-1-set" class="regular-radio" /><label for="radio-1-2"></label><br /> <input type="radio" id="radio-1-3" name="radio-1-set" class="regular-radio" /><label for="radio-1-3"></label><br /> <input type="radio" id="radio-1-4" name="radio-1-set" class="regular-radio" /><label for="radio-1-4"></label><br /> </div> </div> <div> <div class="tag">Radio Big</div> <div class="button-holder"> <input type="radio" id="radio-2-1" name="radio-2-set" class="regular-radio big-radio" /><label for="radio-2-1"></label><br /> <input type="radio" id="radio-2-2" name="radio-2-set" class="regular-radio big-radio" /><label for="radio-2-2"></label><br /> <input type="radio" id="radio-2-3" name="radio-2-set" class="regular-radio big-radio" checked /><label for="radio-2-3"></label><br /> <input type="radio" id="radio-2-4" name="radio-2-set" class="regular-radio big-radio" /><label for="radio-2-4"></label><br /> <input type="radio" id="radio-2-5" name="radio-2-set" class="regular-radio big-radio" /><label for="radio-2-5"></label><br /> </div> </div> </div> <div 0; font:normal 14px/24px 'MicroSoft YaHei';">  </div> </body> </html>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值