CSS3学习笔记-03-表单伪类验证

6/3/2020 HTMLCSS3

# 文章目录

# 一、input:disabled 禁用的

<style>
  input:disabled {
    background-color: teal;
  }
</style>
<body>
  <input type="text" disabled />
</body>
1
2
3
4
5
6
7
8

效果:
在这里插入图片描述

# 二、input:enabled 启用的

<style>
  input:optional {
    background-color: green;
  }
</style>
<body>
  <input type="text" value="启用的" />
</body>
1
2
3
4
5
6
7
8

效果:
在这里插入图片描述

# 三、input:checked+label 选中的

<style>
  input:checked + label {
    color: green;
  }
</style>
<body>
  <input type="radio" name="sex" id="boy" checked value="" />
  <label for="boy"></label>
  <input type="radio" name="sex" id="girl" value="" />
  <label for="girl"></label>
</body>
1
2
3
4
5
6
7
8
9
10
11

效果:
在这里插入图片描述

# 四、input:required 必填项

<style>
  input:required {
    background-color: red;
  }
</style>
<body>
  <input type="text" required />
</body>
1
2
3
4
5
6
7
8

效果:
在这里插入图片描述

# 五、input:optional 非必填项

<style>
  input:optional {
    background-color: pink;
  }
</style>
<body>
  <input type="text" />
</body>
1
2
3
4
5
6
7
8

效果:
在这里插入图片描述

# 六、input:valid 输入值有效的样式

<style>
  input:valid {
    background-color: seagreen;
  }
</style>
<body>
  <input type="text" required />
</body>
1
2
3
4
5
6
7
8

效果:
在这里插入图片描述

# 七、input:invalid 输入值无效的样式

<style>
  input:valid {
    background-color: seagreen;
  }
  input:invalid {
    background-color: rgb(139, 39, 39);
  }
</style>
<body>
  <input type="text" required />
</body>
1
2
3
4
5
6
7
8
9
10
11

效果:
在这里插入图片描述

# 八、总览

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>表单属性的操作</title>

    <style>
      input {
        /* 去掉外边线 */
        /* outline: none; */
      }
      /* 禁用的样式 */
      input:disabled {
        border: 1px solid #ddd !important;
      }
      /* 启用的 */
      input:enabled {
        background-color: teal;
      }
      /* 选中的样式的兄弟标签 */
      input:checked + label {
        color: teal;
      }

      /* 必填项 */
      input:required {
        border: 1px solid red;
        background-color: teal;
      }

      /* 非必填项 */
      input:optional {
        border: 1px solid green;
      }
      /* 输入的值有效的样式设置 */
      input:valid {
        background-color: yellowgreen;
      }
      /* 输入的值无效的样式 */
      input:invalid {
        background-color: rebeccapurple;
      }
    </style>
  </head>
  <body>
    <form action="">
      <input type="text" disabled />
      <input type="text" />
      <hr />
      <input type="radio" name="sex" id="boy" checked value="" />
      <label for="boy"></label>
      <input type="radio" name="sex" id="girl" value="" />
      <label for="girl"></label>
      <hr />
      <input type="text" required />
      <hr />
      邮箱
      <input type="email" />
      <button>保存</button>
    </form>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
最后更新于: 2021年9月15日星期三晚上10点43分
Dawn
DDRKirby(ISQ)