Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve CSS value, not inline-style value

Tags:

javascript

In Javascript, can I retrieve the CSS values of an element without taking account of its inline style?

example:

body { font-size: 15px; }

<body style="font-size: 20px;">

Is there a way I can retrieve "15px" and not "20px"?

like image 600
Simon Arnold Avatar asked Sep 20 '25 16:09

Simon Arnold


1 Answers

Yes, of course! Just get rid of the style attribute, use getComputedStyle(), and add the style attribute back:

//Get the body's style attribute:
var bodyStyle = document.body.getAttribute("style");
//Now, get rid of it:
document.body.removeAttribute("style");
//Now use getComputedStyle() to compute the font size of the body and output it to the screen:
document.write( getComputedStyle(document.body).fontSize );
//Now add the style attribute back:
document.body.setAttribute("style", bodyStyle);
body { font-size: 15px; }
<body style="font-size: 20px;">
</body>
like image 111
Noble Mushtak Avatar answered Sep 22 '25 07:09

Noble Mushtak