Computer >> Computer tutorials >  >> Programming >> Javascript

How to set the horizontal alignment of text with JavaScript?


Use the textAlign property in JavaScript and set it to right for aligning it horizontally. You can try to run the following code to return the horizontal alignment of text with JavaScript −

Example

<!DOCTYPE html>
<html>
   <body>
      <p id = "myText">This is demo text</p>
      <button onclick = "display()">Set Horizontal Alignment</button>
      <script>
         function display() {
            document.getElementById("myText").style.textAlign = "right";
         }
      </script>
   </body>
</html>