There are a couple of things wrong with your code.
First, getComputedStyle
expects its first argument to be a single DOM element.
getElementsByClassName
returns a
HTMLCollection
,
an array-like object containing a live
collection of DOM elements. That
is why, if you look in your error console, you should see an error message along
the lines of "TypeError: Argument 1 of Window.getComputedStyle does not
implement interface Element.".
Second, getComputedStyle
returns a
CSSStyleDeclaration
object, which does not have a .fontSize
method. It does however have a
getPropertyValue
method that you can use to get the font size.
// use querySelector, which returns a single DOM element that is the first in
// the DOM to match the provided query string
let menu = document.querySelector(".menu");
// You can just omit the second parameter if you are not targeting a pseudo
// element.
let styles = window.getComputedStyle(menu);
// get the font size in px
let fontSize = styles.getPropertyValue('font-size');
fontSize = parseFloat(fontSize);
alert('font-size =' + fontSize);
.menu {
font-size: 1.5em;
}
<div class="menu">Menu</div>