HTML canvas textAlign attribute
Example
Create a red line at position 150. Position 150 is the anchor for all text defined in the example below. Study the effect of each textAlign attribute value:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// Create a red line in position 150
ctx.strokeStyle="red";
ctx.moveTo(150,20);
ctx.lineTo(150,170);
ctx.stroke();
ctx.font="15px Arial";
// Show the different textAlign values
ctx.textAlign="start";
ctx.fillText("textAlign=start",150,60);
ctx.textAlign="end";
ctx.fillText("textAlign=end",150,80);
ctx.textAlign="left";
ctx.fillText("textAlign=left",150,100);
ctx.textAlign="center";
ctx.fillText("textAlign=center",150,120);
ctx.textAlign="right";
ctx.fillText("textAlign=right",150,140);
Try it »
Browser support





Internet Explorer 9, Firefox, Opera, Chrome and Safari support the textAlign attribute.
**Note:** Internet Explorer 8 and earlier versions do not support
Definition and Usage
The textAlign attribute sets or returns the current alignment of the text content relative to the anchor.
Normally, the text starts from the specified positionat the beginning, however, if you set textAlign="right" and place the text at position 150, it will beposition 150 at the end.
**Tip:** Please use fillText() or strokeText() method actually draws and positions the text on the canvas.
| Default value: | start |
|---|---|
| JavaScript syntax: | context.textAlign="center|end|left|right|start"; |
Attribute values
| Value | Description |
|---|---|
| start | Default. The text starts at the specified position. |
| end | The text ends at the specified position. |
| center | The center of the text is placed at the specified position. |
| left | The text starts at the specified position. |
| right | The text ends at the specified position. |
HTML canvas Reference Manual