Removing input background colour for Chrome autocomplete?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Many web developers have noticed that when using Google Chrome, form inputs can exhibit an unwanted visual effect during autocomplete. Chrome often displays an input's background in a light yellow color (#ffffe0) once autocomplete has filled in the value. This default style may not align with the desired aesthetic of a webpage. However, the visual styling can be altered through specific CSS overrides.
Understanding the Chrome Autocomplete Background Color
Chrome's autocomplete functionality aims to improve user experience by automatically filling in commonly entered information in web forms, such as names, addresses, or credit card numbers. While helpful, the visual feedback of this feature - changing the background color of input fields - might not be desired by all web developers or fit with branded websites.
How to Override Chrome's Autocomplete Styles
To ensure that the style of the form inputs remains consistent with your site design, you can use CSS to override the default styles applied by Chrome. Here is a step-by-step guide on how to do this.
CSS Selector for Autocomplete
Chrome uses the :-webkit-autofill pseudo-class, which selects input fields that are filled automatically by the browser. To tweak the color of these fields, we target this pseudo-class in the CSS.
Example of CSS Override
Below is an example of how you might write your CSS to change the background color of autocomplete fields:
This CSS snippet does a few things:
- It sets a very slow transition for the background color to effectively keep the background color as you set it (
whitein this case). - It changes the text color to white (
#fff). - It uses the
-webkit-box-shadowproperty to 'cover' the original background with the desired color (whitein the example).
Table Summary of Key CSS Properties for Chrome Autocomplete
| Property | Purpose | Example Value | Explanation |
| transition | eases the color change duration | background-color 5000s ease-in-out 0s | Sets a long transition to retain styled bg color |
| -webkit-text-fill-color | color of the text in autofill | #fff | Ensures text color is visible |
| -webkit-box-shadow | inset shadow to override bg color | 0 0 0 30px white inset | Masks the yellow bg with another color |
Additional Considerations
While the solution provided will work for most designs and is widely used as a hack to override Chrome's default autofill styles, it’s important to test thoroughly across multiple browsers and devices. Other browsers might not recognize -webkit- prefixed properties and might require additional CSS or a different approach.
Conclusion
Customizing the look of Chrome's autocomplete background color involves understanding how to select and style these elements effectively using CSS. By applying a few specific styles, you can maintain the consistency of your form's design even when autocomplete is active, enhancing both aesthetics and user experience.
References and Further Reading
For those interested in a deeper understanding or looking for more complex implementations, you may consider reading up on CSS pseudo-classes and the full range of properties supported by web browsers to handle autofill. Browser's official documentation and web standards by W3C provide authoritative information on supported CSS features and browser compatibility.
Related reading
- Rendering Plaintext as HTML maintaining whitespace – without pre
- Repeat a string in JavaScript a number of times
- Repeatedly prompt user until resolved using nodeJS async-await
- Replace an image with Ajax on seaside
- Replace carets with HTML superscript markup using Java
- Retrying a failed async/promise function?
- Return multiple values in JavaScript?
- Return value from asynchronous OR synchronous JavaScript request
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.