【问题标题】:How to prevent Chrome from changing font when autofilling username/password?自动填充用户名/密码时如何防止Chrome更改字体?
【发布时间】:2019-09-25 07:40:27
【问题描述】:

我有一个包含用户名和密码输入的登录表单。在 Windows 上的 Chrome 中(在其他浏览器或 Mac 上不会发生),将鼠标悬停在 Chrome 密码管理器中保存的用户名上时,字体会发生变化。然后字体更改会更改输入的宽度,从而使我的对齐方式不正常。

显然我可以为我的输入设置一个固定宽度以保存我的对齐方式,但这并不能真正解决问题,只是在上面贴上创可贴。我真正想要的是首先防止字体更改。

我尝试使用:-webkit-autofill 定位输入并将!important 放在我输入的css 中,只是为了看看是否有任何东西会粘住但没有骰子。

代码笔here。您需要在 Windows 上使用 Chrome 并使用 Google 的密码管理器。

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;
    }
  }
}

任何关于防止这种字体更改的线索将不胜感激!

【问题讨论】:

  • 我也有同样的问题。在我看来,这是 Chrome 中的一个错误。我决定将所有不断改变宽度的输入字段放在 flexbox 容器中,以保持宽度稳定作为临时解决方法。
  • 与您在这里所说的相反,这不是特定于 Windows 的。所有桌面版本的 Chrome 都将存在相同的错误。如果您没有在 Mac 上观察到它,我怀疑这是因为您在该 Mac 上安装的 Chrome 在您测试时不是最新的。 (请注意,在您提出此问题前不到一个月,该错误就被引入 Chrome 的发布版本。)

标签: html css


【解决方案1】:

这是 2021 年对我有用的有效解决方案,可防止 Chrome 更改密码/用户名输入字段的字体:

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>

我还注意到由于某种原因display: flex 与该代码冲突,请查看:

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>

【讨论】:

  • 这对我有用。
  • 感谢您对display: flex 的补充说明。这是为我解决问题的关键。
【解决方案2】:

如何在输入时更改 Chrome 自动完成样式:

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;
}

}

【讨论】:

    【解决方案3】:

    试试这个!

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

    你可能只需要这个

         &:-webkit-autofill::first-line
    

    其他的只是以防万一

    【讨论】:

    • 你知道吗,它成功了!我只需要第一行。谢谢@cup_of!
    • 好吧,这么多年了,这就是这个问题的最终答案!唯一需要注意的是,您必须重复字体系列、字体大小和字体粗细的样式。
    • 于 2021 年 1 月实施,input:-webkit-autofill::first-line 在 Chrome 上正常工作。
    • 在我的情况下不起作用
    【解决方案4】:

    这似乎是 Chrome 的最新变化:Issue 953689: Previously entered form values overrides styling when moused over。据我所见,这种情况在 macOS 和 Windows 上都会发生,自动填充出现在最终用户的任何地方。显然,这样做是为了解决先前实现的安全问题 - Issue 916838: Security: Two autocomplete flaws together allow sites to invisibly read credit card numbers after a single keypress

    目前似乎没有办法覆盖这些新样式 - 如果样式的更改实在太令人讨厌,您可以禁用自动填充 (Disabling Chrome Autofill) 或设置字段的字体样式 (@987654329 @、font-weightfont-stylefont-size 以匹配 Chrome 的自动填充建议 - 如下所示:https://stackoverflow.com/a/56997845/1798491)

    【讨论】:

    • 嘿。从这里的观点和投票来看,很多人对我对该漏洞利用报告造成的损害感到恼火。对于那个很抱歉。在我的辩护中,我did warn Google SecurityTeam 认为此更改有 UX 缺点并建议进行另一个修复,但他们还是继续这样做了。从好的方面来说,a commit 现在公开实施替代修复,所以也许我们很快也会看到强制使用默认字体。
    • @MarkAmery 知道这个修复什么时候发布吗?
    • 在 macOS Chrome 版本 95 上不再为我工作。Chrome 删除自动填充字体样式的原因归结为安全问题 bugs.chromium.org/p/chromium/issues/detail?id=1035058::first-line 是解决他们最初的创可贴“修复”的一个技巧,但他们现在也修复了我们的解决方法。值得注意的是,Chrome 96 将支持 :autofill chromestatus.com/feature/5592445322526720 但我不确定 Chrome 会在多大程度上让我们对其进行样式设置。
    • 再次签到说 :autofill 遗憾地不允许更改字体大小(尚未测试其他字体属性)。
    【解决方案5】:

    到目前为止,Chrome 似乎无法更改这一点。我肯定会称之为错误。

    但是,一个不错的解决方法是为所有可自动填充的输入或具有自动填充功能的表单中的输入设置font-family

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

    这是一个很棒的跨浏览器、跨平台解决方案,因为它只需要你的系统字体,这正是 Chrome 似乎正在为它的自动填充字体所做的。

    它还确保您的表单在用户使用的任何操作系统上都具有可读字体。

    【讨论】:

      【解决方案6】:

      我不在 Windows 上运行,但您是否也尝试过定位标签和表单?回复:CSS 特异性。然后尝试所有的 web-kit 自动填充

      【讨论】:

      • 要是这么简单就好了!将其添加到我的代码笔中,但没有运气。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 2010-11-29
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      相关资源
      最近更新 更多