HTML Canvas Text

Beginner
8 min read
Updated: Nov 2026
5 examples
Live preview

Definition

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.

Syntax

text.js
ctx.font = "bold 32px sans-serif";
ctx.fillStyle = "#2563eb";
ctx.fillText("Hello", x, y);

ctx.strokeStyle = "#0f172a";
ctx.lineWidth = 2;
ctx.strokeText("Hello", x, y);

// Optional fourth argument
ctx.fillText("Long label", x, y, maxWidth);

Measure with const m = ctx.measureText(text); then use m.width.

Key Methods & Properties

Core text tools:

APIDescriptionTry it
fillText(text, x, y [, maxWidth])Fill glyphs with fillStyleTry it
strokeText(text, x, y [, maxWidth])Outline glyphs with strokeStyleTry it
fontCSS font string (default 10px sans-serif)Try it
textAlignstart | end | left | right | centerTry it
textBaselinetop | middle | alphabetic | …Try it
measureText(text)Returns TextMetrics (use .width)Try it

Global Attributes

The <canvas> tag also supports the Global Attributes in HTML.

Live Preview

Switch alignment and fill vs stroke. The guide line marks the (x, y) point.

Default baseline is alphabetic; preview uses middle for easier centering.

Canvas

textAlign = "center" · fillText

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — fillText

Set a CSS-like font, then fill the string.

HTML
<canvas id="demo" width="300" height="160" aria-label="fillText Hello on canvas"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.font = "bold 36px sans-serif";
  ctx.fillStyle = "#2563eb";
  ctx.fillText("Hello", 40, 95);
</script>
Try It Yourself

How It Works

  1. Set font with CSS syntax (size + family).
  2. Set fillStyle for the ink color.
  3. Call fillText with the string and position.
  4. y = 95 is the alphabetic baseline by default.

Example 2 — strokeText

Outline letters with strokeStyle and lineWidth.

HTML
<canvas id="demo" width="300" height="160" aria-label="strokeText outline on canvas"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.font = "bold 42px sans-serif";
  ctx.strokeStyle = "#0f172a";
  ctx.lineWidth = 2;
  ctx.strokeText("Outline", 40, 95);
</script>
Try It Yourself

How It Works

  1. Same font as for fills.
  2. Use strokeStyle instead of fillStyle.
  3. Set lineWidth so the outline is visible.
  4. strokeText draws hollow glyphs.

Example 3 — textAlign Center

Center a label on a vertical guide.

HTML
<canvas id="demo" width="300" height="160" aria-label="Centered canvas text"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const cx = 150;

  ctx.strokeStyle = "#cbd5e1";
  ctx.beginPath();
  ctx.moveTo(cx, 20);
  ctx.lineTo(cx, 140);
  ctx.stroke();

  ctx.font = "bold 28px sans-serif";
  ctx.textAlign = "center";
  ctx.fillStyle = "#7c3aed";
  ctx.fillText("Centered", cx, 90);
</script>
Try It Yourself

How It Works

  1. Draw a guide at the intended center X.
  2. Set textAlign = "center".
  3. Pass that X to fillText.
  4. Also try left / right to see the pivot change.

Example 4 — textBaseline Middle

Vertically center text on a horizontal line.

HTML
<canvas id="demo" width="300" height="160" aria-label="textBaseline middle example"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const cy = 80;

  ctx.strokeStyle = "#cbd5e1";
  ctx.beginPath();
  ctx.moveTo(20, cy);
  ctx.lineTo(280, cy);
  ctx.stroke();

  ctx.font = "bold 28px sans-serif";
  ctx.textBaseline = "middle";
  ctx.fillStyle = "#16a34a";
  ctx.fillText("middle", 40, cy);
</script>
Try It Yourself

How It Works

  1. Draw a horizontal guide at cy.
  2. Set textBaseline = "middle".
  3. Pass the same Y to fillText.
  4. Combine with textAlign = "center" to pin text to a point.

Example 5 — measureText Width

Measure a string, then draw a background that fits it.

HTML
<canvas id="demo" width="300" height="160" aria-label="measureText background box"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  const label = "Badge";
  const x = 40, y = 90;
  const pad = 12;

  ctx.font = "bold 28px sans-serif";
  const w = ctx.measureText(label).width;

  ctx.fillStyle = "#dbeafe";
  ctx.fillRect(x - pad, y - 28, w + pad * 2, 44);

  ctx.fillStyle = "#1d4ed8";
  ctx.fillText(label, x, y);
</script>
Try It Yourself

How It Works

  1. Set the same font you will use to draw.
  2. measureText(label).width returns the pixel width.
  3. Draw a padded rect using that width.
  4. 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.

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 Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
fillText / strokeText Widely supported

Bottom line: Safe for production labels, HUD text, and outlined titles on Canvas 2D everywhere modern browsers run.

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.

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.

Next: Canvas Composition

Learn globalCompositeOperation to blend text and shapes.

Canvas Composition →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful