-2

I have this in my 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>

And this in my SCSS:

input[name=firstName]:focus {

    background-color: blue !important;

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

the blue background for the input works but the label doesn't change at all. Am I doing this wrong?

georgej
  • 3,041
  • 6
  • 24
  • 46

1 Answers1

2

You need to put the label next to the input box and use + or ~ selector, i.e.

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

For visually reordering the input and label positions, see the simple demo with 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>
Stickers
  • 75,527
  • 23
  • 147
  • 186