简体   繁体   中英

How to change Safari autofill yellow background color

This image is what I get from safari:

这就是我从Safari中获得的东西

While this is my code:

这是我的样式表

Sources:

How to Remove WebKit's Banana-Yellow Autofill Background

Remove forced yellow input background in Chrome - even it says chrome but still pointing to webkit-autofill

I had tried to use background-color:white !important; to override the locked css. Debug tool showed User Agent Stylesheet background-color had crossed out, but still the color didn't change and the custom was in used.

That's what really confused me, I have no idea why aren't allow to change the User Agent Stylesheet.

Was reading that most browsers want to indicate autofill (with Safari being a bit more adamant with this indication).

I did find an alternative hack , where you use the -webkit-box-shadow to cover the background color. Upon inspection, trying to target the specific input with a class doesn't work (ie specificity).

In Safari, (10.0.2), the browser adds shadow content as the user agent, which includes the background-color and the value of the autofill. The issue here is that, the browser prohibits the the editing of those specific styles (such as background-color ), therefore the reason for the answer below.

input:-webkit-autofill, input:-webkit-autofill:focus {
  -webkit-box-shadow: 0 0 0 1000px white inset;
  -webkit-text-fill-color: #333;
}

if you also have a border-bottom in the input

input:-webkit-autofill, input:-webkit-autofill:focus {
  border-bottom: 1px solid #333;
}

Source:
- medium-post
- stackoverflow discussion
- why-browsers ignore autocomplete="off"

What I found out is that those solutions including -webkit-box-shadow and background-color work only in Google Chrome browser, but not in Safari. What works for me is adding background-clip: content-box . In the end, the code (working in all browsers) looks like:

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active  {
    -webkit-box-shadow: 0 0 0 60px #fafafa inset !important;
    background-color: #fafafa !important;
    background-clip: content-box !important;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM