Home > OS >  Access div from its css properties
Access div from its css properties

Time:09-14

I have this div that I need to change the background color of, however it does not have any class or an id to access it.

<div style="margin-bottom:10px;background-color:#fff;padding:10px;">
</div>

The reason I can't add a class is because I am making an extension for the website, not the actual website, and it needs to access it and change the background color (dark mode).

CodePudding user response:

You can use an attribute selector. Be aware that this will only work

  • as long as the attribute (style) value doesn't change, not in a single character;
  • if you need to overwrite any of the inline styles being set in that div, you need to make your CSS rule !important to beat the inline style specificity.

div[style="margin-bottom:10px;background-color:#fff;padding:10px;"] {
  background-color: orange !important;
}
<div style="margin-bottom:10px;background-color:#fff;padding:10px;"></div>

  • Related