首页 > Chrome 自动填充的表单是淡黄色的背景,有方法自定义吗

Chrome 自动填充的表单是淡黄色的背景,有方法自定义吗

当记住用户名和密码后,下次填写表单时,被记住的部分会被填充为淡黄色,有些时候不好看,能不能自定义呢,该如何自定义?


这个本人原创文章,原文链接

去掉chrome记住密码后自动填充表单的黄色背景

写本文起因

chrome浏览器自动填充表单的黄色背景高亮(#FAFFBD)一直困扰着我,我之前没想着理它,可是最近一个登陆框,需要用到图标,于是我草率的直接设置在input元素里面,结果问题就来了,很难看很难看,因此还是总结一下。

这个问题,在2008年的时候就已经存在了,隔了好几年了,在chromium上面可以找到 Issue 46543,但是官方好像没有理这个问题,英文没怎么看懂,谁理解的,可以给大家分享一下。

思路一: 打补丁

Webkit内核的浏览器有一个-webkit-autofill私有属性,

通过审查元素可以看到这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对其赋予以下样式:

cssinput:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
  background-color: rgb(250, 255, 189); /* #FAFFBD; */
  background-image: none;
  color: rgb(0, 0, 0);
}

因此我们就会想到覆盖这个样式,代码如下,但是依然不能覆盖原有的背景、字体颜色。需要注意的是:加 !important 依然不能覆盖原有的背景、字体颜色,除了chrome默认定义的background-colorbackground-imagecolor不能用 !important 提升其优先级以外,其他的属性均可使用!important提升其优先级。

cssinput:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
  background-color: #FFFFFF;
  background-image: none;
  color: #333;
  /* -webkit-text-fill-color: red; //这个私有属性是有效的 */
}
input:-webkit-autofill:hover {
  /* style code */
}
input:-webkit-autofill:focus {
  /* style code */
}

情景一:input文本框是纯色背景的

解决办法:

cssinput:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px white inset;
  -webkit-text-fill-color: #333;
}

情景二:input文本框是使用图片背景的

解决办法:

jsif (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0)
{
  var _interval = window.setInterval(function () {
    var autofills = $('input:-webkit-autofill');
    if (autofills.length > 0) {
      window.clearInterval(_interval); // 停止轮询
      autofills.each(function() {
        var clone = $(this).clone(true, true);
        $(this).after(clone).remove();
      });
    }
  }, 20);
}

下面的js不是太靠谱

jsif (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
  $(window).load(function(){
    $('input:-webkit-autofill').each(function(){
      var clone = $(this).clone(true, true);
      $(this).after(clone).remove();
    });
  });
}

思路二: 关闭浏览器自带填充表单功能

设置表单属性 autocomplete="off/on" 关闭自动填充表单,自己实现记住密码

html<!-- 对整个表单设置 -->
<form autocomplete="off" method=".." action="..">
<!-- 或对单一元素设置 -->
<input type="text" name="textboxname" autocomplete="off">

网上大部分文章是使用Cookie实现记住用户名、密码。不管是在前端,还是后端都可以实现。本文不对Cookie存储展开讨论。可自行谷歌

其他

stackoverflow.com上面也有类似的回答
Google Chrome form autofill and its yellow background
Override browser form-filling and input highlighting with HTML/CSS

总结

// TODO: 待编辑


自动填充变成透明的话,google下还有另一种曲线救国的方式

:-webkit-autofill {
  -webkit-text-fill-color: #fff !important;
  transition: background-color 5000s ease-in-out 0s;
}

就是让浏览器颜色过5000s再变,还是蛮有用的


修改:有辦法直接修改。

input:-webkit-autofill {
    background-color: rgb(250, 255, 189);
    background-image: none;
    color: rgb(0, 0, 0);
}

貌似不能覆蓋默認樣式,但是可以用別的樣式曲線達到目的。

input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px white inset;
}

效果:

第一個應用了自定義樣式,第二個作對照。


原先的思路:

屏蔽 Chrome 的功能,自己實現一個,和原生功能模仿得像一點。

好吧,其實你也可以自己繪製 input,將真正的 input 用 css 隱藏起來,就像這樣:

input#in {
    position: fixed;
    top: -10000px;
}

加載完畢檢測一下 input 有沒有被 chrome 填充,有的話,應用自定義樣式。

這個思路甚至可以更改光標的樣式和顏色,示例如下:

【热门文章】
【热门文章】