How do I change the background color of the element if there is a child element?

Asked

Viewed 77 times

0

I have the body tag and the background color will be changed if there is a div.login as a daughter.

...
<body>
  <div class="container"><div class="login"></div></div>
<body>
...

1 answer

0

There are no Parent selectors in CSS.

There are several ways to dynamically select the parent of an element, but they all involve a bit of Javascript such as jQuery’s Parent() method.

Using SASS, you can do something using the ampersand operator:

Try to use:

div.container {
    body & {
      background-color: red;
  }
}

I’m starting now, I don’t know if it’s the best way to be doing it. Test and see if it works. I hope I’ve helped!!

  • with Sass I really could not, I did the process with javascript same. vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.