Canvas textBaseline: Control Vertical Text Alignment

The Canvas textBaseline property controls which vertical baseline is placed at the y coordinate supplied to fillText() or strokeText().

Valid values are top, hanging, middle, alphabetic, ideographic, and bottom. The default is alphabetic.

ctx.textBaseline = 'middle';
ctx.fillText('Canvas text', 80, 100);

Compare Canvas text baselines Top ↑

Canvas text baseline comparison.

What the baseline values mean Top ↑

topTop of the em square
hangingHanging baseline used by some scripts
middleMiddle of the em square
alphabeticNormal alphabetic baseline; default
ideographicIdeographic baseline
bottomBottom of the bounding box

textBaseline vs textAlign Top ↑

textBaseline controls vertical positioning around y; textAlign controls horizontal positioning around x. They can be combined.

Frequently asked questions Top ↑

What is the default Canvas textBaseline? Top ↑

The default is alphabetic.

Does top mean the top of every visible glyph? Top ↑

It is defined using the em square, so it is not simply the top pixel of each glyph.

What is middle based on? Top ↑

middle aligns the middle of the em square with the y coordinate.

Why do fonts look different on the same baseline? Top ↑

Different fonts have different metrics and glyph shapes even when they use the same baseline setting.

Does textBaseline affect measureText()? Top ↑

TextMetrics can report baseline and bounding-box measurements; set the intended font and text settings before measuring.

textAlign | fillText() | strokeText() | font | Text demo | Canvas tutorial





plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer