Style textAlignProperty

Style 对象参考手册Style Object

Definition and Usage

The textAlign property sets or returns the horizontal alignment of text in a block-level element.

Syntax

Set the textAlign property:

Object.style.textAlign="left|right|center|justify|inherit"

Return the textAlign property:

Object.style.textAlign

Value Description
left Default. Aligns text to the left.
right Aligns text to the right.
center Aligns text to the center.
justify Aligns text according to the textJustify property.
inherit The value of the textAlign property is inherited from the parent element.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the textAlign property.

Note:IE7 and earlier versions do not support the "inherit" value. IE8 supports "inherit" only if a !DOCTYPE is specified. IE9 supports "inherit".


Example

Example

Center-align the text in a p element:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
<script>
function displayResult(){
    document.getElementById("p1").style.textAlign="center";
}
</script>
</head>
<body>

<p id="p1">This is some text.</p>
<br>
<button type="button" onclick="displayResult()">Align text</button>

</body>
</html>

Try it yourself »


Style 对象参考手册Style Object Other Extensions