Kros的博客 Kros的博客
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云

Kros

凡心所向,素履以往,生如逆旅,一苇以航
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云
  • 修改浏览器自动填充的背景颜色

  • 前端
  • CSS
kros
2024-06-08

修改浏览器自动填充的背景颜色

在我们的登录时为了方便通常会记住密码,同时浏览器也会通过自动填充将数据写入到输入框中。这里就会出现一个问题,自动填充后的输入框会多出背景颜色,如下所示:

bj

如果想要修改该背景或者添加自定义样式,可以使用:autofill伪类来设置自动填充样式。MDN-autofill 将背景设置成transparent时需要借助transition来延迟设置背景,避免被浏览器默认样式覆盖。

input:autofill {
  background-color: transparent !important;  
  transition: background-color 50000s ease-in-out 0s;
}
input:-webkit-autofill {
  background-color: transparent !important;  
  transition: background-color 50000s ease-in-out 0s;
}
1
2
3
4
5
6
7
8

bj2

如果想要设置自定义颜色时,使用background-color是不生效的

input:autofill {
  /* 不生效 */
  background-color: red !important;  
  transition: background-color 50000s ease-in-out 0s;
}
1
2
3
4
5

这里我们需要借助box-shadow来实现背景颜色,代码如下:

input:autofill {
  box-shadow: 0 0 0px 1000px red  inset !important; 
  -webkit-box-shadow: 0 0 0px 1000px red  inset !important; 
}
1
2
3
4

bj2

同样直接使用color也是无法设置字体颜色,这里同样我们需要借助特殊属性-webkit-text-fill-color来设置颜色

input:autofill {
  -webkit-text-fill-color: blue !important;
}
1
2
3

bj3

上次更新: 2025/09/05, 8:09:00
最近更新
01
Find the next perfect square
09-05
02
Regex validate PIN code
09-05
03
Find the odd int
09-05
更多文章>
Theme by Vdoing | Copyright © 2020-2025 kros king
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
icon-heart-o icon-heart icon-infinity icon-pause icon-play link next prev