How do I style HTML5 canvas text to be bold and/or italic?
Just an additional heads up for anyone who stumbles across this: be sure to follow the order shown in the accepted answer.
I ran into some cross-browser issues when I had the wrong order. Having "10px Verdana bold" works in Chrome, but not in IE or Firefox. Switching it to "bold 10px Verdana" as indicated fixed those problems. Double-check the order if you run into similar problems.
From the MDN documentation on CanvasRenderingContext2D.font
:
The
CanvasRenderingContext2D.font
property of the Canvas 2D API specifies the current text style to use when drawing text. This string uses the same syntax as the CSS font specifier.
So, that means any of the following will work:
ctx.font = "italic 10pt Courier";
ctx.font = "bold 10pt Courier";
ctx.font = "italic bold 10pt Courier";
Here are a couple of additional resources for more information:
- Dive into HTML5
- HTML5 canvas - the basics