The CanvasRenderingContext2D.font property of the Canvas 2D API specifies the current text style being used when drawing text. This string uses the same syntax as the CSS font specifier. The default font is 10px sans-serif.


ctx.font = value;


A DOMString parsed as CSS font value. The default font is 10px sans-serif.


Using the font property

This is just a simple code snippet using the font property to set a different font size and font family.


<canvas id="canvas"></canvas>


var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.font = '48px serif';
ctx.strokeText('Hello world', 50, 100);

Edit the code below and see your changes update live in the canvas:

Loading fonts with the CSS Font Loading API

With the help of the FontFace API, you can explicitly load fonts before using it in canvas.

var f = new FontFace('test', 'url(x)');

f.load().then(function() {
  // Ready to use the font in a canvas context


Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.font' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge Full support 12Firefox Full support 3.5IE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes


Full support  
Full support

Gecko-specific notes

  • In Gecko-based browsers, such as Firefox, a non-standard and deprecated property ctx.mozTextStyle is implemented besides this property. Don't use it.

Quantum CSS notes

  • In Gecko, when you set a system font as the value of a canvas 2D context's font (e.g. menu), getting the font value fails to return the expected font (it returns nothing). This is fixed in Firefox's new parallel CSS engine (also known as Quantum CSS or Stylo, planned for release in Firefox 57) (bug 1374885).

See also

Document Tags and Contributors

Contributors to this page: fscholz, chrisdavidmills, SamStonehouse, nmve, erikadoyle
Last updated by: fscholz,