纯CSS实现自定义单选框和复选框

纯CSS实现自定义单选框和复选框<!DOCTYPEhtml><html> <head> <metacharset=”utf-8″> <title></title> <styletype=”text/css”> #main{ display:flex; justify-content:center; align-items:center; flex-wrap:wrap; } .

大家好,又见面了,我是你们的朋友全栈君。

目录

1 实现效果 

2 知识点讲解

2.1 标签

2.2 CSS3 box-shadow 属性

2.3 CSS3 transition 属性

2.4 CSS3 :checked 选择器

2.5 CSS element+element 选择器

3 代码实现 


1 实现效果 

纯CSS实现自定义单选框和复选框

演示地址: https://www.albertyy.com/2020/7/check1.html 

另一篇文章:https://albertyang.blog.csdn.net/article/details/107349231

2 知识点讲解

2.1 <label>标签

在html中,<label>标签通常和<input>标签一起使用,<label>标签为input元素定义标注(标记)。label 元素不会向用户呈现任何特殊效果,<label>标签的作用是为鼠标用户改进了可用性,当用户点击<label>标签中的内容时,浏览器就会自动将焦点转到和该标签相关联的控件上<label>标签在单选按钮和复选按钮上经常被使用,使用该标签后,你点击label标签内的内容,也可以选中对应的单选按钮或复选按钮。

<label>标签语法格式:

<label for=”关联控件的id” form=”所属表单id列表”>文本内容</label>

关联控件的id一般指的是input元素的id;在html5中还新增了一个属性form,form属性是用来规定所属的一个或多个表单的 id 列表,以空格隔开;当<label>标签不在表单标签<form>中时,就需要使用form属性来指定所属表单;

<label> 元素没有特别的样式考虑——结构上,<label> 是简单的行内元素,所以可使用和 <span> 或 <a>元素大致相同的方式来应用样式。

2.2 CSS3 box-shadow 属性

boxShadow 属性把一个或多个下拉阴影添加到框上。该属性是一个用逗号分隔阴影的列表,每个阴影由 2-4 个长度值、一个可选的颜色值和一个可选的 inset 关键字来规定。省略长度的值是 0。

语法:

box-shadow: h-shadow v-shadow blur spread color inset;

说明

h-shadow

必需的。水平阴影的位置。允许负值

v-shadow

必需的。垂直阴影的位置。允许负值

blur

可选。模糊距离

spread

可选。阴影的大小

color

可选。阴影的颜色。

inset

可选。从外层的阴影(开始时)改变阴影内侧阴影

2.3 CSS3 transition 属性

transition 属性用来设置元素渡效果,四个简写属性为:

transition-property

transition-duration

transition-timing-function

transition-delay

语法

transition: property duration timing-function delay;

描述

transition-property

指定CSS属性的name,transition效果

transition-duration

transition效果需要指定多少秒或毫秒才能完成

transition-timing-function

指定transition效果的转速曲线

transition-delay

定义transition效果开始的时间

2.4 CSS3 :checked 选择器

:checked 选择器匹配每个选中的输入元素(仅适用于单选按钮或复选框)。

2.5 CSS element+element 选择器

element+element 选择器用于选择(不是内部)指定的第一个元素之后紧跟的元素。

例如:选择所有紧接着 <div> 元素之后的第一个 <p> 元素:

div+p{ background-color:yellow; }

