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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.container {
display: block;
box-sizing: border-box;
width: 300px;
height: 50px;
margin: 200px auto;
padding: 10px;
position: relative;
}

.container input {
border: none;
position: absolute;
width: calc(100% - 20px);
height: 26px;
}

.container .bar {
display: block;
width: calc(100% - 20px);
height: 2px;
background-color: #ddd;
position: absolute;
bottom: 5px;
left: 10px;
}

.container .bar::after {
content: '';
display: block;
width: 0;
height: 100%;
background-color: #aaa;
position: absolute;
left: 50%;
transform: translate(-50%, 0);
transition: 0.4s ease;
}

.container label {
position: absolute;
font-size: 20px;
color: #aaa;
transition: 0.4s ease;
cursor: text;
}

/* 带有required的input 填入内容后就能匹配到:valid伪类 */
.container input:focus,
.container input:valid {
outline: none;
font-size: 24px;
color: #666;
}

.container input:focus ~.bar::after,
.container input:valid ~.bar::after {
width: 100%;
}

.container input:focus ~label,
.container input:valid ~label {
color: #ccc;
transform: translateY(-24px);
font-size: 16px;
cursor: context-menu;
}

</style>

<body>
<div class="container">
<!-- 输入框 -->
<!-- required 必填 -->
<input id="username" required type="text">

<!-- 底部边框条 -->
<span class="bar"></span>

<!-- 提示字样 -->
<label for="username">User Name</label>
</div>
</body>

</html>