Home  >  Article  >  Web Front-end  >  Use CSS to modify HTML default radio and checkbox styles (code example)

Use CSS to modify HTML default radio and checkbox styles (code example)

云罗郡主
云罗郡主forward
2018-10-20 14:23:355398browse

本篇文章给大家带来的内容是关于使用CSS修改HTML默认单选和复选框样式(代码实例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

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

我做的是一个记住密码的选择框

首先上 HTML 代码

<label>
    <input>
    <span></span>
    记住密码
</label>

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;
}

Use CSS to modify HTML default radio and checkbox styles (code example)最后一步:设置复选框选中状态,使用伪类 :aftercontent 属性添加特殊符号对号表示选中

.remmber[type=checkbox]:checked+span:after {
	content: '\2714';
	position: absolute;
	font-size: 0.28rem;
	left: 0.05rem;
}

Use CSS to modify HTML default radio and checkbox styles (code example)

上述 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 中的checkout 改成 radio 就可以了。

以上就是对使用CSS修 HTML默认单选和复选框样式(代码实例)的全部介绍,如果您想了解更多有关CSS视频教程,请关注PHP中文网。

The above is the detailed content of Use CSS to modify HTML default radio and checkbox styles (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete