CSS Reference ยท Pseudo-classes
The :autofill pseudo-class
Reference for the CSS :autofill pseudo-class: style inputs filled in by the browser, why the background is hard to override, and the older -webkit- prefix.
The :autofill pseudo-class matches an <input> whose value was filled in by the browser's autofill, for example a saved email address or password. Browsers usually give such fields a tinted background (often pale yellow or blue) to show that the value came from outside the page.
You can use :autofill to keep the field consistent with your design, but browsers deliberately limit what can be changed.
input:autofill { box-shadow: inset 0 0 0 100px #e6fffa; -webkit-text-fill-color: #134e4a; }
Try it
Syntax
:autofill { /* declarations */ } :-webkit-autofill { /* declarations */ }
No arguments; specificity (0,1,0). Do not put both selectors in one list: a browser that does not understand one of them drops the whole rule. Write two separate rules instead.
Syntax and arguments
| Part | Description |
|---|---|
:autofill |
The standard name. Matches inputs filled by the browser. |
:-webkit-autofill |
The older WebKit/Blink name; still needed for older browsers. |
| Matching stops | When the user edits the value, the field usually stops matching. |
Usage notes
- The background is protected. Chromium-based browsers paint the autofill background with a user-agent style that author
background-colorcannot override. Common workarounds are a large insetbox-shadow, as shown, or a very longtransitiononbackground-color. - Text colour. Use
-webkit-text-fill-colorto change the colour of autofilled text in browsers that ignorecolorthere. - Match only after the browser acts. Many browsers apply
:autofillonly after the user chooses the suggestion or interacts with the page, so you cannot rely on it for page-load logic. - Cannot be detected by scripts reliably. Reading the value of an autofilled password field may be blocked until the user interacts with the page.
- Keep contrast. If you change the colours, make sure the text still meets contrast guidelines against the new background.
Browser support
The unprefixed :autofill works in current browsers; older ones need :-webkit-autofill. How much you can change differs by browser.
Related
:placeholder-shownโ empty inputs with a placeholder:user-validโ valid after interaction:focus-visibleโ keyboard focus- Lessons: Styling forms, User interface