How to prevent Chrome from changing font when autofilling username/password?
Asked Answered
I

7

65

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!

Infamous answered 7/5, 2019 at 15:33 Comment(4)
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.Caster
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.)Nadabas
The same question in #57552030Reichsmark
Does this answer your question? Input text very small when hovering on autofill suggestionVictim
C
34

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

Constantan answered 2/4, 2020 at 8:36 Comment(5)
Well what do you know, it worked! I just needed the first line. Thanks @cup_of!Infamous
Ok, this is the definitive answer for the problem after all these years! Only caveat is you have to repeat the styles for font-family, font-size and font-weight.Airburst
Implemented this in January 2021 and the input:-webkit-autofill::first-line works properly on Chrome.Jiffy
didn't work in my caseMilliary
Yeah this doesn't seem to be working at this point in time.Jobholder
E
28

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://mcmap.net/q/302688/-style-autofill-or-autocomplete-default-browser-dropdown)

Expansionism answered 22/7, 2019 at 13:25 Comment(6)
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 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 now public implementing the alternative fix, so maybe we will see the forcing of a default font reverted soon too.Nadabas
@MarkAmery any idea when this fix will be released?Allman
No longer working for me on macOS Chrome Version 95. The reason Chrome removed autofill font face styles was down to a security concern 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 chromestatus.com/feature/5592445322526720 but I'm not sure to what degree Chrome will let us style it.Dayak
Checking back in to say that :autofill sadly doesn't allow font size changes (haven't tested other font properties).Dayak
@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?Doubletalk
@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)Radley
V
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>
Victim answered 5/8, 2021 at 19:42 Comment(3)
Thanks for the additional note about display: flex. It was key to fixing the issue for me.Serial
Works after selection, but unfortunately doesn't work while hovering over an entry, which is what the OP asked for.Doubletalk
Also, this solution doesn't work to preserve font-sizeRadley
P
4

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;
}
Paly answered 13/2, 2023 at 20:46 Comment(0)
T
3

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;
}
}
Tourism answered 18/9, 2020 at 14:51 Comment(0)
E
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

Euphrasy answered 7/5, 2019 at 15:46 Comment(1)
If only it were so simple! Added that to my codepen but no luck.Infamous
O
-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.

Omnivore answered 11/7, 2019 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.