:autofill
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since Februar 2023.
Die :autofill
CSS-Pseudoklasse matcht, wenn ein <input>
-Element seinen Wert vom Browser automatisch ausgefüllt bekommen hat. Die Klasse matcht nicht mehr, wenn der Benutzer das Feld bearbeitet.
Probieren Sie es aus
label {
display: block;
margin-top: 1em;
}
input:is(:-webkit-autofill, :autofill) {
border: 3px solid darkorange;
}
<form>
<p>Click on the text box and choose any option suggested by your browser.</p>
<label for="name">Name</label>
<input id="name" name="name" type="text" autocomplete="name" />
<label for="email">Email Address</label>
<input id="email" name="email" type="email" autocomplete="email" />
<label for="country">Country</label>
<input id="country" name="country" type="text" autocomplete="country-name" />
</form>
Hinweis:
Die Benutzeragenten-Stylesheets vieler Browser verwenden !important
in ihren :-webkit-autofill
-Style-Deklarationen, was sie ohne JavaScript-Hacks für Webseiten nicht überschreibbar macht. Zum Beispiel hat Chrome folgendes in seinem internen Stylesheet:
background-color: rgb(232 240 254) !important;
background-image: none !important;
color: -internal-light-dark(black, white) !important;
Das bedeutet, dass Sie background-color
, background-image
, oder color
in Ihren eigenen Regeln nicht setzen können.
Syntax
:autofill {
/* ... */
}
Beispiele
Das folgende Beispiel demonstriert die Verwendung der :autofill
-Pseudoklasse, um den Rahmen eines Textfeldes zu ändern, das vom Browser automatisch ausgefüllt wurde.
Um sicherzustellen, dass wir keine ungültige Selektorliste erstellen, werden sowohl :-webkit-autofill
als auch :autofill
mit einer großzügigen Selektorliste mit :is()
gematcht.
input {
border-radius: 3px;
}
input:is(:-webkit-autofill, :autofill) {
border: 3px dotted orange;
}
<form method="post" action="">
<label for="email">Email</label>
<input type="email" name="email" id="email" autocomplete="email" />
</form>
Spezifikationen
Specification |
---|
HTML> # selector-autofill> |
Selectors Level 4> # selectordef-autofill> |
Browser-Kompatibilität
Loading…
Siehe auch
- Chromium Issue 46543: Auto-filled input text box yellow background highlight cannot be turned off
- WebKit Bug 66032: Allow site authors to override autofilled fields' colors.
- Mozilla Bug 740979: implement
:-moz-autofill
pseudo-class on input elements with an autofilled value - User Interface Module Level 4: more selectors