How to Retrieve Computed Font Size in JavaScript
Determining the actual font size of a DOM element is crucial in various scenarios, such as customizing text editors or designing responsive web layouts. This involves considering not just the element's explicit style but also inherited values and browser-level settings.
Solution
To obtain the computed font size, you can leverage two methods:
1. Browser-Specific Method: currentStyle
For Internet Explorer, you can use the non-standard currentStyle property:
element.currentStyle['fontSize']
Note that property names containing hyphens, like font-size, must be accessed in camelCase (e.g., fontSize) for currentStyle.
2. Standard Method: getComputedStyle
For all other browsers and cross-browser compatibility, you can employ the DOM Level 2 standard getComputedStyle method:
document.defaultView.getComputedStyle(element, null).getPropertyValue('fontSize')
Implementation Example
Define a utility function to fetch the style:
function getStyle(element, styleProp) { var camelize = function (str) { return str.replace(/\-(\w)/g, function(str, letter){ return letter.toUpperCase(); }); }; if (element.currentStyle) { return element.currentStyle[camelize(styleProp)]; } else if (document.defaultView && document.defaultView.getComputedStyle) { return document.defaultView.getComputedStyle(element,null) .getPropertyValue(styleProp); } else { return element.style[camelize(styleProp)]; } }
Usage:
var element = document.getElementById('elementId'); getStyle(element, 'font-size');
Additional Tips
- Check the existence of document.defaultView before accessing getComputedStyle.
- If both currentStyle and getComputedStyle are unavailable, use element.style to retrieve inline CSS properties.
The above is the detailed content of How to Get the Computed Font Size of a DOM Element in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

What's happening when you see some JavaScript that calls super()?.In a child class, you use super() to call its parent’s constructor and super. to access its

JavaScript has a variety of built-in popup APIs that display special UI for user interaction. Famously:

I was chatting with some front-end folks the other day about why so many companies struggle at making accessible websites. Why are accessible websites so hard

There is an HTML attribute that does exactly what you think it should do:

People say JAMstack sites are fast — let’s find out why by looking at real performance metrics! We’ll cover common metrics, like Time to First Byte

I've been playing around with CSS Custom Properties to discover their power since browser support is finally at a place where we can use them in our


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software