概述
针对Form表单的元素有其特殊的伪类选择器,下面列举出几个:
:valid
: 表单元素有效,主要适用于input type为number
, email
等类型;
:invalid
: 与:valid
相反概念;
:required
: input是否设置required属性;
:optional
:与:required
相对应;
:in-range
:针对input type为number的情况,如果输入的值属于有效的值;
:out-of-range
: 与:in-range
相对应;
下面是一个示例:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title>Form Specific Pseudo Class</title>
<style>
input {
display: inline-block;
}
input:required {
background-color: yellow;
}
input:optional {
background-color: rgba(128, 126, 125, 0.3);
}
input[type='email']:valid {
border: 2px green solid;
}
input[type='email']:invalid {
border: 2px red solid;
}
input[type='number']:in-range {
border: 2px green solid;
}
input[type='number']:out-of-range {
border: 2px red solid;
}
input + span {
display:none;
}
input:invalid + span {
display:inline-block;
color:red;
}
</style>
</head>
<body>
<form>
<label>姓名</label>
<input type="text" value="姜健" name="author" readonly/>
<br>
<label>年龄</label>
<input type="number" name="age"/>
<span>年龄不符合要求</span>
<br>
<label>邮箱:</label>
<input type="email" required/>
<span>邮箱不符合要求</span>
</form>
</body>
</html>
效果如下:
1 当邮箱不符合要求时:
2 邮箱输入有效时:
最后
以上就是想人陪发带为你收集整理的【CSS】Form特定的伪类选择器的全部内容,希望文章能够帮你解决【CSS】Form特定的伪类选择器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复