在 WebKit 浏览器中更改自动填充样式

Avatar of Geoff Graham
Geoff Graham

我们从 Lydia Dugger 通过电子邮件获得了一个很好的技巧,介绍了更改 WebKit 浏览器对已自动填充的表单字段应用的样式的方法。

我们所说的自动填充是什么

许多浏览器(包括 Chrome 和 Safari)提供了一种设置,允许用户自动填写常见表单字段的详细信息。当您填写要求填写姓名、地址和电子邮件等信息的表单时,您已经看到了这一点。

问题是,用户必须启用此设置,才能使此代码片段生效。如果启用此设置,那么 WebKit 浏览器将为用户自动填充的字段设置样式,如下所示

请注意自动填充的字段是否有黄色背景?这就是我们要谈论的,并且我们将使用此代码片段进行更改。

代码片段

我们可以使用 -webkit-autofill 伪选择器来定位这些字段并根据需要设置其样式。默认样式仅影响背景颜色,但大多数其他属性在此处适用,例如 borderfont-size。我们甚至可以使用 -webkit-text-fill-color 更改文本颜色,该颜色包含在下面的代码片段中。

/* Change Autocomplete styles in Chrome*/
input:-webkit-autofill,
input:-webkit-autofill:hover, 
input:-webkit-autofill:focus,
textarea:-webkit-autofill,
textarea:-webkit-autofill:hover,
textarea:-webkit-autofill:focus,
select:-webkit-autofill,
select:-webkit-autofill:hover,
select:-webkit-autofill:focus {
  border: 1px solid green;
  -webkit-text-fill-color: green;
  -webkit-box-shadow: 0 0 0px 1000px #000 inset;
  transition: background-color 5000s ease-in-out 0s;
}

演示

查看 CodePen 上 CSS-Tricks (@css-tricks) 的笔 在 WebKit 中更改自动填充样式