1

Here, I want to edit some attributes of my label when there is a focus event on input field so,

But it is not working, when the input field gains focus there is no change in label element what should i do both of the below methods are not working for me.

I am writing it as,

  .input-field:focus + .input-label{
     background : black;
   }
   or
  .input-field:focus .input-label{
     background : black;
   }

Both the methods are not working for me.

Artem Arkhipov
  • 5,138
  • 5
  • 25
  • 44
pratik
  • 11
  • 2

1 Answers1

0

Use :focus-within pseudo selector to achieve the desired functionality. This selector works if element receives focus or contains another element which receives focus.

.container:focus-within label{
  background-color: red;
}
<div class="container">
  <label>Label</label>
  <input type="text">
</div>

You can read more about this selector here. Note, that this method is not supported in IE, but who cares...

Artem Arkhipov
  • 5,138
  • 5
  • 25
  • 44