text-align Attribute | textAlign Property

Sets or retrieves whether the text in the object is left-aligned, right-aligned, centered, or justified.

Syntax

HTML { text-align : sAlign }
Scripting[ sAlign = ] object.style.textAlign

Possible Values

sAlignString that specifies or receives one of the following values:
left
Default. Text is aligned to the left.
right
Text is aligned to the right.
center
Text is centered.
justify
Text is justified.

The property is read/write for all objects except the following, for which it is read-only: currentStyle. The property has a default value of left. The Cascading Style Sheets (CSS) attribute is inherited.

DHTML expressions can be used in place of the preceding value(s). As of Internet Explorer 8, expressions are not supported in IE8 mode. For more information, see About Dynamic Properties.

Remarks

The property applies to block elements. The property is inherited by all block-level objects inside a div object. This parameter receives null if the attribute is not set.

The justify possible value is available as of Microsoft Internet Explorer 4.0.

Examples

The following examples use the text-align attribute and the textAlign property to align text within the object.

This example uses p as a selector and two classes to call an embedded style sheet that aligns the text according to the respective rule.

<STYLE>
    P { text-align:center }
    .align1 { text-align:right }
    .align2 { text-align:justify }
</STYLE>
</HEAD>
<BODY>
<P onclick= "this.className='align1'" 
    ondblclick="this.className='align2'">
. . . </P>
This feature requires Microsoft Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

This example uses inline scripting to change the alignment of the text when an onmouseover event occurs.

<P STYLE="font-size:14" 
    onmouseover="this.style.textAlign='center'">
. . . </P>
This feature requires Microsoft Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This property is defined in Cascading Style Sheets (CSS), Level 1 (CSS1) World Wide Web link.

Applies To

BLOCKQUOTE, BODY, CENTER, currentStyle, DD, DIR, DIV, DL, DT, FIELDSET, FORM, hn, HR, INPUT type=password, INPUT type=text, LI, LISTING, MARQUEE, MENU, OL, P, PLAINTEXT, PRE, runtimeStyle, style, TABLE, TD, TEXTAREA, TH, TR, UL, XMP, CSSCurrentStyleDeclaration Constructor, CSSRuleStyleDeclaration Constructor, CSSStyleDeclaration Constructor
Tags :


Community Content

yecril
Odd behaviour under Transitional DTD

As of IE7, text-align is not inherited by tables if the document type is transitional.

Tags : conformance

yecril
INPUT type=text
An input field is not a block-level element; the behaviour of Internet Explorer is nonstandard.
Tags : conformance

Page view tracker