Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS specificity when Javascript modifies CSS?

What is the CSS specificity when Javascript modifies CSS?

Such as:

document.getElementById("demo").style.color = "red";

Is it considered inline styling?

like image 824
Kingamere Avatar asked Oct 15 '25 20:10

Kingamere


1 Answers

CSS specificity when Javascript modifies CSS?

Exactly the same as if you modified the CSS by editing the original source code instead of using JS.

document.getElementById("demo").style.color = "red";

In this case, you are modifying the styles attached directly to the element.

<div id="demo" style="color: red">

So maximum specificity. (count 1 if the declaration is from is a 'style' attribute rather than a rule with a selector)

If you had used JS to modify a ruleset, then the specificity would be different.

like image 198
Quentin Avatar answered Oct 18 '25 14:10

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!