W3Schools.com

HTML5 canvas textBaseline Attribute

HTML5 track Tag Reference HTML5 canvas reference

Example

Write text with different textBasline alignment:

Yor browser does not support the canvas tag.

JavaScript:

...
ctx.textBaseline="bottom";
ctx.fillText("Bottom",10,50);
ctx.textBaseline="middle";
ctx.fillText("Middle",97,50);
ctx.textBaseline="top";
ctx.fillText("Top",175,50);
...

Try it yourself »

Definition and Usage

The textBaseline attribute is used to vertically align text on the canvas.

Imagine there are lines running through the letters:

textBaseline example

The fillText() and strokeText() methods will use these lines when positioning the text.

The different attributevalues will produce this output:

textBaseline example

Default value: alphabetic
JavaScript syntax: context.textBaseline="middle";


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The textBaseline attribute is supported in all major browsers, with some differences, especially when using the "hanging" and the "ideographic" values.


Attribute Values

Values Description Play it
alphabetic Aligns the text vertically at the alphabetical line (see illustration above) Play it »
bottom Aligns the text vertically at the bottom line (see illustration above) Play it »
hanging Aligns the text vertically at the hanging-line (see illustration above) Play it »
ideographic Aligns the text vertically at the ideographical line (see illustration above) Play it »
middle Aligns the text vertically at the top line (see illustration above) Play it »

HTML5 track Tag Reference HTML5 canvas reference


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE