Consider that I have this CSS rule for an anchor tag:
font-family: Helvetica, Verdana, Calibri, Arial, sans-serif;
Of course by watching at what is rendered in the browser, I can judge which of these fonts has already been used (applied) to format the current anchor element's text.
However, I need to know which font is currently in use via JavaScript (jQuery for example). This jQuery code doesn't help me:
$('#anchor-id').css('font-family');
because it returns 'Helvetica, Verdana, Calibri, Arial, sans-serif;'
. Is there a way to know which font is in action now?
Update: Based on @MC answer, I created this fiddle and you can check that it's working just nice and quite.
check() The check() method of the FontFaceSet returns whether all fonts in the given font list have been loaded and are available.
Chrome Developer tools can show you the exact rendered fonts for a given web page with just a few clicks. Right click on any element in the page and select Inspect. Next head over to the Computed tab, scroll down, and you'll quickly notice the rendered fonts for the page.
Best Web Safe Fonts for HTML and CSSArial (sans-serif) Verdana (sans-serif) Tahoma (sans-serif) Trebuchet MS (sans-serif)
To change font type purely with HTML, use the CSS font-family property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.
First, you need to test if a font has been installed. I have used some scripts on the internet which will test if a font is installed or not.
Include this snippet of code somewere (thanks to Lucas Smith):
var Font = {
isInstalled : function (name) {
name = name.replace(/['"<>]/g,'');
var body = document.body;
var test = document.createElement('div');
var installed = false;
var teststring = "mmmmmmmmwwwwwwww";
var template = '<b style="display:inline !important; width:auto !important; font:normal 10px/1 \'X\',sans-serif !important">' + teststring + '</b><b style="display:inline !important; width:auto !important; font:normal 10px/1 \'X\',monospace !important">' + teststring + '</b>';
var ab;
if (name) {
test.innerHTML = template.replace(/X/g, name);
test.style.cssText = 'position: absolute; visibility: hidden; display: block !important';
body.insertBefore(test, body.firstChild);
ab = test.getElementsByTagName('b');
installed = ab[0].offsetWidth === ab[1].offsetWidth;
body.removeChild(test);
}
return installed;
}
}
In addition, use the following snippet (I wrote that myself) to get the font-family
value and split that value into parts. Each part is a font, as they are separated by a comma in the CSS code (e.g. font-family: "Nimbus", "Courier New";
):
function workingFont(element) {
var fontString = $(element).css('font-family');
var fonts = fontString.split(",");
for (var f in fonts) {
if (Font.isInstalled(fonts[f])) {
return fonts[f];
}
}
}
As you can see, the first installed font will be returned.
Use workingFont(element)
, where element is the element id, class or tagname. This is part of the jQuery API. Notice you must have jQuery included to get the aforementioned script working.
Update: I created this jsfiddle to test it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With