:autofill CSS pseudo-class

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The :autofill CSS pseudo-class matches when an <input> element has its value autofilled by the browser. The class stops matching if the user edits the field.

Try it

input {
  border: 3px solid black;
}
input:autofill {
  outline: 5px solid magenta;
  border: 3px dashed yellow;
}
<form>
  <p>Click on the text box and choose any option suggested by your browser.</p>
  <p>
    <label for="name">Name</label>
    <input id="name" name="name" type="text" autocomplete="given-name">
  </p>
  <p>
    <label for="email">Email Address</label>
    <input id="email" name="email" type="email" autocomplete="email">
  </p>
  <p>
    <label for="country">Country</label>
    <input id="country" name="country" type="text" autocomplete="country-name">
  </p>
</form>

Description

The :autofill pseudo-class selects <input> elements containing un-edited content that was auto-filled by the user-agent. Autocompletion may occur when browser settings are set to enable autocompletion or the autocomplete attribute is set on the element itself.

When the user uses the browser's autocompletion feature to autofill a form control, all the form controls that can be autofilled based on the selection get populated. These will all match the :autofill UI state. If the user edits a control, that control will no longer match :autofill, even if the value is the same as the autofilled value.

The specification includes the vendor-prefixed :-webkit-autofill pseudo-class as an alias. This also matches input elements autofilled by the user agent, and no longer matches if the user edits the autofilled field.

Note that the user agent style sheets of many browsers use !important in their style declarations, making some styles non-overridable. For example, Chrome has the following in its internal stylesheet:

css
input:-internal-autofill-selected {
  appearance: menulist-button;
  background-image: none !important;
  background-color:
    light-dark(rgb(232, 240, 254), rgba(70, 90, 126, 0.4)) !important;
  color: fieldtext !important;
}

This means that you cannot override the default background-color, background-image, or color values on selected autofilled input elements in your own rules, but you can override the appearance, and you can override these properties for the autofilled elements that aren't currently focused.

Syntax

css
:autofill {
  /* ... */
}

Examples

The following example demonstrates the use of the :autofill pseudo-class to change the border of a text field that has been autocompleted by the browser.

HTML

We include three HTML <input> elements, each with an associated <label>. The name and email will likely autocomplete and match :autofill, while the pet value is unlikely to.

html
<form method="post" action="">
  <p>
    <label for="name">Name: </label>
    <input name="n" id="name" autocomplete="given-name" />
  </p>
  <p>
    <label for="email">Email: </label>
    <input type="email" name="email" id="email" autocomplete="email" />
  </p>
  <p>
    <label for="pet">Your pet's name: </label>
    <input name="pet" id="pet" />
  </p>
</form>

CSS

We apply a border-radius, outline, and border-color to the <input> elements when they match the :autofill state.

css
input {
  border-radius: 0px;
}

input:autofill {
  border-radius: 0.5lh;
  outline: 5px dashed magenta;
  border-color: yellow;
}

Results

If you allow the name to autocomplete, the name and email will both have a rounded border and a magenta outline, though likely the border will not change due to the !important flag set in the user agent style sheet. Try editing one of the fields: note how once you edit the value, even if you reset it to it's autocompletion value, the :autofill styles are no longer applied.

Specifications

Specification
HTML
# selector-autofill
Selectors Level 4
# selectordef-autofill

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
:autofill
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
prefix
prefix Implemented with the vendor prefix: -webkit-
Chrome – Full support
Chrome 110 (Release date: 2023-02-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
prefix
prefix Implemented with the vendor prefix: -webkit-
Edge – Full support
Edge 110 (Release date: 2023-02-09)
footnote Full support
Firefox – Partial support
Firefox 86 (Release date: 2021-02-23)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: -webkit-
footnote The :-webkit-autofill pseudo-class matches autofilled username and password fields, but not other autocompleted fields. See bug 1923525.
Firefox – Partial support
Firefox 86 (Release date: 2021-02-23)
footnote Partial support
footnote The :autofill pseudo-class matches autofilled username and password fields, but not other autocompleted fields. See bug 1923525.
Opera – Full support
Opera 15 (Release date: 2013-07-02)
prefix
prefix Implemented with the vendor prefix: -webkit-
Opera – Full support
Opera 96 (Release date: 2023-02-22)
footnote Full support
Safari – Full support
Safari 3 (Release date: 2007-10-26)
prefix
prefix Implemented with the vendor prefix: -webkit-
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
prefix
prefix Implemented with the vendor prefix: -webkit-
Chrome Android – Full support
Chrome Android 110 (Release date: 2023-02-07)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 86 (Release date: 2021-02-23)
prefix
footnote Partial support
prefix Implemented with the vendor prefix: -webkit-
footnote The :-webkit-autofill pseudo-class matches autofilled username and password fields, but not other autocompleted fields. See bug 1923525.
Firefox for Android – Partial support
Firefox for Android 86 (Release date: 2021-02-23)
footnote Partial support
footnote The :autofill pseudo-class matches autofilled username and password fields, but not other autocompleted fields. See bug 1923525.
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
prefix
prefix Implemented with the vendor prefix: -webkit-
Opera Android – Full support
Opera Android 74 (Release date: 2023-03-13)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
prefix
prefix Implemented with the vendor prefix: -webkit-
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
prefix
prefix Implemented with the vendor prefix: -webkit-
Samsung Internet – Full support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
prefix
prefix Implemented with the vendor prefix: -webkit-
WebView Android – Full support
WebView Android 110 (Release date: 2023-02-07)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
prefix
prefix Implemented with the vendor prefix: -webkit-
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also