62

I have a login form with username and password inputs. In Chrome on Windows (doesn't happen in other browsers or on a Mac), when hovering over a saved username from the Chrome password manager, the font changes. The font change then changes the width of the input, throwing my alignment out of whack.

Obviously I can set a fixed width to my input to save my alignment but that doesn't really solve the problem, just puts a band-aid on it. What I really want is to prevent the font change in the first place.

I've tried targeting the input with :-webkit-autofill and putting !important all over my input's css just to see if anything would stick but no dice.

Codepen here. You'll need to be in Chrome on Windows and use Google's password manager.

HTML:

<form>
    <label>
        <input type="text" name="username" placeholder="Username" required />
    </label>
    <label>
        <input type="password" name="password" placeholder="Password" required />
    </label>
  <button type="submit">Login</button>
</form>

SCSS:

// setting font for more exaggerated difference
* {
  font-family: Times, "Times New Roman", serif;
}

// why doesn't this or anything else work?
input {
  &:-webkit-auto-fill {
    &,
    &:hover,
    &:focus {
    font-family: Times, "Times New Roman", serif !important;
    }
  }
}

Any clues on preventing this font change would be appreciated!

shainanigans
  • 872
  • 1
  • 6
  • 17
  • 1
    I have the same issue. It looks to me like it's a bug in Chrome. I decided to put all my input fields that kept changing the width in flexbox containers, to keep the width stable as a temporary workaround. – Rias Jun 24 '19 at 12:34
  • 1
    Contra what you say here, this is not Windows-specific. The same bug will exist in all desktop versions of Chrome. If you didn't observe it on a Mac, I suspect it's because your install of Chrome on that Mac wasn't up to date at the time that you tested. (Note that the bug was introduced into a release version of Chrome less than a month before you asked this question.) – Mark Amery Jan 03 '20 at 15:16
  • The same question in https://stackoverflow.com/questions/57551530/how-can-i-remove-the-styles-from-the-pseudo-element-internal-input-suggested – Binyamin Sep 29 '20 at 20:14
  • Does this answer your question? [Input text very small when hovering on autofill suggestion](https://stackoverflow.com/questions/57242841/input-text-very-small-when-hovering-on-autofill-suggestion) – Dzmitry Dranitski Aug 05 '21 at 19:50

7 Answers7

33

try this!

      &:-webkit-autofill::first-line,
      &:-webkit-autofill,
      &:-webkit-autofill:hover,
      &:-webkit-autofill:focus,
      &:-webkit-autofill:active {
        font-family: Times, "Times New Roman", serif !important;
      }

you might only need this though

     &:-webkit-autofill::first-line

the others are just incase

cup_of
  • 6,397
  • 9
  • 47
  • 94
27

This seems to be a recent change in Chrome: Issue 953689: Previously entered form values overrides styling when moused over. As far as I’ve seen this happens both on macOS and Windows, anywhere autofill is presented to the end user. Apparently this has intentionally been done to fix a security issue with the previous implementation – Issue 916838: Security: Two autocomplete flaws together allow sites to invisibly read credit card numbers after a single keypress

There doesn’t seem to be a way to override those new styles at the moment – if the change in styles is really too obnoxious, you can either disable autofill (Disabling Chrome Autofill) or set your field’s font styles (font-family, font-weight, font-style, font-size to match that of Chrome’s autofill suggestions – as suggested here: https://stackoverflow.com/a/56997845/1798491)

Thibaud Colas
  • 1,248
  • 1
  • 16
  • 24
  • 12
    Heh. Judging by the views and votes here, a lot of people are annoyed by the damage I caused with that exploit report. Sorry about that. In my defence, I [*did* warn Google SecurityTeam](https://bugs.chromium.org/p/chromium/issues/detail?id=916838#c16) that this change had UX downsides and suggest another fix, but they went ahead and did it anyway. On the bright side, there's [a commit](https://chromium.googlesource.com/chromium/src.git/+/39f06061af8da287363cba093071ec348ef642c2) now public implementing the alternative fix, so maybe we will see the forcing of a default font reverted soon too. – Mark Amery Jan 03 '20 at 14:24
  • 3
    @MarkAmery any idea when this fix will be released? – Lenny D Mar 04 '20 at 10:33
  • 3
    No longer working for me on macOS Chrome Version 95. The reason Chrome removed autofill font face styles was down to a security concern https://bugs.chromium.org/p/chromium/issues/detail?id=1035058. `::first-line` was a trick to workaround their initial bandaid "fix", but they've fixed our workaround now also. Of note, Chrome 96 will support `:autofill` https://www.chromestatus.com/feature/5592445322526720 but I'm not sure to what degree Chrome will let us style it. – Kerry Johnson Nov 04 '21 at 17:47
  • 2
    Checking back in to say that `:autofill` sadly doesn't allow font size changes (haven't tested other font properties). – Kerry Johnson Jan 06 '22 at 06:29
  • 1
    @MarkAmery i can see `::-internal-input-suggested` in the dev tools that still overrides everything when the page is loaded and autofilled, or while hovering over suggestions in the dropdown with `font: -webkit-small-control !important;`. Any clues what the correct pseudo-selector for this would be? – Philzen Jul 05 '22 at 00:55
  • @MarkAmery Next time, please don't suggest a 5th option for those lazy Chromium devs to choose >.< (SSO and login pages have suffered for 3 years now) – Nate Nov 11 '22 at 17:46
5

Here is working solution that worked for me in 2021 to prevent Chrome from changing font on password/username input fields:

input#username {
  font-family: "Rubik";
  font-weight: bold;
  color: blue;
}

input:-webkit-autofill::first-line {
      color: red;
      font-family: "Rubik"; 
      font-weight: bold;
}
<link href="https://fonts.googleapis.com/css?family=Rubik&display=swap" rel="stylesheet">
<input id="username" name="username"></input>

Also I noticed that for some reason display: flex conflicts with that code, take a look:

input#username {
  font-family: "Rubik";
  color: blue;
  font-weight: bold;
  display: flex;
}

input:-webkit-autofill::first-line {
      color: red;
      font-family: "Rubik";
      font-weight: bold;
}
<link href="https://fonts.googleapis.com/css?family=Rubik&display=swap" rel="stylesheet">
<input id="username"></input>
Dzmitry Dranitski
  • 165
  • 1
  • 4
  • 13
  • 1
    Thanks for the additional note about `display: flex`. It was key to fixing the issue for me. – Anson Nov 02 '21 at 23:22
  • 4
    Works after selection, but unfortunately **doesn't work** while hovering over an entry, which is what the OP asked for. – Philzen Jul 05 '22 at 01:04
  • 1
    Also, this solution doesn't work to preserve font-size – Nate Nov 11 '22 at 16:03
2

How to change Chrome autocomplete styles on input:

input {
...
font-family: $body-font;
font-size: 1rem;
font-weight: bold;
color: #000;
background-color: #fff;
  
// Background color
&:-webkit-autofill {
  -webkit-box-shadow: 0 0 0 1000px #fff inset;
}
  
// Font styles
&:-webkit-autofill::first-line {
  font-family: $body-font;
  font-size: 1rem;
  font-weight: bold;
  // color: green;
}

}

StefanBob
  • 4,857
  • 2
  • 32
  • 38
2

This is the only way I've found to get around the problem of syncing the font-size when autofill is being used. This zooms the autofill and then resizes the autofill::first-line font-size, which seems to give you independent control of both. Line-height needs to be adjusted accordingly. Password input needs a different line-height around 1.25 with these settings (still jumps a little).

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus {
    zoom: 1.1666;
}

input:-webkit-autofill::first-line {
    font-size: 0.8333rem;
    line-height: 1.5;
}
DusmaN
  • 43
  • 3
0

I don't run on windows but have you tried targeting the label and form as well? Re: css specificity. Then try web-kit auto-fills on all

CCodes
  • 43
  • 1
  • 9
-1

As of now it seems that there's no way to change this in Chrome. I'd definitely call it a bug.

However, a decent workaround is to set the font-family for all autofill-able inputs or inputs within forms that have autofill abilities to this:

font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Ubuntu, Arial, sans-serif;

This is a great cross-browser, cross-platform solution because it just takes whatever your system font is, which is exactly what Chrome seems to be doing for it's autofill font.

It also ensures that your forms are going to have readable fonts on whatever OS your user is using.

d35348w
  • 151
  • 1
  • 9