我正在处理的一个表单中,Chrome自动填充了电子邮件和密码字段。这没问题,但是Chrome将背景颜色更改为淡黄色,这在我的设计中使用浅色文本和深色背景的表单中会显得非常不协调——我有鲜艳的黄色方框和几乎看不见的白色文本。一旦字段获得焦点,则字段恢复正常。
有没有可能阻止Chrome更改这些字段的颜色?
我正在处理的一个表单中,Chrome自动填充了电子邮件和密码字段。这没问题,但是Chrome将背景颜色更改为淡黄色,这在我的设计中使用浅色文本和深色背景的表单中会显得非常不协调——我有鲜艳的黄色方框和几乎看不见的白色文本。一旦字段获得焦点,则字段恢复正常。
有没有可能阻止Chrome更改这些字段的颜色?
/* Change the white to any color */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active{
-webkit-box-shadow: 0 0 0 30px white inset !important;
}
/*Change text in autofill textbox*/
input:-webkit-autofill{
-webkit-text-fill-color: yellow !important;
}
建议:不要使用数百或数千的过度模糊半径。这没有任何好处,可能会给较弱的移动设备增加处理器负载。(对于实际的外部阴影也是如此)。对于一个正常的高度为20px的输入框,30px的“模糊半径”将完全覆盖它。
2023年编辑:
为了同时具有透明度和能够更改背景颜色:
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active{
-webkit-background-clip: text;
-webkit-text-fill-color: #ffffff;
transition: background-color 5000s ease-in-out 0s;
box-shadow: inset 0 0 20px 20px #23232329;
}
我有一个更好的解决方案。
像下面这样将背景设置为另一种颜色并不能解决我的问题,因为我需要一个透明的输入框。
-webkit-box-shadow: 0 0 0px 1000px white inset;
所以我尝试了一些其他的方法,最后想到了这个:
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
transition: background-color 5000s ease-in-out 0s;
}
-webkit-text-fill-color: yellow !important;
来设置文本颜色。 - gfpachecotransition-duration
设置得非常高,倒不如设置 transition-delay
。这样做可以更进一步减少颜色变化的可能性。 - Shaggybackground-color
为什么不起作用?Chrome需要修复这个问题!! - TetraDevinput:-webkit-autofill { transition: all 0s 50000s; }
来延迟所有样式更改。 - RienNeVaPlu͢s以前添加盒阴影的解决方案适用于需要纯色背景的人。另一种添加过渡效果的解决方案虽然可行,但必须设置持续时间/延迟,在某些情况下可能会再次显示。
我的解决方案是改用关键帧,这样它将始终显示您选择的颜色。
@-webkit-keyframes autofill {
0%,100% {
color: #666;
background: transparent;
}
}
input:-webkit-autofill {
-webkit-animation-delay: 1s; /* Safari support - any positive time runs instantly */
-webkit-animation-name: autofill;
-webkit-animation-fill-mode: both;
}
示例 Codepen:https://codepen.io/-Steve-/pen/dwgxPB
color: inherit
设置为该值,则此方法适用。 - Marcinput:-webkit-autofill {
-webkit-background-clip: text;
}
body {
background: lightblue;
}
input {
background: transparent;
}
input.no-autofill-bkg:-webkit-autofill {
-webkit-background-clip: text;
}
<input type="text" name="email" />
<input type="text" name="email" class="no-autofill-bkg" />
!important
是必要的。 - Nadav最新更新后的工作内容:
input:-webkit-autofill,
input:-webkit-autofill:focus {
transition: background-color 600000s 0s, color 600000s 0s;
}
input[data-autocompleted] {
background-color: transparent !important;
}
这是我的解决方案,我使用了过渡和过渡延迟,因此可以在我的输入字段上具有透明的背景。
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
-webkit-transition: "color 9999s ease-out, background-color 9999s ease-out";
-webkit-transition-delay: 9999s;
}
box-shadow
一起很好地用于验证。 - Yoann"color 9999s ease-out, background-color 9999s ease-out"
不是有效的表达式。我使用了以下代码:-webkit-transition: background-color 9999s ease-out;
和-webkit-text-fill-color: #fff !important;
。 - naanacebox-shadow
验证是如何工作的?在我的情况下,由于这个CSS规则,我的自定义红色阴影(表示输入错误)会延迟显示,使用户认为最近的输入无效,而实际上它是正确的。 - Paul Razvan Berg这是设计时的预期行为,因为这种着色行为来自于WebKit。它使用户能够了解数据已经被预先填充。Bug 1334
您可以通过执行以下操作来关闭自动完成(或在特定表单控件上进行设置:
<form autocomplete="off">
...
</form
或者您可以通过以下方式更改自动填充的颜色:
input:-webkit-autofill {
color: #2a2a2a !important;
}
请注意,有一个正在被跟踪的错误以便使其再次工作: http://code.google.com/p/chromium/issues/detail?id=46543
这是一个 WebKit 的行为。
!important
并以 ID 为目标提高特异性也是如此。看起来 Chrome 总是会覆盖它。删除自动完成似乎可以解决问题,但这真的不是我想要做的。 - DisgruntledGoatautocomplete="off"
肯定会引起无障碍问题。不过,为什么这个答案还被标记为正确呢? - João目前的一个可能解决方法是在内部设置“strong”阴影:
input:-webkit-autofill {
-webkit-box-shadow:0 0 0 50px white inset; /* Change the color to your own background color */
-webkit-text-fill-color: #333;
}
input:-webkit-autofill:focus {
-webkit-box-shadow: /*your box-shadow*/,0 0 0 50px white inset;
-webkit-text-fill-color: #333;
}
尝试使用此方法隐藏自动填充样式
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:active,
input:-webkit-autofill:focus {
background-color: #FFFFFF !important;
color: #555 !important;
-webkit-box-shadow: 0 0 0 1000px white inset !important;
-webkit-text-fill-color: #555555 !important;
}
搜索了2个小时,似乎谷歌仍然会以某种方式覆盖黄色,但我找到了解决方法。没错,这也适用于悬停,聚焦等操作。你所需要做的就是在样式属性值后添加!important。
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
-webkit-box-shadow: 0 0 0px 1000px white inset !important;
}
这将完全从输入字段中移除黄色
:-internal-autofill-previewed
和:-internal-autofill-selected
伪类,而不是-webkit-autofill
。然而,神奇的是,-webkit-autofill
仍然有效。我个人没有需要使用!important
。 - Andy!important
。 - Patronaut-webkit-box-shadow: none
。 - beano:active
是基本解决方案。非常感谢。 - Sandro Santoscaret-color: yellow
。 - Cyman