How to style parent element based on his child

huhWell

Let's say our html structure looks like this:

<div class="parent">
  <div class="child"></div>
</div>

Now on some button action I add active className to child's div.

My question is:

How to style only parent element if child's div has active className

// CSS pseudo code //

if(child.has.className('active')
.parent{
background: red;
}
Terry

You can use the :has() pseudo class selector, although that's only supported in newer browsers. Otherwise you'll probably need to use JS.

.parent {
  background: #ccc;
}

.parent:has(.active) {
  background: steelblue;
  color: #eee;
}

/* Ignore below, for stylistic purposes only */
.parent {
  margin: 1rem;
  padding: 1rem;
  border-radius: .5rem;
  font-family: Arial, sans-serif;
}
<div class="parent">
  <div class="child">Child</div>
</div>

<div class="parent">
  <div class="child active">Child (active)</div>
</div>

For a JS-based solution there are two ways:

  • Recommended: in the code that adds the active class, you also toggle a class on the parent, say has-active-child and style it accordingly
  • Not recommended: listen to class changes on the child node using MutationObserver API and style the parent node

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to style multiple child elements based on parent element selector condition

How to change opacity of child-element when hovering his parent

How to make parent element the same size as his child which is absolutely positioned to his grandparent?

How to style the parent element when hovering a child element?

How to fit resizable child div to his parent

XSLT, XPath: How to get attribute of parent element with reference on attribute of his child?

How to hide parent table based on child table style visibility

How to get the attribute of a child element based on the attribute of the parent element

How to get child element based on parent element in python selenium

How can set element alignment which is inside parent with the child style setting in element style attributes?

Style child element when hover on parent

Apply css style to child element of parent class

CSS Style parent based on child selector

Hide parent element based on value of child element

CSS or SASS How to change style of ::after, ::before effects of parent element when child element is active

How to style the background of the parent element when hovering more then one child element?

Cypress: Get child element of parent based on another child element

How to position a relative element in the same coordinates as his parent in the document?

How to find all text and his parent inside a DOM element

JQuery hide parent element based on child

R - How to rename xml parent node based on child element (or associate child elements)?

How to pass class style from parent to child in Angular, and using it in specific element

How can to change same css style after hover parent to child tag element?

How to show a child element which is obscured by its parent's overflow:hidden style setting

How to change parent style based on child's value using Jinja2/Python?

How to expand height of child based on parent parent

How to make absolute child element resize based on content, including larger than the parent element?

How do you select a parent element based on the text of it's child element WITHOUT jQuery?

How to style parent if child has class in vue?