0

I have applied a font-size: style to my html tag via stylesheet. I would like to retrieve it in javascript.

I have tried doing document.getElementsByTagName("html")[0].style and I end up retrieving the styles with no values.

Sequential
  • 1,455
  • 2
  • 17
  • 27
  • 1
    Is the [`window.getComputedStyle()` method](https://developer.mozilla.org/en/docs/Web/API/Window/getComputedStyle) what you're looking for? – nnnnnn Mar 26 '17 at 06:15
  • @nnnnnn I have tried applying it, but was unsuccessful – Sequential Mar 26 '17 at 06:16
  • @nnnnnn just tried it again and it worked... Thank you. `let elem = document.getElementsByTagName("html")[0] console.log(window.getComputedStyle(elem, null).getPropertyValue("font-size"))` – Sequential Mar 26 '17 at 06:18
  • Hope this will help you. http://stackoverflow.com/questions/2664045/how-to-get-an-html-elements-style-values-in-javascript – Kiran Mar 26 '17 at 06:18
  • Try `document.getElementsByTagName("html")[0].style.fontSize`? – PhilS Mar 26 '17 at 06:20
  • @PhilS this didn't work, unsure why. I had to use `window.getComputedStyle` – Sequential Mar 26 '17 at 06:21
  • Ok thanks, I hadn't tried it, just thought it might work, glad you got it working anyway – PhilS Mar 26 '17 at 06:22

2 Answers2

1

The html element can be referred by document.documentElement Try the following:

var elem = window.getComputedStyle(document.documentElement);
var fontSize = elem.fontSize;
console.log(fontSize);
html{
  font-size: 20px;
}
<html>
  <head>
  </head>
  <body>
  </body>
</html>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

Please try the following:

let elem = document.getElementsByTagName("html")[0];
console.log(window.getComputedStyle(elem, null).getPropertyValue("font-size"));
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Sequential
  • 1,455
  • 2
  • 17
  • 27