Chrome
Autocomplete
Input Background Color
Web Development
CSS

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.

Browse interview questions

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:

css
1input:-webkit-autofill,
2input:-webkit-autofill:hover, 
3input:-webkit-autofill:focus, 
4input:-webkit-autofill:active  {
5    transition: background-color 5000s ease-in-out 0s;
6    -webkit-text-fill-color: #fff !important;
7    -webkit-box-shadow: 0 0 0 30px white inset !important;
8}

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 (white in this case).
  • It changes the text color to white (#fff).
  • It uses the -webkit-box-shadow property to 'cover' the original background with the desired color (white in the example).

Table Summary of Key CSS Properties for Chrome Autocomplete

PropertyPurposeExample ValueExplanation
transitioneases the color change durationbackground-color 5000s ease-in-out 0sSets a long transition to retain styled bg color
-webkit-text-fill-colorcolor of the text in autofill#fffEnsures text color is visible
-webkit-box-shadowinset shadow to override bg color0 0 0 30px white insetMasks 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.