The CanvasRenderingContext2D.textBaseline property of the Canvas 2D API specifies the current text baseline being used when drawing text.


ctx.textBaseline = "top" || "hanging" || "middle" || "alphabetic" || "ideographic" || "bottom";


Possible values:

The text baseline is the top of the em square.
The text baseline is the hanging baseline. (Used by Tibetan and other Indic scripts.)
The text baseline is the middle of the em square.
alphabetic (default value)
The text baseline is the normal alphabetic baseline.
The text baseline is the ideographic baseline; this is the bottom of the body of the characters, if the main body of characters protrudes beneath the alphabetic baseline. (Used by Chinese, Japanese and Korean scripts.)
The text baseline is the bottom of the bounding box. This differs from the ideographic baseline in that the ideographic baseline doesn't consider descenders.

The default value is alphabetic.


Using the textBaseline property

This demonstrates the different textBaseline property values.


<canvas id="canvas" width="550" height="500"></canvas>


var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'];
ctx.font = '36px serif';
ctx.strokeStyle = 'red';

baselines.forEach(function (baseline, index) {
    ctx.textBaseline = baseline;
    var y = 75 + index * 75;
    ctx.beginPath(); ctx.moveTo(0, y + 0.5); ctx.lineTo(550, y + 0.5); ctx.stroke();
    ctx.fillText('Abcdefghijklmnop (' + baseline + ')', 0, y);

Edit the code below and see your changes update live in the canvas:


Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.textBaseline' in that specification.
Living Standard  

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) 3.5 (1.9.1) 9 (Yes) (Yes)
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) 1.0 (1.9.1) (Yes) (Yes) (Yes)

See also

Document Tags and Contributors

 Contributors to this page: RexHsu, zbjornson, nmve, erikadoyle, fscholz, Dmandelin
 Last updated by: RexHsu,