简体   繁体   English

如何避免 Chrome 在自动填充输入上隐藏背景图像和颜色?

[英]How to avoid Chrome hide background image and color over an autofill input?

Chrome Browser auto-fill removed effect of background-color and background-image from Username and Password input fields. Chrome 浏览器自动填充从Username名和Password输入字段中删除了background-colorbackground-image的效果。

Before autocomplete自动完成之前

在此处输入图像描述

After autocomplete自动完成后

在此处输入图像描述

But Chrome Browser autocomplete hide my icons from inputs and also changed my background-color .但是Chrome 浏览器自动完成从输入中隐藏了我的图标,并且还改变了我的background-color So i need my icons stay on inputs as it is.所以我需要我的图标保持原样。

It is possible to stop Chrome Browser from change background color of fields and hide images?可以阻止Chrome 浏览器更改字段的背景颜色并隐藏图像吗?

 .form-section.form-control { border-radius: 4px; background-color: #f7f8fa; border: none; padding-left: 62px; height: 51px; font-size: 16px; background-repeat: no-repeat; background-position: left 17px center; }.form-section.form-control:focus { -webkit-box-shadow: none; box-shadow: none; }.form-section.form-group { margin-bottom: 21px; }.form-section.form-control[type="email"] { background-image: url('https://i.stack.imgur.com/xhx3w.png'); }.form-section.form-control[type="password"] { background-image: url('https://i.stack.imgur.com/910l0.png'); }.form-btn { padding:10px; background-color: #65a3fe; border: none; color: #ffffff; font-size: 18px; font-weight: 700; }
 <div class="form-section"> <form> <div class="form-group"> <input title="Email" type="email" class="form-control" name="email" placeholder="Your email address" value=""> </div> <div class="form-group"> <input title="Password" type="password" class="form-control" name="password" placeholder="Your Password"> </div> <button type="submit" class="btn btn-primary form-btn">Log in</button> </form> </div>

Here you can use any color eg white , #DDD , rgba(102, 163, 177, 0.45) .在这里,您可以使用任何颜色,例如white#DDDrgba(102, 163, 177, 0.45)

For Background: transparent won't work here so please use colors.对于背景: transparent在这里不起作用,所以请使用 colors。

 /* Change the white to any color;) */ input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus, input:-webkit-autofill:active { -webkit-box-shadow: 0 0 0 30px white inset;important }

for text color:对于文本颜色:

 /*Change text in autofill textbox*/ input:-webkit-autofill { -webkit-text-fill-color: yellow;important }

Your Solution: (for above case)您的解决方案:(对于上述情况)

Use separate div for icon and for input对图标和input使用单独的div

 .form-section.form-control { font-size: 16px; width: 100%; margin-left: 9px; }.form-section.form-control:focus { -webkit-box-shadow: none; box-shadow: none; }.form-section.form-group { margin-bottom: 21px; }.icon-email { background-image: url('https://i.stack.imgur.com/xhx3w.png'); background-repeat: no-repeat; background-position: center center; width: 30px; height: auto; }.icon-pass { background-image: url('https://i.stack.imgur.com/910l0.png'); background-repeat: no-repeat; background-position: center center; width: 30px; height: auto; }.form-btn { padding:10px; background-color: #65a3fe; border: none; color: #ffffff; font-size: 18px; font-weight: 700; }.input-txt{ padding-left: 5px; float:left; border-left:none; outline:none; border: none; background-color: #f7f8fa; }.custom-input { display: flex; border-radius: 4px; background-color: #f7f8fa;important: border; none: height; 51px: } input,-webkit-autofill: input:-webkit-autofill,hover: input:-webkit-autofill,focus: input:-webkit-autofill:active { -webkit-box-shadow; 0 0 0 30px #f7f8fa inset important }
 <div class="form-section"> <form> <div class="form-group custom-input"> <div class='icon-email'></div> <input title="Email" type="email" class="form-control input-txt" name="email" placeholder="Your email address" value=""> </div> <div class="form-group custom-input"> <div class='icon-pass'></div> <input title="Password" type="password" class="form-control input-txt" name="password" placeholder="Your Password"> </div> <button type="submit" class="btn btn-primary form-btn">Log in</button> </form> </div>

You CAN NOT Override the autocomplete code, since it's being controlled by user agent stylesheet , take a look at cascading-order to know more.不能覆盖自动完成代码,因为它由用户代理样式表控制,请查看cascading-order以了解更多信息。

Here is the code ( i've copied from google chrome ) that is making your icons disappear on autocomplete, just in case you were wondering :这是使您的图标在自动完成时消失的代码我从 google chrome 复制),以防万一您想知道

 input:-internal-autofill-selected { background-color: rgb(232, 240, 254);important: background-image; none:important, color; -internal-light-dark-color(black white) important }

So what now?所以现在怎么办? If you are intent on solving icons disappearing problem here is a hack that you can do it for you:如果您打算解决图标消失问题,这里有一个技巧,您可以为您解决:

 .form-section.form-control { border-radius: 4px; background-color: #f7f8fa; border: none; padding-left: 62px; height: 51px; font-size: 16px; background-repeat: no-repeat; background-position: left 17px center; }.form-section.form-control:focus { -webkit-box-shadow: none; box-shadow: none; }.form-section.form-group { margin-bottom: 21px; position: relative; }.form-icon { position: absolute; left: 16px; top: 50%; transform: translateY(-50%); }.form-section.form-control[type="password"] { background-image: url('https://i.stack.imgur.com/910l0.png'); }.form-btn { padding: 10px; background-color: #65a3fe; border: none; color: #ffffff; font-size: 18px; font-weight: 700; }
 <div class="form-section"> <form> <div class="form-group"> <input title="Email" type="email" class="form-control" name="email" placeholder="Your email address" value=""> <img class="form-icon" src="https://i.stack.imgur.com/xhx3w.png" alt=""> </div> <div class="form-group"> <input title="Password" type="password" class="form-control" name="password" placeholder="Your Password"> </div> <button type="submit" class="btn btn-primary form-btn">Log in</button> </form> </div>

Please check if this works for you.请检查这是否适合您。 I made some changes on your code.我对您的代码进行了一些更改。

You can check in this jsfiddle你可以检查这个jsfiddle

 <div class="form-section"> <form> <div class="form-group control"> <input title="Email" type="email" class="form-control email" name="email" placeholder="Your email address" value=""> <span class='input-icon'></span> </div> <div class="form-group control"> <input title="Password" type="password" class="form-control pass" name="password" placeholder="Your Password"> <span class='input-icon'></span> </div> <button type="submit" class="btn btn-primary form-btn">Log in</button> </form> </div>.form-section.form-control { border-radius: 4px; background-color: #f7f8fa; border: none; padding-left: 62px; height: 51px; font-size: 16px; background-repeat: no-repeat; background-position: left 17px center; }.form-section.form-control:focus { -webkit-box-shadow: none; box-shadow: none; }.form-section.form-group { margin-bottom: 21px; }.form-btn { padding:10px; background-color: #65a3fe; border: none; color: #ffffff; font-size: 18px; font-weight: 700; }.control { position: relative; }.email.pass { padding-left: 35px; padding-top: 10px; padding-bottom: 10px; font-size: 16px; }.email ~.input-icon { background-image: url('https://i.stack.imgur.com/xhx3w.png'); background-repeat: no-repeat; background-size: 100%; background-position: center center; width: 22px; height: 14px; position: absolute; left: 8px; bottom: 0; top: 0; margin: auto; padding-top: 5px; padding-bottom: 5px; }.pass ~.input-icon { background-image: url('https://i.stack.imgur.com/910l0.png'); background-repeat: no-repeat; background-size: 100%; background-position: center center; width: 22px; height: 14px; position: absolute; left: 8px; bottom: 0; top: 0; margin: auto; padding-top: 5px; padding-bottom: 5px; }

jsfiddle 示例

 /* Change the white to any color;) */ input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus, input:-webkit-autofill:active { -webkit-box-shadow: 0 0 0 30px white inset;important, } /*Additionally: you can use this to change the text color:*/ /*Change text in autofill textbox*/ input:-webkit-autofill { -webkit-text-fill-color; yellow important }

Credit to @Fareed Alnamrouti from Removing input background colour for Chrome autocomplete?归功于 @Fareed Alnamrouti 从删除 Chrome 自动完成的输入背景颜色?

Let's try this code, hope this will helpful to you让我们试试这个代码,希望这对你有帮助

 .form-section.form-control { border-radius: 4px; background-color: #f7f8fa; border: none; padding-left: 62px; height: 51px; font-size: 16px; background-repeat: no-repeat; background-position: left 17px center; }.form-section.form-control:focus { -webkit-box-shadow: none; box-shadow: none; }.form-section.form-group { margin-bottom: 21px; }.form-section.form-control[type="email"] { background-image: url('https://i.stack.imgur.com/xhx3w.png'); } @-webkit-keyframes autofill_email { to { background-image: url('https://i.stack.imgur.com/xhx3w.png'); } }.form-section.form-control[type="email"]:-webkit-autofill { -webkit-animation-name: autofill_email; -webkit-animation-fill-mode: both; }.form-section.form-control[type="password"] { background-image: url('https://i.stack.imgur.com/910l0.png'); } @-webkit-keyframes autofill_pass { to { background-image: url('https://i.stack.imgur.com/910l0.png'); } }.form-section.form-control[type="password"]:-webkit-autofill { -webkit-animation-name: autofill_pass; -webkit-animation-fill-mode: both; }.form-btn { padding: 10px; background-color: #65a3fe; border: none; color: #ffffff; font-size: 18px; font-weight: 700; }
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" /> <section class="py-4"> <div class="container"> <div class="form-section"> <form> <div class="form-group"> <input title="Email" type="email" class="form-control" name="email" placeholder="Your email address" value=""> </div> <div class="form-group"> <input title="Password" type="password" class="form-control" name="password" placeholder="Your Password"> </div> <button type="submit" class="btn btn-primary form-btn">Log in</button> </form> </div> </div> </section>

If you want remove background color, then add default background-color in keyframe style如果要删除背景颜色,请在关键帧样式中添加默认背景颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM