# input框padding
- 浏览器下input框加padding后,无法将其长度转换到0,需要套一个div实现
<body>
<input type="text" id="ipt">
<button id="btn">缩</button>
<style>
#ipt {
width: 200px;
padding: 20px;
background-color: rgb(247, 84, 84);
box-sizing: border-box;
transition: all .3s 0s linear;
}
#btn {
display: block;
}
#ipt.small {
width: 0;
}
</style>
<script>
btn.addEventListener("click", () => ipt.classList.toggle("small"))
</script>
</body>
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<body>
<div id="wrapper">
<input type="text" id="ipt">
</div>
<button id="btn">缩</button>
<style>
#ipt {
padding: 20px;
background-color: rgb(247, 84, 84);
box-sizing: border-box;
}
#btn {
display: block;
}
#wrapper {
width: 200px;
transition: all .3s 0s linear;
overflow: hidden;
}
#wrapper.small {
width: 0;
}
</style>
<script>
btn.addEventListener("click", () => wrapper.classList.toggle("small"))
</script>
</body>
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
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
# 状态选中
- 借用
placeholder
消失的特性检测是否输入了值
<body>
<input type="text" id="ipt" placeholder=" ">
<style>
#ipt:not(:placeholder-shown) {
background-color: rgb(226, 73, 73);
}
</style>
</body>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9