首页 > 解决方案 > 文本输入中自动填充的 CSS 字体系列问题

问题描述

我正在尝试为输入设置等宽字体,但是当自动填充启动并在自动填充下拉菜单选项之间切换时,文本输入的自动填充状态内的字体系列不会显示为指定的等宽字体,请参阅到此代码并将字体系列更改为等宽以描绘我的问题(我正在使用 Chrome btw): CSS 技巧的 Codepen 示例

/* Change autocomplete styles in WebKit */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
textarea:-webkit-autofill,
textarea:-webkit-autofill:hover,
textarea:-webkit-autofill:focus,
select:-webkit-autofill,
select:-webkit-autofill:hover,
select:-webkit-autofill:focus {
  border: 1px solid green;
  -webkit-text-fill-color: green;
  -webkit-box-shadow: 0 0 0px 1000px #000 inset;
  transition: background-color 5000s ease-in-out 0s;
}

/* PRESENTATIONAL STYLES */
body {
  background: #333;
  color: #fff;
  display: flex;
  font-family: monospace;
  font-size: 3em;
  justify-content: center;
}

form {
  padding: 50px 0;
  width: 50%;
}
<form>
  <div class="form-group">
    <label for="exampleInputFirst">First Name</label>
    <input type="text" class="form-control input-lg" id="exampleInputFirst">
  </div>
  <div class="form-group">
    <label for="exampleInputLast">Last Name</label>
    <input type="text" class="form-control input-lg" id="exampleInputLast">
  </div>
  <div class="form-group">
    <label for="exampleInputEmail">Email Address</label>
    <input type="email" class="form-control input-lg" id="exampleInputEmail">
  </div>

  <button type="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
</form>

标签: css

解决方案


这里的解决方案是input:-webkit-autofill::first-line选择器。

它允许您在鼠标悬停在自动完成元素上时覆盖默认系统字体(和字体大小)。


推荐阅读