【问题标题】:Highlight label on input focus not working输入焦点上的突出显示标签不起作用
【发布时间】:2023-03-30 09:26:01
【问题描述】:

我的 HTML 中有这个:

<label class="control-label" for="firstName">{{ 'OA_FIRST_NAME' | translate | uppercase }}<sup>*</sup></label>
<input
    formControlName="firstName"
    class="signup-input full-width" 
    type="text"
    name="firstName" 
    required>

这在我的 SCSS 中:

input[name=firstName]:focus {

    background-color: blue !important;

    label[for=firstName] {
        background-color: red !important;
        color: green !important;
    }
}

输入的蓝色背景有效,但标签根本没有改变。我做错了吗?

【问题讨论】:

    标签: html css sass


    【解决方案1】:

    您需要将label 放在input 框旁边并使用+~ 选择器,即

    input:focus + label {
      color: red;
    }
    <input type="text">
    <label>Label</label>

    要直观地重新排序输入和标签位置,请参阅使用 flexbox 的简单演示。

    .fieldset {
      display: inline-flex;
      flex-direction: row-reverse;
    }
    .fieldset label {
      margin-right: 4px;
    }
    .fieldset input:focus + label {
      color: red;
    }
    <div class="fieldset">
      <input type="text">
      <label>Label</label>
    </div>

    【讨论】:

    • 那么有没有办法在输入之前对标签进行这项工作?
    • @georgej 我相信你需要 js。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多