ChatGPT解决这个技术问题 Extra ChatGPT

How do I stop Chrome from yellowing my site's input boxes?

Among other text and visual aids on a form submission, post-validation, I'm coloring my input boxes red to signify the interactive area needing attention.

On Chrome (and for Google Toolbar users) the auto-fill feature re-colors my input forms yellow. Here's the complex issue: I want auto-complete allowed on my forms, as it speeds users logging in. I am going to check into the ability to turn the autocomplete attribute to off if/when there's an error triggered, but it is a complex bit of coding to programmatically turn off the auto-complete for the single affected input on a page. This, to put it simply, would be a major headache.

So to try to avoid that issue, is there any simpler method of stopping Chrome from re-coloring the input boxes?

[edit] I tried the !important suggestion below and it had no effect. I have not yet checked Google Toolbar to see if the !important attribute would work for that.

As far as I can tell, there isn't any means other than using the autocomplete attribute (which does appear to work).

this is not just an issue in Chrome. Google Toolbar for other browsers does the same "yellowing" of the input fields.
I've provided an answer for you which works below.
I don't understand why everyone is talking about outline:none, the question is about the yello background, NOT the outline. And the autocomplete attribute set to off will NOT fix the issue, as davebug said he wants the autocomplete to work, just not the yellow background.

r
random

Set the CSS outline property to none.

input[type="text"], input[type="password"], textarea, select { 
    outline: none;
}

In cases where the browser may add a background color as well this can be fixed by something like

:focus { background-color: #fff; }

Background? I wasn't aware that Chrome added a background color as well? If so, that can be fixed by something like :focus { background-color: #fff; }
Killing the outline with CSS might prevent the yellowing but it won't prevent actual autofilling. If you want to do that, it's important to use the autocomplete attribute, nonstandard or not.
@pestaa correct, this is not the right answer, but it certainly solves a similar problem
Take care when dealing with tablets and stuff, the default Android browser is a little difficult to use without the outline. Easy enough to apply to non-mobile browsers though :)
A small improvement: input[type=text], input[type=password], input[type=email], textarea, select { outline: none; }
B
Ben Hoffstein

I know in Firefox you can use the attribute autocomplete="off" to disable the autocomplete functionality. If this works in Chrome (haven't tested), you could set this attribute when an error is encountered.

This can be used for both a single element

<input type="text" name="name" autocomplete="off">

...as well as for an entire form

<form autocomplete="off" ...>

"I am going to check into the ability to turn the autocomplete attribute to off if/when there's an error triggered, but it is a complex bit of coding to programmatically turn off the auto-complete for the single effected input on a page."
Heh...yes, thank you, though I suppose I do still need to check in Chrome, right?
for people using rails simple form <%= simple_form_for @user, html: { autocomplete: 'off' } do |f| %>
sorry to say that this didn't help me
J
JStormThaKid

this is exactly what your looking for!

// Just change "red" to any color
input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px red inset;
}

this is a great hack. Thanks
I would add focus to that too :D input:-webkit-autofill, input:focus:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #FFF inset; }
hi @JStormThaKid, i used this...,but didn't work for correct inputs... please answer this...
Thanks for answering the question he actually asked! I had almost started to lose hope.
Best answer here, I still wanted google autocomplete, so thanks
B
Benjamin

By using a bit of jQuery you can remove Chrome's styling while keeping the autocomplete functionality intact. I wrote a short post about it here: 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);
    });
});}

Thanks for the hint! However after posting the form, clicking the back button, the yellow highlight came back. I've extended your snippet (see my answer) to cover this.
+1 @Benjamin nice solution, it flashs little bit the yellow but it fixes at the end ;)
L
LPL

To remove the border for all fields you can use the following:

*:focus { outline:none; }

To remove the border for select fields just apply this class to the input fields you want:

.nohighlight:focus { outline:none; }

You can of course change the border as you desire as well:

.changeborder:focus { outline:Blue Solid 4px; }

(From Bill Beckelman: Override Chrome's Automatic Border Around Active Fields Using CSS)


M
Mostlyharmless

Yes, it would be a major headache, which in my opinion isnt worth the return. Maybe you could tweak your UI strategy a bit, and instead of coloring the box red, you could color the borders red, or put a small red tape beside it (like the gmails "Loading" tape) which fades away when the box is in focus.


h
hohner

It's a piece of cake with jQuery:

if ($.browser.webkit) {
    $("input").attr('autocomplete','off');
}

Or if you want to be a bit more selective, add a class name for a selector.


N
Nathan

The simpler way in my opinion is:

Get http://www.quirksmode.org/js/detect.html Use this code: if (BrowserDetect.browser == "Chrome") { jQuery('form').attr('autocomplete','off'); };


3
321X

After applying @Benjamin his solution I found out that pressing the back button would still give me the yellow highlight.

My solution somehow to prevent this yellow highlight to come back is by applying the following jQuery javascript:

<script type="text/javascript">
    $(function() {
        if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
        var intervalId = 0;
            $(window).load(function() {
                intervalId = setInterval(function () { // << somehow  this does the trick!
                    if ($('input:-webkit-autofill').length > 0) {
                        clearInterval(intervalId);
                        $('input:-webkit-autofill').each(function () {
                            var text = $(this).val();
                            var name = $(this).attr('name');
                            $(this).after(this.outerHTML).remove();
                            $('input[name=' + name + ']').val(text);
                        });
                    }
                }, 1);
            });
        }
    });
</script>

Hope it helps anyone!!


V
Vin

This works. Best of all, you can use rgba values (the box-shadow inset hack doesn't work with rgba). This is a slight tweak of @Benjamin's answer. I am using $(document).ready() instead of $(window).load(). It seems to work better for me - now there's much less FOUC. I don't believe there are and disadvantages to using $(document).ready().

if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
    $(document).ready(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);
        });
    });
};

h
hsobhy

for today's versions, This works too if placed in one of the two login inputs. Also fix the version 40+ and late Firefox issue.

<input readonly="readonly" onfocus="this.removeAttribute('readonly');" />

i
ib.
input:focus { outline:none; }

That worked great for me but more than likely to keep things uniform on your site your going to want to also include this in your CSS for textareas:

textarea:focus { outline:none; }

Also it may seem obvious to most but for beginners you can also set it to a color as such:

input:focus { outline:#HEXCOD SOLID 2px ; }

Q
Quentin

If I remember correctly, an !important rule in the stylesheet for the background color of the inputs will override the Google toolbar autocomplete - presumably the same would be true of Chrome.