3 代码实现 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#main {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-wrap: wrap;
			}

			#wrap {
				position: relative;
				margin: 10px;
			}

			.item {
				width: 100px;
				height: 100px;
				background-color: #9E9E9E;
				position: relative;
				box-shadow: 0 0 0 3px #dbe0e3;
				transition: all 0.5s;
				cursor: pointer;
			}

			.item img {
				width: 20px;
				height: 20px;
				position: absolute;
				bottom: 0px;
				right: 0px;
				opacity: 0;
			}

            input[type="radio"],
			input[type="checkbox"] {
				display: none;
			}

			input:checked+label .item {
				box-shadow: 0 0 0 3px #00a3ff;
				color: #FFFFFF;
				background-color: #efad4c;
			}

			input:checked+label .item img {
				opacity: 1;
			}
			
			.content {
				font-size: 30px;
				text-align: center;
				line-height: 100px;
			}
		</style>
	</head>
	<body>
		<div id="main">
			
			<h1>多选</h1>
			<div id="wrap">
			    <input type="checkbox" name="1" id="item1" />
				<label for="item1">
					<div class="item">
						<div class="content">
							1
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="checkbox" name="1" id="item2" />
				<label for="item2">
					<div class="item">
						<div class="content">
							2
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="checkbox" name="1" id="item3" />
				<label for="item3">
					<div class="item">
						<div class="content">
							3
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="checkbox" name="1" id="item4" />
				<label for="item4">
					<div class="item">
						<div class="content">
							4
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="checkbox" name="1" id="item5" />
				<label for="item5">
					<div class="item">
						<div class="content">
							5
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			
			<h1>单选</h1>
			<div id="wrap">
			    <input type="radio" name="1" id="item6" />
				<label for="item6">
					<div class="item">
						<div class="content">
							1
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="radio" name="1" id="item7" />
				<label for="item7">
					<div class="item">
						<div class="content">
							2
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="radio" name="1" id="item8" />
				<label for="item8">
					<div class="item">
						<div class="content">
							3
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="radio" name="1" id="item9" />
				<label for="item9">
					<div class="item">
						<div class="content">
							4
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
			<div id="wrap">
			
				<input type="radio" name="1" id="item10" />
				<label for="item10">
					<div class="item">
						<div class="content">
							5
						</div>
						<img src="ico_checkon.svg" />
					</div>
				</label>
			</div>
		</div>
	</body>
</html>

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139431.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • HttpClient默认重试策略不处理SocketTimeoutException

    HttpClient默认重试策略不处理SocketTimeoutException

    2022年4月2日
    163
  • plot函数的用法_ezplot函数

    plot函数的用法_ezplot函数matlab的图形绘制是非常重要的一种功能,所有关于数据分析挖掘方面一定会用到此项功能。在我们开始第三章数据可视化之前,必须先把plot函数必须弄得清清楚楚才行,下面让我们看看关于它的一切吧!一.plot首先,plot有几种形式(1)plot(X,Y):创建数据Y相对于中相应值X的二维折线图其中,若X,Y是向量,长度必须相等,图是Y对X的若X,Y是矩阵…

    2022年10月11日
    1
  • 通俗的解释什么是Promise

    通俗的解释什么是Promise**说了这么多其实翻译成大白话就是:**1、媳妇儿饿了需要吃饭,所以我要上街买菜(**异步方法**)2、我什么时候买完菜回来她不知道(**异步方法执行几秒未知**),3、但是买完菜回到家之后我会马上做个红烧排骨给媳妇吃(**异步方法执行结束之后需要对返回值做处理**)这时候怎么办呢,就用promise(承诺):就说这个事情交给我吧,我承诺我去买菜,买完回来马上给你做红烧排骨,做完马上就叫你吃(这个地方相当于**promise链式调用**),你现在该干嘛干嘛去,去刷抖音,打游戏都可以(**不影响其他

    2022年5月20日
    36
  • 在Eclipse中使用JUnit4进行单元測试(0基础篇)

    在Eclipse中使用JUnit4进行单元測试(0基础篇)

    2021年11月30日
    39
  • CentOS 中用 Yum 安装、卸载软件

    CentOS 中用 Yum 安装、卸载软件一:Yum简介Yum(全称为YellowdogUpdater,Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。二:常用的Yum命令1、显示已经安装的软件包yumlist…

    2022年6月11日
    30
  • rpm卸载多个有依赖的rpm包[通俗易懂]

    rpm卸载多个有依赖的rpm包[通俗易懂][root@devOOo_3.1.0_src]#rpm-qlibxml2[root@devOOo_3.1.0_src]#rpm-qalibxml2*[root@dev~]#rpm-qa|greplibxml2libxml2-python-2.6.26-2.1.12libxml2-devel-2.6.26-2.1.12libxml2-2.6.26libxm…

    2022年9月22日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号