CSS Portal

:autofill CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :autofill pseudo-class matches form controls that the user agent has automatically populated with stored credentials or remembered form data. It’s applied when the browser fills a control (usually on page load or when the user selects a saved entry) so you can style those controls differently from manually-entered values.

What it targets and when it applies

  • It generally applies to form controls such as input elements (and similar text-entry controls) after the browser fills them.
  • The pseudo-class reflects the browser’s autofill state, not whether the value was programmatically set by script; some browsers only set the state when their internal autofill mechanism populates the control.
  • The state typically remains while the autofilled value is present; editing, clearing, or the browser deciding the field is no longer autofilled will remove the pseudo-class.

Styling notes and gotchas

  • The pseudo-class behaves like any other pseudo-class in selectors and affects specificity as expected.
  • Styles are not inherited from parent elements simply because a child is autofilled; you must target the autofilled control.
  • Many browsers apply their own autofill styling (for example a background color). That UA styling can be hard to override with simple background rules; techniques like using an inset box-shadow or using text-fill color are commonly used to override visual UA defaults.
  • Transitions on properties overridden by the browser’s autofill may not animate as expected. A common workaround is to trigger a small animation on the autofilled selector to force repaint or to apply the final styling immediately.
  • Rely on :autofill for visual affordances instead of trying to detect autofill in JavaScript; browsers may restrict programmatic detection for privacy reasons.

Accessibility and behavior considerations

  • Autofill does not change the semantics of the control; ensure labels and ARIA are correct so autofilled values remain accessible.
  • Don’t hide important contrast when styling autofilled fields - autofill can create unexpected color combinations; keep sufficient contrast for readability.
Typical patterns and examples

Simple visual highlight for autofilled controls:

input:autofill,
textarea:autofill {
  background-color: #e6f7ff;
  border-color: #66b3ff;
}

Override UA background by using an inset box-shadow and ensure readable text color:

input:autofill {
  box-shadow: 0 0 0 1000px white inset; /* hides UA background */
  -webkit-text-fill-color: #111;        /* helps override some UAs */
  transition: background-color 200ms ease;
}

Style when an autofilled field also has focus (combine with a focused state):

input:autofill:focus,
input:autofill:hover {
  outline: 2px solid #4aa3ff;
  box-shadow: 0 0 0 1000px #fff inset;
}

Force a repaint/animation workaround if your transitions aren’t applied by the UA:

@keyframes autofill-repaint { from { opacity: .99; } to { opacity: 1; } }
input:autofill {
  animation-name: autofill-repaint;
  animation-duration: 0.01s;
}

Interactions with other styling hooks

  • You can combine :autofill with other selectors (for example :focus) to create compound states such as focused-and-autofilled.
  • Placeholder styling and autofill can interact visually; consider how autofill styling affects or conflicts with ::placeholder color and spacing.

Best practices

  • Test autofill behavior with actual saved credentials/data rather than mock values.
  • Use non-invasive visual cues (border, subtle background) so autofill remains obvious but accessible.
  • Prefer CSS solutions for visual changes; avoid relying on JavaScript detection of autofill because of privacy-limiting behavior in some browsers.

Syntax

:autofill {
  /* ... */
}

Example

<form action="#">
<div class="field">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" autocomplete="email">
</div>

<div class="field">
<label for="city">City</label>
<input type="text" id="city" name="city" autocomplete="address-level2">
</div>

<button type="submit">Submit</button>
</form>
/* Base styling for the input */
label {
display: block;
}

input {
padding: 10px;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
outline: none;
transition: border-color 0.3s;
}

/* Styles applied ONLY when the browser autofills the field */
input:autofill {
border: 2px solid #4CAF50; /* Green border for autofilled fields */
-webkit-text-fill-color: #2e7d32; /* Change the text color */
}

/* Note: To change the background color of an autofilled field,
most browsers require using a box-shadow trick because they
force their own background-color.
*/
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0px 1000px #e8f5e9 inset; /* Pale green background */
}

Browser Support

The following information will show you the current browser support for the CSS :autofill pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!