ChatGPT解决这个技术问题 Extra ChatGPT

删除 Chrome 自动完成的输入背景颜色?

在我正在处理的表单上,Chrome 会自动填写电子邮件和密码字段。这很好,但是,Chrome 将背景颜色更改为淡黄色。

我正在做的设计是在深色背景上使用浅色文本,所以这真的弄乱了表单的外观——我有鲜明的黄色框和几乎看不见的白色文本。一旦字段被聚焦,字段就会恢复正常。

是否可以阻止 Chrome 更改这些字段的颜色?

甚至不得不问这个事实表明谷歌与世界脱节。

S
Saurin Dashadia

您可以更改输入框样式以及 input 框中的文本样式:

在这里您可以使用任何颜色,例如 white#DDDrgba(102, 163, 177, 0.45)

但是 transparent 在这里不起作用。

/* 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 的“模糊半径”将完美地覆盖它。


现在在 Chrome 中,用户代理样式表显示 :-internal-autofill-previewed:-internal-autofill-selected 伪类而不是 -webkit-autofill...然而,神秘的是,-webkit-autofill 仍然有效。我个人不需要!important
我不需要悬停/焦点/活动伪选择器
这会使焦点的高光变暗,使其成为实线和细线。有解决方案吗?
我认为浏览器能够支持背景颜色自定义,因为自动完成颜色真的弄乱了设计的外观,例如材料设计输入
如果使用半透明背景并希望将其完全移除,请使用 -webkit-box-shadow: none
N
Nathan White

我有更好的解决方案。

将背景设置为如下所示的另一种颜色并不能解决我的问题,因为我需要一个透明的输入字段

-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;
}

太棒了,我还需要一个透明的背景。 PS:不要忘记文本颜色的 -webkit-text-fill-color: yellow !important;
当使用 display 隐藏/显示输入时,这仍会显示自动填充背景。 Fiddle - Check it out
与其将 transition-duration 设置得高得离谱,不如设置 transition-delay。这样,您可以进一步降低颜色发生任何变化的可能性。
很好的解决方案....但是为什么这行得通?为什么设置 background-color 不起作用? Chrome 需要解决这个问题!!
使用:input:-webkit-autofill { transition: all 0s 50000s; } 延迟所有样式更改
S
Saurin Dashadia

以前添加框阴影的解决方案非常适合需要纯色背景的人。添加过渡的另一种解决方案有效,但必须设置持续时间/延迟将意味着在某些时候它可能会再次显示。

我的解决方案是改用关键帧,这样它就会始终显示您选择的颜色。

@-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


我发现这比接受的答案更好...... box-shadow 方法很聪明并且确实有效,但是当用户选择要自动填充的值时,默认颜色会在输入字段中短暂闪烁,这可能很麻烦如果您的输入有深色背景。此解决方案没有此类问题。干杯!
这适用于 Vuetify 2,特别是如果您设置 color: inherit
这曾经对我有用,但在最新的 chrome(88) 中却不是。任何想法为什么?和更新的解决方案?
T
TylerH

要在不使用时间延迟的情况下获得透明背景(尤其是在现代 Web 应用程序中,人们可以暂时停止使用它并希望界面具有可预测的行为),请使用以下命令:

input:-webkit-autofill { 
    -webkit-background-clip: text;
}

身体{背景:浅蓝色; }输入{背景:透明; } input.no-autofill-bkg:-webkit-autofill { -webkit-background-clip: text; }

正在处理:Chrome 83 / 84.0.4147.89,Edge 84.0.522.44

如果您决定重新发布我的解决方案,我只要求您提供我的姓名或指向此的链接。


不幸的是,这不适用于带有 background-color 集的输入
谢谢。另外,它会影响下面评论中的 Bootstrap 样式吗? @manuel-84 stackoverflow.com/questions/21401766/…
@KABA 是的,当然它会覆盖 webkit 上的 background-clip 属性,它仍然是 css 代码,所以每个人都必须研究对他的代码库的影响
太棒了,-webkit-background-clip:文本; + 边界半径:.25rem = 圆角
要更改颜色,请添加 -webkit-text-fill-color: white !important;
S
Saurin Dashadia

这是我的解决方案,我使用了转换和转换延迟,因此我可以在我的输入字段上有一个透明的背景。

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 配合使用非常好,以进行验证
chrome 说 "color 9999s ease-out, background-color 9999s ease-out"; 不是有效的表达式。然后我使用了代码,-webkit-transition: background-color 9999s ease-out;-webkit-text-fill-color: #fff !important;
@Yoann 它究竟如何与您现有的 box-shadow 验证一起工作?在我的例子中,由于这个 css 规则,我的自定义红色框阴影(表示输入错误)被延迟,使用户认为最近的输入是无效的,而实际上它是好的。
M
Mohamed Mansour

自从这种着色行为来自 WebKit 以来,这就是设计的。它允许用户了解数据已经预填充。 Bug 1334

您可以通过执行(或在特定的表单控件上)关闭自动完成功能:

<form autocomplete="off">
...
</form

或者您可以通过执行以下操作更改自动填充的颜色:

input:-webkit-autofill {
    color: #2a2a2a !important;
}

请注意,正在跟踪一个错误以使其再次起作用:http://code.google.com/p/chromium/issues/detail?id=46543

这是一种 WebKit 行为。


谢谢,但是那个 webkit CSS 规则不起作用。用户代理样式表总是覆盖背景颜色,即使它 (a) 设置为 !important 并且 (b) 以 ID 为目标以获得更高的特异性。看起来 Chrome 总是会覆盖它。删除自动完成似乎确实有效,但这真的不是我想要做的。
有些人有同样的问题,你检查过错误帖子吗? code.google.com/p/chromium/issues/detail?id=1334
Chrome 会阻止任何 CSS 尝试覆盖该黄色。设置 autocomplete="off" 肯定会引发可访问性问题。为什么这个答案仍然被标记为正确?
这是一个很好的解决方案,但如果你使用 React,它会抱怨自动完成是一个未知的 DOM 属性。因此,它实际上是自动完成的(注意驼峰式)。
关闭自动完成是一种技巧而不是解决方案
T
Tamás Pap

目前一个可能的解决方法是在阴影中设置一个“强”:

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;
}  

这是实际工作的解决方案,我们不想只跳过黄色背景几次,因为接受的答案会导致。
H
Hannes Schneidermayer

2022 年更新

2022 年更新后工作:

input:-webkit-autofill,
input:-webkit-autofill:focus {
    transition: background-color 600000s 0s, color 600000s 0s;
}
input[data-autocompleted] {
    background-color: transparent !important;
}

S
Saurin Dashadia

试试这个隐藏自动填充样式

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;
}

对于好奇的,背景颜色没有效果。 -webkit-box-shadow 是在 Chrome 中覆盖黄色背景的巧妙部分
P
Patrick Fisher

SASS

input:-webkit-autofill

  &,
  &:hover,
  &:focus,
  &:active
    transition-delay: 9999s
    transition-property: background-color, color

这实际上对我有用,因为接受的答案呈淡黄色闪烁。
S
Saurin Dashadia

以上所有答案都有效,但确实存在缺陷。下面的代码是上述两个答案的合并,可以完美地工作而不会闪烁。

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
    transition: background-color 5000s ease-in-out 0s;
    -webkit-box-shadow: 0 0 0px 1000px #fff inset;
}

我已将图像设置为输入字段的背景,此解决方案消除了黄色阴影,但背景图像仍然隐藏
这是唯一一个在 Chrome 83 中为我工作的。投票最多的是在 Chrome 82 之前工作。
S
Saurin Dashadia

经过 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;
}

这将从输入字段中完全删除黄色


S
StefansArya

添加一小时延迟将暂停输入元素上的任何 css 更改。这比添加过渡动画或内部阴影更好。

input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill{
  transition-delay: 3600s;
}

刚刚浏览了这个线程并使用转换延迟对所有答案进行了投票,它运行完美,并且很容易理解如果将代码传递给不知道为什么将其放在那里的其他人。
S
Saurin Dashadia

我遇到了无法使用 box-shadow 的问题,因为我需要输入字段是透明的。这有点骇人听闻,但纯 CSS。将过渡设置为很长的时间。

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
    transition: background-color 50000s ease-in-out 0s, color 5000s ease-in-out 0s;
}

截至目前,在最新的 Chrome 50.xx 中,它运行良好。非常感谢!
与其将 transition-duration 设置得高得离谱,不如设置 transition-delay。这样,您可以进一步降低颜色发生任何变化的可能性。
@Shaggy 谢谢,我只是将其切换为延迟。好多了。
S
Saurin Dashadia

试试这个:与上面的 @Nathan-white 答案相同,只是稍作调整。

/* For removing autocomplete highlight color in chrome (note: use this at bottom of your css file). */

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
    transition: all 5000s ease-in-out 0s;
    transition-property: background-color, color;
}

我最喜欢这个,因为这意味着我没有再次专门设置我的背景颜色或文本颜色。 (对我使用各种不同颜色的组件很有用。)我将延迟从 0 秒更改为 5000 秒,以给它额外的 5000 秒作为我的背景颜色和文本颜色。
L
Linghua Jin

除此之外:

input:-webkit-autofill{
-webkit-box-shadow: 0 0 0px 1000px white inset;
}

您可能还想添加

input:-webkit-autofill:focus{
-webkit-box-shadow: 0 0 0px 1000px white inset, 0 0 8px rgba(82, 168, 236, 0.6);
}

否则,当您单击输入时,黄色会回来。对于焦点,如果您使用的是引导程序,则第二部分是边框突出显示 0 0 8px rgba(82, 168, 236, 0.6);

这样它看起来就像任何引导输入。


B
Benjamin

如果你想保持自动完成功能不变,你可以使用一些 jQuery 来删除 Chrome 的样式。我在这里写了一篇关于它的短文:http://www.benjaminmiles.com/2010/11/22/fixing-google-chromes-yellow-autocomplete-styles-with-jquery/

if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
$(window).load(function(){
    $('input:-webkit-autofill').each(function(){
        var text = $(this).val();
        var name = $(this).attr('name');
        $(this).after(this.outerHTML).remove();
        $('input[name=' + name + ']').val(text);
    });
});}

这似乎是最好的解决方案,尽管它会阻止 Kicksend's mailcheck。关于如何解决这个问题的任何想法?
这不是一个好的解决方案,因为谷歌浏览器的密码自动完成功能停止工作。最初是您填写名称,Google chrome 会自动填写密码。然而,一旦上面的 javascript 执行,名称就会被删除并重新设置,并且自动填充的密码会丢失
2
2ne

我有一个使用 CSS 过滤器的纯 CSS 解决方案。

filter: grayscale(100%) brightness(110%);

灰度滤镜用灰色代替黄色,然后亮度去除灰色。

SEE CODEPEN


好的,但是与许多其他解决方案一样,这不会允许我拥有透明的背景
S
Saurin Dashadia

这适用于正常、悬停、焦点和活动状态下的输入、文本区域和选择。

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active,
textarea:-webkit-autofill,
textarea:-webkit-autofill:hover,
textarea:-webkit-autofill:focus,
textarea:-webkit-autofill:active,
select:-webkit-autofill,
select:-webkit-autofill:hover,
select:-webkit-autofill:focus,
select:-webkit-autofill:active {
    -webkit-box-shadow: 0 0 0 1000px white inset !important;
}

这是上述解决方案的 SCSS 版本,适用于使用 SASS/SCSS 的人。

input:-webkit-autofill,
textarea:-webkit-autofill,
select:-webkit-autofill {
    &, &:hover, &:focus, &:active{
        -webkit-box-shadow: 0 0 0px 1000px white inset !important;
    }
}

D
Daniel Fairweather

我已经开发了另一种使用 JavaScript 没有 JQuery 的解决方案。如果您觉得这很有用或决定重新发布我的解决方案,我只要求您提供我的名字。享受。 ——丹尼尔·费尔韦瑟

var documentForms = document.forms;

for(i = 0; i < documentForms.length; i++){
    for(j = 0; j < documentForms[i].elements.length; j++){
        var input = documentForms[i].elements[j];

        if(input.type == "text" || input.type == "password" || input.type == null){
            var text = input.value;
            input.focus();
            var event = document.createEvent('TextEvent');
            event.initTextEvent('textInput', true, true, window, 'a');
            input.dispatchEvent(event);
            input.value = text;
            input.blur();
        }
    }
}

此代码基于这样一个事实,即一旦输入其他文本,Google Chrome 就会删除 Webkit 样式。仅仅更改输入字段值是不够的,Chrome 需要一个事件。通过关注每个输入字段(文本、密码),我们可以发送一个键盘事件(字母 'a'),然后将文本值设置为之前的状态(自动填充的文本)。请记住,此代码将在每个浏览器中运行,并将检查网页中的每个输入字段,并根据您的需要进行相应调整。


好吧,它在大多数情况下都有效。不幸的是,它清除了我的密码。试图让它先保存密码并在 for 循环之后重置它,但 chrome 似乎有一些问题。嗯嗯
j
jedmao

对于那些使用指南针的人:

@each $prefix in -webkit, -moz {
    @include with-prefix($prefix) {
        @each $element in input, textarea, select {
            #{$element}:#{$prefix}-autofill {
                @include single-box-shadow(0, 0, 0, 1000px, $white, inset);
            }
        }
    }
}

L
Laurel

这对我来说可以删除该字段的黄色背景:

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active,
input:-webkit-autofill:valid,
select:-webkit-autofill,
select:-webkit-autofill:hover,
select:-webkit-autofill:focus {
    -webkit-transition-delay: 99999s;
    -webkit-text-fill-color:#D7D8CE;
}

S
Saurin Dashadia

Google Chrome 用户代理阻止了开发人员的 CSS,因此要更改 autofill UI,必须使用其他属性,如下所示:

input:-webkit-autofill,
textarea:-webkit-autofill,
select:-webkit-autofill {
    -webkit-box-shadow: 0 0 0 1000px #d500ff inset !important;
    /*use inset box-shadow to cover background-color*/
    -webkit-text-fill-color: #ffa400 !important;
    /*use text fill color to cover font color*/
}

box-shadow 属性不再需要以 -webkit- 为前缀,将其大小设置为输入高度的一半(而不是 1000px)会更高效 - 当然,在可能的情况下。但除此之外,不幸的是,这是解决问题的最佳方法
S
Saurin Dashadia

可以更改此框的颜色,例如接受的答案。

但是如果你想让背景透明,这个方法就没有用了。但是,有一种方法(或者更确切地说是解决方法)可以使其透明,如下所示:

input:-webkit-autofill,
input:-webkit-autofill:hover, 
input:-webkit-autofill:focus, 
input:-webkit-autofill:active  {
    transition: background-color 5000s;
    -webkit-text-fill-color: #fff !important;
}

这基本上相当于透明背景。


这是唯一适用于现代 Chrome 的
S
Saurin Dashadia

我们可以使用 -webkit-autofill 伪选择器来定位这些字段并设置我们认为合适的样式。默认样式仅影响背景颜色,但大多数其他属性都适用于此处,例如边框和字体大小。我们甚至可以使用下面代码段中包含的 -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;
}

您提到了字体大小:我如何实际更改字体大小?添加 font-size: 24px 不起作用
S
Saurin Dashadia

Daniel Fairweather (Removing input background colour for Chrome autocomplete?) 的解决方案(我很想支持他的解决方案,但仍然需要 15 个代表)效果非常好。最受好评的解决方案有一个非常大的区别:您可以保留背景图片!但稍作修改(只是 Chrome 检查)

你需要记住,它只适用于可见领域!

因此,如果您在表单中使用 $.show(),则需要在 show() 事件之后运行此代码

我的完整解决方案(我有一个登录表单的显示/隐藏按钮):

if (!self.isLoginVisible()) {
    var container = $("#loginpage");
    container.stop();
    self.isLoginVisible(true);
    
    if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
        var documentForms = document.forms;
        
        for (i = 0; i < documentForms.length; i++) {
            for (j = 0; j < documentForms[i].elements.length; j++) {
                var input = documentForms[i].elements[j];

                if (input.type == "text" || input.type == "password" || input.type == null) {
                    var text = input.value;
                    input.focus();
                    var event = document.createEvent('TextEvent');
                    event.initTextEvent('textInput', true, true, window, 'a');
                    input.dispatchEvent(event);
                    input.value = text;
                    input.blur();
                }
            }
        }
    }
} else {
    self.hideLogon();
}

再次抱歉,我希望它是一个评论。

如果你愿意,我可以放一个链接到我使用它的网站。


S
Saurin Dashadia

如果你想阻止谷歌浏览器的自动填充但它有点“弯刀”,我有一个解决方案,只需删除谷歌浏览器添加到这些输入字段的类并将值设置为“”如果你不需要显示加载后存储数据。

$(document).ready(function () {
    setTimeout(function () {
        var data = $("input:-webkit-autofill");

        data.each(function (i, obj) {
            $(obj).removeClass("input:-webkit-autofill");
            obj.value = "";
        });
    }, 1);          
});

e
ed1nh0

我放弃!

由于无法使用自动完成更改输入的颜色,我决定使用 jQuery 为 webkit 浏览器禁用所有这些。像这样:

if (/webkit/.test(navigator.userAgent.toLowerCase())) {
    $('[autocomplete="on"]').each(function() {
        $(this).attr('autocomplete', 'off');
    });
}

i
i_a

没有一个解决方案对我有用,插入阴影对我不起作用,因为输入有一个半透明的背景覆盖在页面背景上。

所以我问自己,“Chrome 如何确定给定页面上应该自动填充的内容?”

“它会查找输入 ID、输入名称吗?表单 ID?表单操作?”

通过我对用户名和密码输入的实验,我发现只有两种方法会导致 Chrome 无法找到应该自动填充的字段:

1)将密码输入放在文本输入之前。 2) 给他们相同的名字和身份证……或者根本没有名字和身份证。

页面加载后,您可以使用 javascript 动态更改页面上输入的顺序,或者动态地为它们提供名称和 id ...

Chrome 不知道是什么击中了它……自动完成功能坏了!

疯狂的黑客,我知道。但它对我有用。

铬 34.0.1847.116,OSX 10.7.5


a
arczi

这对我有用:

padding: 5px;
background-clip: content-box;

background-clip: text; 在 Chrome 中工作