0

I have div in which I have input and button .I want to change the background-color of parent div when i focused to input div .but it is not working .Can we do this using css not javascript.

I tried like this

.a:focus{
  background-color:red;
  border:2px solid grey;
} 

But it not work why ?

I want when I focus to input field than parent div which have class "a" will become background-color:red and border become red

here is my code https://jsbin.com/kimejehane/1/edit?html,css,output

<div class="a">
    <div class="b">
      <input type="text">
      <button>aa</button>
    </div>
  </div>
user944513
  • 12,247
  • 49
  • 168
  • 318

1 Answers1

3

If you want to change background of parant div having class b, then try out this.

.b:focus-within {
  background-color:red;
  border:2px solid grey;
}
ImZedi
  • 294
  • 4
  • 15
  • 1
    This is still a Working Draft in CSS Selectors Level 4 https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within – dippas Dec 09 '18 at 13:59