From this answer, the rendered fonts can be inspected via Chrome's Dev Tools. But how can we get these info directly from the JavaScript?
For example, I want to create a webpage, which shows the actual rendered fonts on this page.
From this answer, the rendered fonts can be inspected via Chrome's Dev Tools. But how can we get these info directly from the JavaScript?
For example, I want to create a webpage, which shows the actual rendered fonts on this page.
I think this function will work
var div = document.getElementById( 'show' );
function showCSS( element, property ) {
return window.getComputedStyle( element, null ).getPropertyValue( property );
}
let fontName = showCSS( div, 'font-family' );
var para = document.createElement("p");
var node = document.createTextNode('Font Name: ' + fontName);
para.appendChild(node);
var element = document.getElementById("fontName");
element.appendChild(para)
div {
font-family: Arial
}
<div id="show">Text: Show Font Name</div>
<div id="fontName"></div>