On an <canvas>, draw text with fillText(text, x, y) or strokeText(text, x, y). Style it with font (CSS font syntax), textAlign, and textBaseline.
Default font is 10px sans-serif. Default baseline is alphabetic, so y is the baseline — not the top of the letters. Canvas does not auto-wrap; use maxWidth or measure and split lines yourself.
Baseline · Widely available.
fillText, strokeText, and text styling properties are stable Canvas 2D features across modern browsers since about July 2015.
Baseline vs top.
With the default textBaseline = "alphabetic", text sits on y. If letters look too high or low, set top, middle, or bottom.
Then fillText on top — useful for chips and labels.
Edge Cases & Pitfalls
Most text bugs are baseline confusion, missing fonts, or expecting auto-wrap.
y
Y is the baseline by default
Alphabetic baseline means descenders sit below y. Switch to top or middle when laying out boxes.
wrap
No automatic wrapping
Long strings overflow. Pass maxWidth to shrink one line, or split into multiple fillText calls.
font
Font must be available
Custom webfonts need to be loaded before drawing, or the browser falls back. Prefer system stacks for simple demos.
a11y
Pixels are not selectable text
Canvas text is a bitmap. Provide an accessible name on the canvas (or adjacent HTML) for screen readers when text matters.
Compatibility
Browser Support
fillText, strokeText, and text style properties are part of the Canvas 2D API in every modern browser.
✓ Baseline · Widely available
Canvas text
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support fillText, strokeText, font, alignment, and measureText. Internet Explorer 9+ also supports the core APIs.
98%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
Supported
fillText / strokeTextWidely supported
Bottom line: Safe for production labels, HUD text, and outlined titles on Canvas 2D everywhere modern browsers run.
Remember
Key Takeaways
Draw:fillText or strokeText.
Style:font, textAlign, textBaseline.
Measure:measureText(text).width.
Remember: no auto-wrap; baseline matters.
One line: set font & align → fill or stroke text.
UA style
Default CSS Settings
Text drawing is a Canvas API concern. The <canvas> element still uses typical user-agent CSS:
default.css
canvas {
display: inline;
}
Canvas default text style is font: 10px sans-serif on the context — not CSS on the element.
Frequently Asked Questions
Set ctx.font (CSS font syntax), optionally set textAlign and textBaseline, then call fillText(text, x, y) or strokeText(text, x, y).
By default textBaseline is "alphabetic", so y is the text baseline — not the top of the letters. Use "top", "middle", or "bottom" when that is clearer.
Set textAlign = "center" and optionally textBaseline = "middle", then pass the center point as (x, y).
No. Canvas does not auto-wrap. Split lines yourself, or use maxWidth to shrink a single line to fit.
measureText(text) returns a TextMetrics object. Use metrics.width to size boxes, center labels, or decide where to wrap.
fillText paints the glyph interiors with fillStyle. strokeText outlines glyphs with strokeStyle and lineWidth.