Javascript DOM CSS Style textAlign Property

Introduction

Center-align the text in a <p> element:

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

View in separate window

<!DOCTYPE html>
<html>
<body>

<p id="myP">This is an example paragraph.</p>

<button type="button" onclick="myFunction()">Align text</button>

<script>
function myFunction() {/*from   www  .  ja  v  a  2 s .  c o  m*/
  document.getElementById("myP").style.textAlign = "center";
}
</script>

</body>
</html>

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

Property Values

Value Description
leftAligns the text to the left. default
right Aligns the text to the right
center Centers the text
justify The text is justified
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

The textAlign property returns a String representing the horizontal alignment of text within the element.




PreviousNext

Related