javascript - modify css parent of html element

css has
css selectors
css select parent based on child
apply css to parent div
scss parent selector
css parent selector 2019
css element
child class to parent class css

So, I have a selector which matches a <td>. This <td> inherits from a css class. Let's call it item.

returns "", but




I want to modify the css class so that the backgroundImage is something else for all inheritors.

I haven't been able to figure out how to do this! I can change the backgroundImage of the individual element, but I don't know how to get the class object that defines backgroundImageoriginally.

Write new CSS for target class and put it in <style> and append it to head tag.

var style = '<style>.item {background: red !important}</style>';
document.querySelector('head').innerHTML += style;
.item {
  width: 50px;
  height: 50px;
  border: 1px solid #000;
  background: green;
<div class="item"></div>
<div class="item"></div>

Parent Selectors in CSS, Another solution is to combine a bit of mutation observer javascript with the css rel selector to provide a “lookahead”. So when an element changes in the dom, simply record that elements selector(with the id and classes and possible events, such as hover) into an attribute of the shared parent. Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

You can use unset to reset the inherited property. See the snippet below. The second cell (whose id is dontInherit) won't receive the background-color property.

td {
  background-color: #f0f0ff;

td#dontInherit {
  background-color: unset;
    <td id="inherit">
    <td id="dontInherit">
      Not inherited

CSS element>element Selector, how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. Select and style every <p> element where the parent is a <div> element: The element>element selector is used to select elements with a specific parent. Definition and Usage. The element>element selector is used to select elements with a specific parent.. Note: Elements that are not directly a child of the specified parent, are not selected.

I would add one more thing to @Mohammad's answer.

If you want it for dynamic class then you can get the className and change it's style and add it to head tag.

See the Snippet below:

var item = document.querySelector("#itemA");

var style = '<style>.'+item.className+' {background-image: url("") }</style>';

document.querySelector('head').innerHTML += style;
  background-image: url('');
  <tr><td id="itemA" class="item-A">A for Apple</td></tr>
  <tr><td class="item-B">B for Ball</td></tr>
  <tr><td class="item-C">C for Cat</td></tr>
  <tr><td class="item-A">A for Ant</td></tr>

Selecting Parent Elements With CSS and jQuery, How do you target a child to a parent in CSS? Definition and Usage. The parentElement property returns the parent element of the specified element. The difference between parentElement and parentNode, is that parentElement returns null if the parent node is not an element node:

Manipulating CSS with JavaScript · WebPlatform Docs, How do I give a parent a div in CSS? The parent element might be a div or a p. So, how can we properly select the parent element based on certain states of its descendants? Possible Workarounds. As we’ve covered, there is no such thing as a parent selector, so selecting a parent isn’t possible. We might be tempted to sidestep the problem, by completely reconsidering the design

i dont know what a parent element is or how to use css to fill the , elements up to but not including the element specified in the parentheses. The children property returns a collection of an element's child elements, as an HTMLCollection object. The elements in the collection are sorted as they appear in the source code and can be accessed by index numbers. The index starts at 0.

parent element, a document's style dynamically. By working with the document's list of stylesheets—for example: adding, removing or modifying a stylesheet. By working with the rules in a stylesheet—for example: adding, removing or modifying a rule. The focus of CSS is on the HTML element. CSS answers the question of how a tag appears to the viewer. There are two methods of working with HTML elements statically: as part of the individual tag and within a header that defines a style for all tags of the same type.

  • You can change the rule of the CSS that applies:
  • Did an answer suit your question?