Style textAlign attribute

Definition and usage

textAlign Sets or returns the horizontal alignment of the text in a block-level element.

See also:

CSS Tutorial:CSS Text

CSS Reference Manual:text-align attribute

Instance

Example 1

The text within the <p> element is centered:

document.getElementById("myP").style.textAlign = "center";

Try it yourself

Example 2

Returns the text alignment of the <p> element:

alert(document.getElementById("myP").style.textAlign);

Try it yourself

Syntax

Return textAlign attribute:

object.style.textAlign

Set textAlign attribute:

object.style.textAlign = "left|right|center|justify|initial|inherit"

Attribute value

Value Description
left Align the text to the left. Default.
right Align the text to the right.
center Center the text.
justify The text is adjusted (for left and right alignment).
initial Sets this attribute to its default value. See initial.
inherit This attribute inherits from its parent element. See inherit.

Technical details

Default value: left
Return value: A string indicating the horizontal alignment of the text within the element.
CSS Version: CSS1

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support