【问题标题】:input:not(:placeholder-shown) ~ label selector does not work with autofillinput:not(:placeholder-shown) ~ 标签选择器不适用于自动填充
【发布时间】:2019-08-21 00:23:54
【问题描述】:

我在输入字段中有浮动占位符。

当我们输入的值没有提供时,占位符会出现在中心。如下图所示(邮箱和密码为占位符)。

现在,当您向电子邮件提供值时,它确实如下所示。提供值时,观察电子邮件和密码已被提取

当浏览器从页面加载时保存的凭据(如用户名、电子邮件、密码等)开始自动填充/自动完成此值时,就会出现问题。请看下面的屏幕截图:

css

:root {
  --input-padding-x: .75rem;
  --input-padding-y: .75rem;
}

html,
body {
  height: 100%;
}

.form-signin {
  width: 100%;
  max-width: 600px;
  padding: 15px;
  margin: auto;
  padding-top: 40px;
  padding-bottom: 40px;
}

.form-label-group {
  position: relative;
  margin-bottom: 1rem;
}

.form-label-group > input,
.form-label-group > label {
  padding: var(--input-padding-y) var(--input-padding-x);
}

.form-label-group > label {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: 100%;
  margin-bottom: 0; /* Override default `<label>` margin */
  line-height: 1.5;
  color: #495057;
  border: 1px solid transparent;
  border-radius: .25rem;
  transition: all .1s ease-in-out;
}

.form-label-group input::-webkit-input-placeholder {
  color: transparent;
}

.form-label-group input:-ms-input-placeholder {
  color: transparent;
}

.form-label-group input::-ms-input-placeholder {
  color: transparent;
}

.form-label-group input::-moz-placeholder {
  color: transparent;
}

.form-label-group input::placeholder {
  color: transparent;
}

.form-label-group input:not(:placeholder-shown) {
  padding-top: calc(var(--input-padding-y) + var(--input-padding-y) * (2 / 3));
  padding-bottom: calc(var(--input-padding-y) / 3);
}

.form-label-group input:not(:placeholder-shown) ~ label {
  padding-top: calc(var(--input-padding-y) / 3);
  padding-bottom: calc(var(--input-padding-y) / 3);
  font-size: 12px;
  color: #777;
}

HTML

<form class="form-signin">
    <h1 class="h3 mb-3 font-weight-normal">Please Type new password</h1>
    <div class="form-label-group">
        <input type="text" id="inputEmail" [(ngModel)]="email" name="email" class="form-control" placeholder="Email" required=""
            autofocus="">
        <label for="inputEmail">Email</label>
    </div>

    <div class="form-label-group">
        <input type="password" id="inputPassword" [(ngModel)]="password" name="password" class="form-control" placeholder="Password"
            required="">
        <label for="inputPassword">Password</label>
    </div>

    <div class="row">
      <div class="col-sm-6">
        <button class="btn btn-lg btn-primary btn-block" (click)="onSubmit()" type="button">Change password</button>
      </div>
    </div>
</form>

我已经尝试自动关注电子邮件字段,但没有奏效。 我还尝试在页面加载后单击代码中的元素,但没有运气。请帮我解决这个问题。

【问题讨论】:

    标签: css css-selectors placeholder pseudo-class


    【解决方案1】:

    对我有用

    .form-label-group input:-webkit-autofill ~ label {
            /* CSS property  */
    }
    

    你可以试试

    【讨论】:

    • 感谢:-webkit-autofill 的提示,这正是我所缺少的!
    【解决方案2】:

    我遇到了同样的问题。在我的表单中,我使用以下选择器将我的标签文本移开,满足以下 3 个条件:

    1. :focus(所以在聚焦时不会妨碍光标)
    2. :not(:placeholder-shown)(表示“输入不为空”)
    3. :-webkit-autofill(因为 2 没有在页面刷新/自动填充时触发)

    SCSS 组合为:

    input {
      &:focus, &:not(:placeholder-shown), &:-webkit-autofill {
        &+label { /* Move your label */ }
      }
    }
    

    (请注意,您的输入还需要placeholder=" "。)

    这是一个活生生的例子:https://codepen.io/jeffward/pen/ZdBxXd

    【讨论】:

    • 在 Chrome 中运行良好,在 FF 中不行。我找不到任何替代方案。
    • 确实,愚蠢的火狐。 :-moz-autofill bugzilla.mozilla.org/show_bug.cgi?id=740979 有一个非常古老的出色功能请求,尽管如果 :placeholder-shown 在自动填充时正常工作,:autofill 甚至都不是必需的。 :P
    猜你喜欢
    • 2012-10-09
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多