The DOM style textAlign property returns and modify the horizontal alignment of text in a block level element in an HTML document.
Syntax
Following is the syntax −
Returning textAlign
object.style.textAlign
2. Modifying textAlign
object.style.textAlign = “value”
Example
Let us see an example of style textAlign property −
<!DOCTYPE html> <html> <head> <style> body { color: #000; background: lightblue; height: 100vh; } p { border: 2px solid #fff; margin: 1.5rem auto; } .btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; } </style> </head> <body> <h1>DOM Style textAlign Property Example</h1> <p>This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.</p> <button onclick="add()" class="btn">Set textAlign</button> <script> function add() { document.querySelector('p').style.textAlign = "center"; } </script> </body> </html>
Output
This will produce the following output −
Click on “Set textAlign” button to align the text inside the paragraph element.