Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the actual rendered font when it's not defined in CSS?

How do I get the actual font face and font size of an element when the CSS font-face and font-size properties are not defined?

For example, the JavaScript snippet

object.style.fontFamily

does not return any value. That's pretty obvious, assuming CSS hasn't applied a style to object anywhere. But, of course, a certain font is used to render the text, probably the system font or the webbrowser default font.

So can, for instance, JavaScript get that rendered font?

like image 942
MC Emperor Avatar asked Sep 16 '11 12:09

MC Emperor


People also ask

How do I find the rendered font?

There is a better way! 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.

How do you reference OTF fonts in CSS?

Add a font-face section to your CSS codesrc: url('fonts/lovely_font. otf') format('opentype'); src: url('fonts/lovely_font. ttf') format('truetype'); As another optional efficiency measure, we can get the browser to check for a local copy of the font in case the user already has it.

Can JavaScript get the font that is rendered?

But, of course, a certain font is used to render the text, probably the system font or the webbrowser default font. So can, for instance, JavaScript get that rendered font? Java or Flash can do it. JS - cannot give you names of the font, but you can try to detect font by deciphering text render in canvas.

Is there a way to check if font rendering has changed?

I found a way (for all browsers which support <canvas> ), by checking pixel for pixel if the font rendering has changed If you are using this for another dialect (e.g. japanese) you may want to change the testString variable to the most common characters in that dialect.

How to find out what font is being used?

There is no standard, reliable method for determining the actual font being used. The previous answers here will report the styled fontFamily style value, but that can be a list of font names and doesn't specifically identify the actual font rendered (which was the actual question posed here).

Can you name a font but render it as Consolas?

You can name a font Foo, but still render Arial, or reference monospace, which renders Consolas. Chrome knows, but doesn't expose that info = ( The devtools have a Rendered fonts section in Computed which is always correct, but it uses those hidden methods.


3 Answers

I suggest this function:

function css( element, property ) {     return window.getComputedStyle( element, null ).getPropertyValue( property ); } 

Usage:

css( object, 'font-size' ) // returns '16px' for instance 

Note: getComputedStyle doesn't work in IE8.

Live demo: http://jsfiddle.net/4mxzE/

like image 125
Šime Vidas Avatar answered Oct 16 '22 03:10

Šime Vidas


There is no standard, reliable method for determining the actual font being used. The previous answers here will report the styled fontFamily style value, but that can be a list of font names and doesn't specifically identify the actual font rendered (which was the actual question posed here).

(As mentioned in some comments, there are ways to guess at the font by inspecting visual cues, but that isn’t likely to be 100% reliable.)

like image 44
danorton Avatar answered Oct 16 '22 03:10

danorton


You can find the information about the rendered font in Chrome/Firefox Developer Tools. Try inspecting the paragraph in the following code snippet:

p { font-family: sans-serif;  }
<p>Some text and <span title="an emoji">😁</span></p>

In Chrome Developer Tools (tested on 55.0.2883.75 m 64-bit) you get the following information:

Chrome Developer Tools > Element > Computed Tab


In Firefox Developer Tools (tested on 47.0.2 with about:config > devtools.fontinspector.enabled = true) you get the following information:

Firefox Developer Tools > Element > Fonts Tab

like image 43
Salman A Avatar answered Oct 16 '22 04:10

Salman A